From e39cfd089248a7a235cf69888c9714a99988a75f Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 5 Dec 2021 17:43:06 +0100 Subject: [PATCH] fix: nextRunAt value can be null --- src/Job.ts | 2 +- src/JobProcessingQueue.ts | 7 +------ src/JobProcessor.ts | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Job.ts b/src/Job.ts index a30cd52..c4e3d35 100644 --- a/src/Job.ts +++ b/src/Job.ts @@ -62,7 +62,7 @@ export class Job { ...args, // Set defaults if undefined priority: parsePriority(args.priority), - nextRunAt: args.nextRunAt || new Date(), + nextRunAt: args.nextRunAt === undefined ? new Date() : args.nextRunAt, type: args.type }; } diff --git a/src/JobProcessingQueue.ts b/src/JobProcessingQueue.ts index edef62f..893994b 100644 --- a/src/JobProcessingQueue.ts +++ b/src/JobProcessingQueue.ts @@ -102,7 +102,7 @@ export class JobProcessingQueue { | undefined; }, handledJobs: IJobParameters['_id'][] - ): (JobWithId & { attrs: IJobParameters & { nextRunAt: Date } }) | undefined { + ): (JobWithId & { attrs: IJobParameters & { nextRunAt?: Date | null } }) | undefined { const next = (Object.keys(this._queue) as unknown as number[]).reverse().find(i => { const def = this.agenda.definitions[this._queue[i].attrs.name]; const status = jobStatus[this._queue[i].attrs.name]; @@ -116,11 +116,6 @@ export class JobProcessingQueue { !handledJobs.includes(this._queue[i].attrs._id) && (!status || !def.concurrency || status.running < def.concurrency) ) { - if (!this._queue[i].attrs.nextRunAt) { - // eslint-disable-next-line no-console - console.log('this._queue[i]', this._queue[i].attrs); - throw new Error('no nextRunAt date'); - } return true; } return false; diff --git a/src/JobProcessor.ts b/src/JobProcessor.ts index 6b213c6..4d7e37c 100644 --- a/src/JobProcessor.ts +++ b/src/JobProcessor.ts @@ -409,7 +409,7 @@ export class JobProcessor { // If the 'nextRunAt' time is older than the current time, run the job // Otherwise, setTimeout that gets called at the time of 'nextRunAt' - if (job.attrs.nextRunAt <= now) { + if (!job.attrs.nextRunAt || job.attrs.nextRunAt <= now) { log.extend('jobProcessing')( '[%s:%s] nextRunAt is in the past, run the job immediately', job.attrs.name,