Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix lastDate() value for intervals > 25 days #711

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ export class CronJob<OC extends CronOnCompleteCommand<C> | 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 @@ -256,6 +255,7 @@ export class CronJob<OC extends CronOnCompleteCommand<C> | 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 @@ -1108,7 +1108,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 @@ -1120,6 +1120,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