Skip to content

Commit

Permalink
Merge pull request #1100 from origranot/add-disbaled-flag
Browse files Browse the repository at this point in the history
feat(cron): add disabled flag to cron jobs
  • Loading branch information
kamilmysliwiec committed Feb 3, 2023
2 parents 2c08db2 + 27f21bc commit d4aed4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/decorators/cron.decorator.ts
Expand Up @@ -29,6 +29,12 @@ export interface CronOptions {
* If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
*/
unrefTimeout?: boolean;

/**
* This flag indicates whether the job will be executed at all.
* @default false
*/
disabled?: boolean;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/scheduler.orchestrator.ts
Expand Up @@ -67,6 +67,11 @@ export class SchedulerOrchestrator
const cronKeys = Object.keys(this.cronJobs);
cronKeys.forEach((key) => {
const { options, target } = this.cronJobs[key];

if (options.disabled) {
return;
}

const cronJob = new CronJob(
options.cronTime,
target as any,
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/cron-jobs.spec.ts
Expand Up @@ -94,6 +94,17 @@ describe('Cron', () => {
expect(job.running).toBeFalsy();
});

it(`should not run "cron" at all`, async () => {
const service = app.get(CronService);

await app.init();
const registry = app.get(SchedulerRegistry);

expect(() => {
registry.getCronJob('DISABLED');
}).toThrow();
});

it(`should return cron id by name`, async () => {
await app.init();
const registry = app.get(SchedulerRegistry);
Expand Down
6 changes: 6 additions & 0 deletions tests/src/cron.service.ts
Expand Up @@ -57,6 +57,12 @@ export class CronService {
}
}

@Cron(CronExpression.EVERY_30_SECONDS, {
name: 'DISABLED',
disabled: true,
})
handleDisabledCron() {}

addCronJob(): CronJob {
const job = new CronJob(CronExpression.EVERY_SECOND, () => {
++this.dynamicCallsCount;
Expand Down

0 comments on commit d4aed4d

Please sign in to comment.