Skip to content

Commit 62bb95b

Browse files
authoredOct 23, 2023
Merge pull request #1461 from sheerlox/renovate/cron-3.x
fix(deps)!: update dependency cron to v3
2 parents a213b49 + 6a9de20 commit 62bb95b

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed
 

‎lib/decorators/cron.decorator.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { applyDecorators, SetMetadata } from '@nestjs/common';
2+
import { CronJobParams } from 'cron';
23
import { SchedulerType } from '../enums/scheduler-type.enum';
34
import {
5+
SCHEDULE_CRON_OPTIONS,
46
SCHEDULER_NAME,
57
SCHEDULER_TYPE,
6-
SCHEDULE_CRON_OPTIONS,
78
} from '../schedule.constants';
89

910
/**
1011
* @ref https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cron/index.d.ts
1112
*/
12-
export interface CronOptions {
13+
export type CronOptions = {
1314
/**
1415
* Specify the name of your cron job. This will allow to inject your cron job reference through `@InjectCronRef`.
1516
*/
@@ -18,12 +19,11 @@ export interface CronOptions {
1819
/**
1920
* 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.
2021
*/
21-
timeZone?: string;
22-
22+
timeZone?: unknown;
2323
/**
2424
* 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.
2525
*/
26-
utcOffset?: string | number;
26+
utcOffset?: unknown;
2727

2828
/**
2929
* 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.
@@ -35,18 +35,29 @@ export interface CronOptions {
3535
* @default false
3636
*/
3737
disabled?: boolean;
38-
}
38+
} &
39+
(
40+
// make timeZone & utcOffset mutually exclusive
41+
| {
42+
timeZone?: string;
43+
utcOffset?: never;
44+
}
45+
| {
46+
timeZone?: never;
47+
utcOffset?: number;
48+
}
49+
);
3950

4051
/**
4152
* Creates a scheduled job.
42-
* @param cronTime The time to fire off your job. This can be in the form of cron syntax or a JS ```Date``` object.
53+
* @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.
4354
* @param options Job execution options.
4455
*/
4556
export function Cron(
46-
cronTime: string | Date,
57+
cronTime: CronJobParams['cronTime'],
4758
options: CronOptions = {},
4859
): MethodDecorator {
49-
const name = options && options.name;
60+
const name = options?.name;
5061
return applyDecorators(
5162
SetMetadata(SCHEDULE_CRON_OPTIONS, {
5263
...options,

‎lib/scheduler.orchestrator.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
OnApplicationBootstrap,
44
OnApplicationShutdown,
55
} from '@nestjs/common';
6-
import { CronJob } from 'cron';
6+
import { CronCallback, CronJob, CronJobParams } from 'cron';
77
import { v4 } from 'uuid';
88
import { CronOptions } from './decorators/cron.decorator';
99
import { SchedulerRegistry } from './scheduler.registry';
@@ -13,7 +13,7 @@ type TimeoutHost = { timeout: number };
1313
type RefHost<T> = { ref?: T };
1414

1515
type CronOptionsHost = {
16-
options: CronOptions & Record<'cronTime', string | Date | any>;
16+
options: CronOptions & Record<'cronTime', CronJobParams['cronTime']>;
1717
};
1818

1919
type IntervalOptions = TargetHost & TimeoutHost & RefHost<number>;
@@ -67,23 +67,11 @@ export class SchedulerOrchestrator
6767
const cronKeys = Object.keys(this.cronJobs);
6868
cronKeys.forEach((key) => {
6969
const { options, target } = this.cronJobs[key];
70-
const cronJob = new CronJob(
71-
options.cronTime,
72-
target as any,
73-
undefined,
74-
false,
75-
options.timeZone,
76-
undefined,
77-
false,
78-
options.utcOffset,
79-
options.unrefTimeout,
80-
);
81-
82-
if (options.disabled) {
83-
cronJob.stop();
84-
} else {
85-
cronJob.start();
86-
}
70+
const cronJob = CronJob.from({
71+
...options,
72+
onTick: target as CronCallback<null, false>,
73+
start: !options.disabled
74+
});
8775

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

125113
addCron(
126114
methodRef: Function,
127-
options: CronOptions & Record<'cronTime', string | Date | any>,
115+
options: CronOptions & Record<'cronTime', CronJobParams['cronTime']>,
128116
) {
129117
const name = options.name || v4();
130118
this.cronJobs[name] = {

‎lib/scheduler.registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class SchedulerRegistry {
103103
this.timeouts.delete(name);
104104
}
105105

106-
private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object): Function {
106+
private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object): (...args: unknown[]) => Promise<void> {
107107
return async (...args: unknown[]) => {
108108
try {
109109
await methodRef.call(instance, ...args);

‎package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"release": "release-it"
1919
},
2020
"dependencies": {
21-
"cron": "2.4.3",
21+
"cron": "3.1.3",
2222
"uuid": "9.0.1"
2323
},
2424
"devDependencies": {

‎tests/e2e/cron-jobs.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { INestApplication, Logger } from '@nestjs/common';
22
import { Test } from '@nestjs/testing';
3+
import { CronJob } from "cron";
34
import sinon from 'sinon';
5+
import { CronExpression } from "../../lib";
46
import { SchedulerRegistry } from '../../lib/scheduler.registry';
57
import { AppModule } from '../src/app.module';
68
import { CronService } from '../src/cron.service';
79
import { nullPrototypeObjectProvider } from '../src/null-prototype-object.provider';
8-
import { CronJob } from "cron";
9-
import { CronExpression } from "../../lib";
1010

1111
const deleteAllRegisteredJobsExceptOne = (
1212
registry: SchedulerRegistry,
@@ -62,15 +62,15 @@ describe('Cron', () => {
6262
const job = registry.getCronJob('EXECUTES_EVERY_30_SECONDS');
6363
deleteAllRegisteredJobsExceptOne(registry, 'EXECUTES_EVERY_30_SECONDS');
6464

65-
expect(job.running).toBeTruthy();
65+
expect(job.running).toBe(true);
6666
expect(service.callsCount).toEqual(0);
6767

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

7272
clock.tick('31');
73-
expect(job.running).toBeFalsy();
73+
expect(job.running).toBe(false);
7474
});
7575

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

9090
clock.tick('03:01');
91-
expect(job.running).toBeFalsy();
91+
expect(job.running).toBe(false);
9292
});
9393

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

108108
clock.tick('03:00:01');
109-
expect(job.running).toBeFalsy();
109+
expect(job.running).toBe(false);
110110
});
111111

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

144144
const job = registry.getCronJob('dynamic');
145145
expect(job).toBeDefined();
146-
expect(job.running).toBeUndefined();
146+
expect(job.running).toBe(false);
147147

148148
job.start();
149-
expect(job.running).toEqual(true);
149+
expect(job.running).toBe(true);
150150

151151
clock.tick(3000);
152152
expect(service.dynamicCallsCount).toEqual(3);

0 commit comments

Comments
 (0)
Please sign in to comment.