Skip to content

Commit

Permalink
fix: fix lastDate() value for intervals > 25 days (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox authored Oct 12, 2023
1 parent 6536084 commit 141aa00
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
// again. This processing might make us miss the deadline by a few ms
// times the number of sleep sessions. Given a MAXDELAY of almost a
// month, this should be no issue.
this.lastExecution = new Date();
if (remaining) {
if (remaining > MAXDELAY) {
remaining -= MAXDELAY;
Expand All @@ -254,6 +253,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
setCronTimeout(timeout);
} else {
// We have arrived at the correct point in time.
this.lastExecution = new Date();

this.running = false;

Expand Down
19 changes: 18 additions & 1 deletion tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ describe('cron', () => {
clock.restore();
});

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

it('should give the correct last execution date for intervals greater than 25 days (#710)', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new CronJob('0 0 0 1 * *', callback); // At 00:00 on day-of-month 1.
job.start();

// tick one tick before nextDate()
clock.tick(job.nextDate().toMillis() - 1);

expect(callback).toHaveBeenCalledTimes(0);
expect(job.lastDate()?.getTime()).toBeUndefined();

job.stop();
clock.restore();
});

it('should throw when providing both exclusive parameters timeZone and utcOffset', () => {
expect(() => {
// @ts-expect-error testing runtime exception
Expand Down

0 comments on commit 141aa00

Please sign in to comment.