Skip to content

Commit b8cc61f

Browse files
authored
fix(jobprocessor): check for object.fromEntries for node 10 support (#3)
1 parent b13d054 commit b8cc61f

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/JobProcessor.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ export class JobProcessor {
2525
// eslint-disable-next-line @typescript-eslint/no-var-requires,global-require
2626
const { version } = require('../package.json');
2727

28+
const jobStatus =
29+
(typeof Object.fromEntries === 'function' &&
30+
Object.fromEntries(
31+
Object.keys(this.jobStatus).map(job => [
32+
job,
33+
{
34+
...this.jobStatus[job]!,
35+
config: this.agenda.definitions[job]
36+
}
37+
])
38+
)) ||
39+
undefined;
40+
41+
if (typeof Object.fromEntries !== 'function') {
42+
console.warn('job status not available due to too old node version');
43+
}
44+
2845
return {
2946
version,
3047
queueName: this.agenda.attrs.name,
@@ -34,15 +51,7 @@ export class JobProcessor {
3451
maxConcurrency: this.maxConcurrency,
3552
processEvery: this.processEvery
3653
},
37-
jobStatus: Object.fromEntries(
38-
Object.keys(this.jobStatus).map(job => [
39-
job,
40-
{
41-
...this.jobStatus[job]!,
42-
config: this.agenda.definitions[job]
43-
}
44-
])
45-
),
54+
jobStatus,
4655
queuedJobs: this.jobQueue.length,
4756
runningJobs: !fullDetails ? this.runningJobs.length : this.runningJobs,
4857
lockedJobs: !fullDetails ? this.lockedJobs.length : this.lockedJobs,
@@ -384,17 +393,10 @@ export class JobProcessor {
384393
this.jobProcessing();
385394
}, runIn);
386395
}
387-
// console.log('this.localQueueProcessing', this.localQueueProcessing);
388396
if (this.localQueueProcessing < this.maxConcurrency && jobEnqueued) {
389397
// additionally run again and check if there are more jobs that we can process right now (as long concurrency not reached)
390398
setImmediate(() => this.jobProcessing());
391-
} /* else {
392-
console.log(
393-
'NOT CALLING JOB PROCESSING AGAIN DUE TO',
394-
this.localQueueProcessing,
395-
this.maxConcurrency
396-
);
397-
} */
399+
}
398400
} finally {
399401
this.localQueueProcessing -= 1;
400402
}
@@ -424,9 +426,9 @@ export class JobProcessor {
424426
(!jobDefinition.concurrency || !status || status.running < jobDefinition.concurrency) &&
425427
this.runningJobs.length < this.maxConcurrency
426428
) {
427-
// NOTE: Shouldn't we update the 'lockedAt' value in MongoDB so it can be picked up on restart?
428-
// -> not needed, as the timeout has been reached, and it will be picked up anyways again
429429
if (job.isDead()) {
430+
// not needed to update lockedAt in databsase, as the timeout has been reached,
431+
// and it will be picked up anyways again
430432
log.extend('runOrRetry')(
431433
'[%s:%s] job lock has expired, freeing it up',
432434
job.attrs.name,
@@ -445,8 +447,7 @@ export class JobProcessor {
445447

446448
this.lockedJobs.splice(lockedJobIndex, 1);
447449
this.updateStatus(job.attrs.name, 'locked', -1);
448-
this.jobProcessing();
449-
return false;
450+
return true;
450451
}
451452

452453
const runJob = async () => {

src/types/AgendaStatus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface IAgendaStatus {
99
maxConcurrency: number;
1010
processEvery: string | number;
1111
};
12-
jobStatus: { [name: string]: { running: number; locked: number } | undefined };
12+
jobStatus?: { [name: string]: { running: number; locked: number } };
1313
queuedJobs: number;
1414
runningJobs: number | Job[];
1515
lockedJobs: number | Job[];

test/jobprocessor.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ describe('JobProcessor', () => {
160160

161161
await agenda.start();
162162

163+
let runningJobs = 0;
163164
const allJobsStarted = new Promise(async resolve => {
164-
let runningJobs = 0;
165165
do {
166166
runningJobs = (await agenda.getRunningStats()).runningJobs as number;
167167
await new Promise(wait => setTimeout(wait, 50));
@@ -170,7 +170,12 @@ describe('JobProcessor', () => {
170170
});
171171

172172
expect(
173-
await Promise.race([allJobsStarted, new Promise(resolve => setTimeout(resolve, 1500))])
173+
await Promise.race([
174+
allJobsStarted,
175+
new Promise(resolve =>
176+
setTimeout(() => resolve(`not all jobs started, currently running: ${runningJobs}`), 1500)
177+
)
178+
])
174179
).to.equal('all started');
175180
});
176181
});

0 commit comments

Comments
 (0)