Skip to content
Open
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
22 changes: 0 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
);
}

// one-shot jobs (realDate) have no "next" execution — stop instead of rescheduling
if (this.runOnce) {
this._isActive = false;
return;
}

timeout = this.cronTime.getTimeout();
setCronTimeout(timeout);
}
Expand Down
4 changes: 0 additions & 4 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ export class CronTime {
}

if (this.realDate) {
if (DateTime.local() > date) {
throw new CronError('WARNING: Date in past. Will never be fired.');
}

return date;
}

Expand Down
91 changes: 91 additions & 0 deletions tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,97 @@ describe('cron', () => {
job.stop();
expect(callback).toHaveBeenCalledTimes(4);
});

describe('with past date', () => {
let warnSpy: jest.SpyInstance;

beforeEach(() => {
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
warnSpy.mockRestore();
});

it('should not throw when date is 10ms in the past', () => {
const d = new Date();
const clock = sinon.useFakeTimers(d.getTime());
const pastDate = new Date(d.getTime() - 10);

expect(() => {
const job = new CronJob(pastDate, callback, null, true);
clock.tick(1000);
job.stop();
}).not.toThrow();

expect(callback).toHaveBeenCalledTimes(1);
});

it('should execute immediately when past date is within threshold', () => {
const d = new Date();
const clock = sinon.useFakeTimers(d.getTime());
const pastDate = new Date(d.getTime() - 100);

const job = new CronJob(pastDate, callback, null, true);
clock.tick(1000);

// 100ms is within default 250ms threshold — should fire
expect(callback).toHaveBeenCalledTimes(1);
expect(job.isActive).toBe(false);

const message = warnSpy.mock.calls[0][0];
expect(message).toContain('Executing immediately');
});

it('should skip execution when past date exceeds threshold', () => {
const d = new Date();
const clock = sinon.useFakeTimers(d.getTime());
const pastDate = new Date(d.getTime() - 1000);

const job = new CronJob(pastDate, callback, null, true);
clock.tick(1000);

// 1000ms exceeds default 250ms threshold — should skip
expect(callback).toHaveBeenCalledTimes(0);
expect(job.isActive).toBe(false);

const message = warnSpy.mock.calls[0][0];
expect(message).toContain('Skipping execution');
});

it('should deactivate after handling past date (no infinite loop)', () => {
const d = new Date();
const clock = sinon.useFakeTimers(d.getTime());
const pastDate = new Date(d.getTime() - 50);

const job = new CronJob(pastDate, callback, null, true);

// tick well beyond the original date — must not reschedule
clock.tick(5000);

// should only fire once (within threshold), not repeatedly
expect(callback).toHaveBeenCalledTimes(1);
expect(job.isActive).toBe(false);
});

it('should handle past date using CronJob.from()', () => {
const d = new Date();
const clock = sinon.useFakeTimers(d.getTime());
const pastDate = new Date(d.getTime() - 100);

const job = CronJob.from({
cronTime: pastDate,
onTick: callback,
start: true
});

clock.tick(1000);

// within default threshold — fires and deactivates
expect(callback).toHaveBeenCalledTimes(1);
expect(job.isActive).toBe(false);
});
});
});

describe('with timezone', () => {
Expand Down
35 changes: 27 additions & 8 deletions tests/crontime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,14 +670,33 @@ describe('crontime', () => {
expect(actual).toEqual(expected);
});

it('should detect real date in the past', () => {
const clock = sinon.useFakeTimers();
const d = new Date();
clock.tick(1000);
const time = new CronTime(d);
expect(() => {
time.sendAt();
}).toThrow();
describe('with real date in the past', () => {
it('should not throw from sendAt()', () => {
const clock = sinon.useFakeTimers();
const d = new Date();
clock.tick(1000);
const ct = new CronTime(d);
expect(() => {
ct.sendAt();
}).not.toThrow();
});

it('should return the original date from sendAt()', () => {
const clock = sinon.useFakeTimers();
const d = new Date();
clock.tick(1000);
const ct = new CronTime(d);
const result = ct.sendAt();
expect(result.toMillis()).toEqual(d.getTime());
});

it('should return a negative value from getTimeout()', () => {
const clock = sinon.useFakeTimers();
const d = new Date();
clock.tick(1000);
const ct = new CronTime(d);
expect(ct.getTimeout()).toBeLessThan(0);
});
});

it('should throw when providing both exclusive parameters timeZone and utcOffset', () => {
Expand Down