diff --git a/src/job.ts b/src/job.ts index 59555ca0..f051d1ac 100644 --- a/src/job.ts +++ b/src/job.ts @@ -207,7 +207,8 @@ export class CronJob { } async fireOnTick() { - if (!this.waitForCompletion && this._isCallbackRunning) return; + // skip job if previous callback is still running + if (this.waitForCompletion && this._isCallbackRunning) return; this._isCallbackRunning = true; diff --git a/tests/cron.test.ts b/tests/cron.test.ts index c11e8f69..6202521a 100644 --- a/tests/cron.test.ts +++ b/tests/cron.test.ts @@ -1231,10 +1231,12 @@ describe('cron', () => { it('should track multiple running jobs correctly', async () => { const clock = sinon.useFakeTimers(); const executionOrder: number[] = []; + let started = 0; const job = new CronJob( '* * * * * *', async () => { + started++; await new Promise(resolve => { setTimeout(() => { callback(); @@ -1256,7 +1258,8 @@ describe('cron', () => { await clock.tickAsync(3500); - expect(executionOrder).toEqual([1, 2]); + expect(started).toBe(2); + expect(executionOrder).toEqual([1]); job.stop(); });