Skip to content

Commit 141aa00

Browse files
authored
fix: fix lastDate() value for intervals > 25 days (#711)
1 parent 6536084 commit 141aa00

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/job.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
241241
// again. This processing might make us miss the deadline by a few ms
242242
// times the number of sleep sessions. Given a MAXDELAY of almost a
243243
// month, this should be no issue.
244-
this.lastExecution = new Date();
245244
if (remaining) {
246245
if (remaining > MAXDELAY) {
247246
remaining -= MAXDELAY;
@@ -254,6 +253,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
254253
setCronTimeout(timeout);
255254
} else {
256255
// We have arrived at the correct point in time.
256+
this.lastExecution = new Date();
257257

258258
this.running = false;
259259

tests/cron.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ describe('cron', () => {
11371137
clock.restore();
11381138
});
11391139

1140-
it('should give the last execution date', () => {
1140+
it('should give the correct last execution date', () => {
11411141
const callback = jest.fn();
11421142
const clock = sinon.useFakeTimers();
11431143
const job = new CronJob('* * * * * *', callback);
@@ -1149,6 +1149,23 @@ describe('cron', () => {
11491149
clock.restore();
11501150
});
11511151

1152+
it('should give the correct last execution date for intervals greater than 25 days (#710)', () => {
1153+
const callback = jest.fn();
1154+
const clock = sinon.useFakeTimers();
1155+
1156+
const job = new CronJob('0 0 0 1 * *', callback); // At 00:00 on day-of-month 1.
1157+
job.start();
1158+
1159+
// tick one tick before nextDate()
1160+
clock.tick(job.nextDate().toMillis() - 1);
1161+
1162+
expect(callback).toHaveBeenCalledTimes(0);
1163+
expect(job.lastDate()?.getTime()).toBeUndefined();
1164+
1165+
job.stop();
1166+
clock.restore();
1167+
});
1168+
11521169
it('should throw when providing both exclusive parameters timeZone and utcOffset', () => {
11531170
expect(() => {
11541171
// @ts-expect-error testing runtime exception

0 commit comments

Comments
 (0)