Skip to content

Commit cf70739

Browse files
committed
fix: bind correct context to process
1 parent 8e950b2 commit cf70739

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/JobProcessor.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class JobProcessor {
121121
// Make sure an interval has actually been set
122122
// Prevents race condition with 'Agenda.stop' and already scheduled run
123123
if (!this.isRunning) {
124-
log.extend('process')('JobProcessor got stopped already, returning', this);
124+
log.extend('process')('JobProcessor got stopped already, returning');
125125
return;
126126
}
127127

@@ -482,8 +482,7 @@ export class JobProcessor {
482482
// const a = new Error();
483483
// console.log('STACK', a.stack);
484484
log.extend('runOrRetry')(
485-
'JobProcessor got stopped already while calling runOrRetry, returning!',
486-
this
485+
'JobProcessor got stopped already while calling runOrRetry, returning!'
487486
);
488487
return;
489488
}
@@ -510,11 +509,22 @@ export class JobProcessor {
510509
setTimeout(async () => {
511510
// when job is not running anymore, just finish
512511
if (!jobIsRunning) {
512+
log.extend('runOrRetry')(
513+
'[%s:%s] checkIfJobIsStillAlive detected job is not running anymore. stopping check.',
514+
job.attrs.name,
515+
job.attrs._id
516+
);
513517
resolve();
514518
return;
515519
}
516520

517521
if (await job.isExpired()) {
522+
log.extend('runOrRetry')(
523+
'[%s:%s] checkIfJobIsStillAlive detected an expired job, killing it.',
524+
job.attrs.name,
525+
job.attrs._id
526+
);
527+
518528
reject(
519529
new Error(
520530
`execution of '${job.attrs.name}' canceled, execution took more than ${
@@ -526,6 +536,12 @@ export class JobProcessor {
526536
}
527537

528538
if (!job.attrs.lockedAt) {
539+
log.extend('runOrRetry')(
540+
'[%s:%s] checkIfJobIsStillAlive detected a job without a lockedAt value, killing it.',
541+
job.attrs.name,
542+
job.attrs._id
543+
);
544+
529545
reject(
530546
new Error(
531547
`execution of '${job.attrs.name}' canceled, no lockedAt date found. Ensure to call touch() for long running jobs to keep them alive.`

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ export class Agenda extends EventEmitter {
538538
this.attrs.processEvery
539539
);
540540

541-
this.on('processJob', this.jobProcessor.process);
541+
this.on('processJob', this.jobProcessor.process.bind(this.jobProcessor));
542542
}
543543

544544
/**
@@ -562,7 +562,7 @@ export class Agenda extends EventEmitter {
562562
await this.db.unlockJobs(jobIds);
563563
}
564564

565-
this.off('processJob', this.jobProcessor.process);
565+
this.off('processJob', this.jobProcessor.process.bind(this.jobProcessor));
566566

567567
this.jobProcessor = undefined;
568568
}

test/agenda.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ describe('Agenda', () => {
744744
}); */
745745

746746
let j1processes = 0;
747+
747748
globalAgenda.define('j1', (_job, done) => {
748749
j1processes += 1;
749750
done();
@@ -766,7 +767,7 @@ describe('Agenda', () => {
766767
await globalAgenda.every('10 seconds', 'j2');
767768
await globalAgenda.every('15 seconds', 'j3');
768769

769-
await delay(5001);
770+
await delay(3001);
770771

771772
process.removeListener('unhandledRejection', rejectionsHandler);
772773

0 commit comments

Comments
 (0)