Skip to content

Commit

Permalink
Merge pull request #1461 from sheerlox/renovate/cron-3.x
Browse files Browse the repository at this point in the history
fix(deps)!: update dependency cron to v3
  • Loading branch information
kamilmysliwiec committed Oct 23, 2023
2 parents a213b49 + 6a9de20 commit 62bb95b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 47 deletions.
29 changes: 20 additions & 9 deletions lib/decorators/cron.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import { CronJobParams } from 'cron';
import { SchedulerType } from '../enums/scheduler-type.enum';
import {
SCHEDULE_CRON_OPTIONS,
SCHEDULER_NAME,
SCHEDULER_TYPE,
SCHEDULE_CRON_OPTIONS,
} from '../schedule.constants';

/**
* @ref https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cron/index.d.ts
*/
export interface CronOptions {
export type CronOptions = {
/**
* Specify the name of your cron job. This will allow to inject your cron job reference through `@InjectCronRef`.
*/
Expand All @@ -18,12 +19,11 @@ export interface CronOptions {
/**
* Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both ```timeZone``` and ```utcOffset``` together or weird things may happen.
*/
timeZone?: string;

timeZone?: unknown;
/**
* This allows you to specify the offset of your timezone rather than using the ```timeZone``` param. Probably don't use both ```timeZone``` and ```utcOffset``` together or weird things may happen.
*/
utcOffset?: string | number;
utcOffset?: unknown;

/**
* 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.
Expand All @@ -35,18 +35,29 @@ export interface CronOptions {
* @default false
*/
disabled?: boolean;
}
} &
(
// make timeZone & utcOffset mutually exclusive
| {
timeZone?: string;
utcOffset?: never;
}
| {
timeZone?: never;
utcOffset?: number;
}
);

/**
* Creates a scheduled job.
* @param cronTime The time to fire off your job. This can be in the form of cron syntax or a JS ```Date``` object.
* @param cronTime The time to fire off your job. This can be in the form of cron syntax, a JS ```Date``` object or a Luxon ```DateTime``` object.
* @param options Job execution options.
*/
export function Cron(
cronTime: string | Date,
cronTime: CronJobParams['cronTime'],
options: CronOptions = {},
): MethodDecorator {
const name = options && options.name;
const name = options?.name;
return applyDecorators(
SetMetadata(SCHEDULE_CRON_OPTIONS, {
...options,
Expand Down
28 changes: 8 additions & 20 deletions lib/scheduler.orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
OnApplicationBootstrap,
OnApplicationShutdown,
} from '@nestjs/common';
import { CronJob } from 'cron';
import { CronCallback, CronJob, CronJobParams } from 'cron';
import { v4 } from 'uuid';
import { CronOptions } from './decorators/cron.decorator';
import { SchedulerRegistry } from './scheduler.registry';
Expand All @@ -13,7 +13,7 @@ type TimeoutHost = { timeout: number };
type RefHost<T> = { ref?: T };

type CronOptionsHost = {
options: CronOptions & Record<'cronTime', string | Date | any>;
options: CronOptions & Record<'cronTime', CronJobParams['cronTime']>;
};

type IntervalOptions = TargetHost & TimeoutHost & RefHost<number>;
Expand Down Expand Up @@ -67,23 +67,11 @@ export class SchedulerOrchestrator
const cronKeys = Object.keys(this.cronJobs);
cronKeys.forEach((key) => {
const { options, target } = this.cronJobs[key];
const cronJob = new CronJob(
options.cronTime,
target as any,
undefined,
false,
options.timeZone,
undefined,
false,
options.utcOffset,
options.unrefTimeout,
);

if (options.disabled) {
cronJob.stop();
} else {
cronJob.start();
}
const cronJob = CronJob.from({
...options,
onTick: target as CronCallback<null, false>,
start: !options.disabled
});

this.cronJobs[key].ref = cronJob;
this.schedulerRegistry.addCronJob(key, cronJob);
Expand Down Expand Up @@ -124,7 +112,7 @@ export class SchedulerOrchestrator

addCron(
methodRef: Function,
options: CronOptions & Record<'cronTime', string | Date | any>,
options: CronOptions & Record<'cronTime', CronJobParams['cronTime']>,
) {
const name = options.name || v4();
this.cronJobs[name] = {
Expand Down
2 changes: 1 addition & 1 deletion lib/scheduler.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class SchedulerRegistry {
this.timeouts.delete(name);
}

private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object): Function {
private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object): (...args: unknown[]) => Promise<void> {
return async (...args: unknown[]) => {
try {
await methodRef.call(instance, ...args);
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"release": "release-it"
},
"dependencies": {
"cron": "2.4.3",
"cron": "3.1.3",
"uuid": "9.0.1"
},
"devDependencies": {
Expand Down
16 changes: 8 additions & 8 deletions tests/e2e/cron-jobs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { INestApplication, Logger } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { CronJob } from "cron";
import sinon from 'sinon';
import { CronExpression } from "../../lib";
import { SchedulerRegistry } from '../../lib/scheduler.registry';
import { AppModule } from '../src/app.module';
import { CronService } from '../src/cron.service';
import { nullPrototypeObjectProvider } from '../src/null-prototype-object.provider';
import { CronJob } from "cron";
import { CronExpression } from "../../lib";

const deleteAllRegisteredJobsExceptOne = (
registry: SchedulerRegistry,
Expand Down Expand Up @@ -62,15 +62,15 @@ describe('Cron', () => {
const job = registry.getCronJob('EXECUTES_EVERY_30_SECONDS');
deleteAllRegisteredJobsExceptOne(registry, 'EXECUTES_EVERY_30_SECONDS');

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

clock.tick('30');
expect(service.callsCount).toEqual(1);
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:00:30.000Z'));

clock.tick('31');
expect(job.running).toBeFalsy();
expect(job.running).toBe(false);
});

it(`should run "cron" 3 times every 60 seconds`, async () => {
Expand All @@ -88,7 +88,7 @@ describe('Cron', () => {
expect(job.lastDate()).toEqual(new Date('2020-01-01T00:03:00.000Z'));

clock.tick('03:01');
expect(job.running).toBeFalsy();
expect(job.running).toBe(false);
});

it(`should run "cron" 3 times every hour`, async () => {
Expand All @@ -106,7 +106,7 @@ describe('Cron', () => {
expect(job.lastDate()).toEqual(new Date('2020-01-01T03:00:00.000Z'));

clock.tick('03:00:01');
expect(job.running).toBeFalsy();
expect(job.running).toBe(false);
});

it(`should not run "cron" at all`, async () => {
Expand Down Expand Up @@ -143,10 +143,10 @@ describe('Cron', () => {

const job = registry.getCronJob('dynamic');
expect(job).toBeDefined();
expect(job.running).toBeUndefined();
expect(job.running).toBe(false);

job.start();
expect(job.running).toEqual(true);
expect(job.running).toBe(true);

clock.tick(3000);
expect(service.dynamicCallsCount).toEqual(3);
Expand Down

0 comments on commit 62bb95b

Please sign in to comment.