Skip to content

Commit b248a2b

Browse files
committed
fix: tests, agenda-instance should have a smaller processEvery
1 parent 71ff8a2 commit b248a2b

File tree

6 files changed

+45
-32
lines changed

6 files changed

+45
-32
lines changed

.mocharc.jsonc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"recursive": true,
88
"reporter": "spec",
99
"slow": 75,
10-
"timeout": 5000,
10+
"timeout": 25000,
1111
"ui": "bdd",
1212
"exit": true
1313
}

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
"build": "tsc",
1919
"test": "npm run lint && npm run mocha",
2020
"lint": "eslint",
21-
"mocha": "mocha -r ts-node/register --reporter spec --timeout 8000 -b",
22-
"mocha-debug": "DEBUG=agenda:**,-agenda:internal:** mocha -r ts-node/register --reporter spec --timeout 8000 -b",
23-
"mocha-debug-internal": "DEBUG=agenda:internal:** mocha -r ts-node/register --reporter spec --timeout 8000 -b",
24-
"mocha-debug-all": "DEBUG=agenda:** mocha -r ts-node/register --reporter spec --timeout 8000 -b",
21+
"mocha": "mocha -r ts-node/register --reporter spec -b",
22+
"mocha-debug": "DEBUG=agenda:**,-agenda:internal:** mocha -r ts-node/register --reporter spec -b",
23+
"mocha-debug-internal": "DEBUG=agenda:internal:** mocha -r ts-node/register --reporter spec -b",
24+
"mocha-debug-all": "DEBUG=agenda:** mocha -r ts-node/register --reporter spec -b",
2525
"docs": "jsdoc --configure .jsdoc.json --verbose"
2626
},
2727
"config": {

src/JobProcessor.ts

+35-24
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export class JobProcessor {
7777
) {
7878
log('creating interval to call processJobs every [%dms]', processEvery);
7979
this.processInterval = setInterval(() => this.process(), processEvery);
80+
this.process();
8081
}
8182

8283
stop(): Job[] {
@@ -345,34 +346,33 @@ export class JobProcessor {
345346
// Check if there is any job that is not blocked by concurrency
346347
const job = this.jobQueue.returnNextConcurrencyFreeJob(this.jobStatus);
347348

348-
if (job) {
349+
if (!job) {
350+
log.extend('jobProcessing')('[%s:%s] there is no job to process');
351+
return;
352+
}
353+
354+
log.extend('jobProcessing')('[%s:%s] there is a job to process', job.attrs.name, job.attrs._id);
355+
356+
// If the 'nextRunAt' time is older than the current time, run the job
357+
// Otherwise, setTimeout that gets called at the time of 'nextRunAt'
358+
if (job.attrs.nextRunAt <= now) {
349359
log.extend('jobProcessing')(
350-
'[%s:%s] there is a job to process',
360+
'[%s:%s] nextRunAt is in the past, run the job immediately',
351361
job.attrs.name,
352362
job.attrs._id
353363
);
354-
355-
// If the 'nextRunAt' time is older than the current time, run the job
356-
// Otherwise, setTimeout that gets called at the time of 'nextRunAt'
357-
if (job.attrs.nextRunAt <= now) {
358-
log.extend('jobProcessing')(
359-
'[%s:%s] nextRunAt is in the past, run the job immediately',
360-
job.attrs.name,
361-
job.attrs._id
362-
);
363-
this.runOrRetry(job);
364-
} else {
365-
const runIn = job.attrs.nextRunAt.getTime() - now.getTime();
366-
log.extend('jobProcessing')(
367-
'[%s:%s] nextRunAt is in the future, calling setTimeout(%d)',
368-
job.attrs.name,
369-
job.attrs._id,
370-
runIn
371-
);
372-
setTimeout(() => {
373-
this.jobProcessing();
374-
}, runIn);
375-
}
364+
this.runOrRetry(job);
365+
} else {
366+
const runIn = job.attrs.nextRunAt.getTime() - now.getTime();
367+
log.extend('jobProcessing')(
368+
'[%s:%s] nextRunAt is in the future, calling setTimeout(%d)',
369+
job.attrs.name,
370+
job.attrs._id,
371+
runIn
372+
);
373+
setTimeout(() => {
374+
this.jobProcessing();
375+
}, runIn);
376376
}
377377
}
378378

@@ -437,6 +437,11 @@ export class JobProcessor {
437437
log.extend('runOrRetry')('[%s:%s] processing job', job.attrs.name, job.attrs._id);
438438
// CALL THE ACTUAL METHOD TO PROCESS THE JOB!!!
439439
await job.run();
440+
log.extend('runOrRetry')(
441+
'[%s:%s] processing job successfull',
442+
job.attrs.name,
443+
job.attrs._id
444+
);
440445

441446
// Job isn't in running jobs so throw an error
442447
if (!this.runningJobs.includes(job)) {
@@ -449,6 +454,12 @@ export class JobProcessor {
449454
);
450455
}
451456
} catch (err) {
457+
log.extend('runOrRetry')(
458+
'[%s:%s] processing job failed',
459+
job.attrs.name,
460+
job.attrs._id,
461+
err
462+
);
452463
job.agenda.emit('error', err);
453464
} finally {
454465
// Remove the job from the running queue

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export class Agenda extends EventEmitter {
8888

8989
this.attrs = {
9090
name: config.name || '',
91-
processEvery: humanInterval(config.processEvery) || humanInterval('5 seconds'),
91+
processEvery:
92+
(config.processEvery && humanInterval(config.processEvery)) || humanInterval('5 seconds'),
9293
defaultConcurrency: config.defaultConcurrency || 5,
9394
maxConcurrency: config.maxConcurrency || 20,
9495
defaultLockLimit: config.defaultLockLimit || 0,

test/fixtures/agenda-instance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const agenda = new Agenda(
88
{
99
db: {
1010
address: connStr
11-
}
11+
},
12+
processEvery: 100
1213
},
1314
async () => {
1415
tests.forEach(test => {

test/job.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ describe('Job', () => {
12031203
});
12041204

12051205
let ran1 = false;
1206-
let ran2 = true;
1206+
let ran2 = false;
12071207
let doneCalled = false;
12081208

12091209
const serviceError = function (e) {

0 commit comments

Comments
 (0)