Skip to content

Commit a7a2b83

Browse files
authored
fix: calculate nextRunAt when endDate is undefined (#1227)
for backward compatibility check that endDate and startDate are defined when calculating nextRunAt value. when upgrading from an older version of agenda those fields could be missing from the job and fail the computation of nextRunAt with 'failed to calculate nextRunAt due to an invalid repeat interval' exception this solves agenda/agenda#1224
1 parent 4988291 commit a7a2b83

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/job/compute-next-run-at.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const computeNextRunAt = function (this: Job) {
6262
}
6363

6464
// If start date is present, check if the nextDate should be larger or equal to startDate. If not set startDate as nextDate
65-
if (startDate !== null) {
65+
if (startDate) {
6666
startDate = moment
6767
.tz(moment(startDate).format("YYYY-MM-DD"), timezone!)
6868
.toDate();
@@ -83,7 +83,7 @@ export const computeNextRunAt = function (this: Job) {
8383
}
8484

8585
// If endDate is less than the nextDate, set nextDate to null to stop the job from running further
86-
if (endDate !== null) {
86+
if (endDate) {
8787
const endDateDate: Date = moment
8888
.tz(moment(endDate).format("YYYY-MM-DD"), timezone!)
8989
.toDate();

test/job.js

+9
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ describe("Job", () => {
355355
job.computeNextRunAt();
356356
expect(job.attrs.nextRunAt).to.be(undefined);
357357
});
358+
359+
it("sets the correct interval when endDate is undefined", () => {
360+
// (Issue #1224): failed to calculate nextRunAt when endDate is undefined
361+
job.repeatEvery("0 0 * * *"); // Daily at midnight
362+
job.attrs.endDate = undefined;
363+
job.computeNextRunAt();
364+
expect(job.attrs.failCount).to.be(undefined);
365+
expect(job.attrs.nextRunAt).not.to.be(undefined);
366+
});
358367
});
359368

360369
it("gives the correct nextDate when the lastRun is 1ms before the expected time", () => {

0 commit comments

Comments
 (0)