Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: add waitForCompletion for cronjobs to disable concurrent running cronjobs #1870

Merged
merged 3 commits into from
Jan 23, 2025
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
6 changes: 6 additions & 0 deletions lib/decorators/cron.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export type CronOptions = {
*/
unrefTimeout?: boolean;

/**
* If true, no additional instances of cronjob will run until the current onTick callback has completed.
* Any new scheduled executions that occur while the current cronjob is running will be skipped entirely.
*/
waitForCompletion?: boolean;

/**
* This flag indicates whether the job will be executed at all.
* @default false
Expand Down
74 changes: 74 additions & 0 deletions tests/e2e/cron-jobs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,80 @@ describe('Cron', () => {
expect(job.running).toBe(false);
});

it(`should wait for "cron" to complete`, async () => {
// run every minute for 61 seconds
// 00:01:00 - 00:02:01
// 00:02:00 - skipped
// 00:03:00 - 00:04:01
// 00:04:00 - skipped
// 00:05:00 - 00:06:01

const service = app.get(CronService);

await app.init();
const registry = app.get(SchedulerRegistry);
const job = registry.getCronJob('WAIT_FOR_COMPLETION');
deleteAllRegisteredJobsExceptOne(registry, 'WAIT_FOR_COMPLETION');

expect(job.running).toBe(true);
expect(service.callsCount).toEqual(0);

await clock.tickAsync('01:00');
// 00:01:00
expect(service.callsCount).toEqual(1);
expect(service.callsFinishedCount).toEqual(0);
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:01:00.000Z'));

await clock.tickAsync('00:01');
// 00:01:01
expect(service.callsCount).toEqual(1);
expect(service.callsFinishedCount).toEqual(0);

await clock.tickAsync('00:59');
// 00:02:00
expect(service.callsCount).toEqual(1);
expect(service.callsFinishedCount).toEqual(0);

await clock.tickAsync('00:01');
// 00:02:01
expect(service.callsCount).toEqual(1);
expect(service.callsFinishedCount).toEqual(1);
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:02:00.000Z'));

await clock.tickAsync('00:59');
// 00:03:00
expect(service.callsCount).toEqual(2);
expect(service.callsFinishedCount).toEqual(1);

await clock.tickAsync('00:01');
// 00:03:01
expect(service.callsCount).toEqual(2);
expect(service.callsFinishedCount).toEqual(1);
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:03:00.000Z'));

await clock.tickAsync('00:59');
// 00:04:00
expect(service.callsCount).toEqual(2);
expect(service.callsFinishedCount).toEqual(1);

await clock.tickAsync('00:01');
// 00:04:01
expect(service.callsCount).toEqual(2);
expect(service.callsFinishedCount).toEqual(2);
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:04:00.000Z'));

await clock.tickAsync('00:59');
// 00:05:00
expect(service.callsCount).toEqual(3);
expect(service.callsFinishedCount).toEqual(2);

await clock.tickAsync('01:01');
// 00:06:01
expect(service.callsCount).toEqual(3);
expect(service.callsFinishedCount).toEqual(3);
expect(job.running).toBe(false);
});

it(`should run "cron" 3 times every 60 seconds`, async () => {
const service = app.get(CronService);

Expand Down
16 changes: 16 additions & 0 deletions tests/src/cron.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CronJob } from 'cron';
@Injectable()
export class CronService {
callsCount = 0;
callsFinishedCount = 0;
dynamicCallsCount = 0;

constructor(private readonly schedulerRegistry: SchedulerRegistry) {}
Expand Down Expand Up @@ -75,6 +76,21 @@ export class CronService {
return job;
}

@Cron(CronExpression.EVERY_MINUTE, {
name: 'WAIT_FOR_COMPLETION',
waitForCompletion: true,
})
async handleLongRunningCron() {
++this.callsCount;
await new Promise((r) => setTimeout(r, 61 * 1000));
++this.callsFinishedCount;

if (this.callsCount > 2) {
const ref = this.schedulerRegistry.getCronJob('WAIT_FOR_COMPLETION');
ref!.stop();
}
}

doesExist(name: string): boolean {
return this.schedulerRegistry.doesExist('cron', name);
}
Expand Down