-
Notifications
You must be signed in to change notification settings - Fork 75
/
cron.decorator.ts
52 lines (47 loc) · 2.08 KB
/
cron.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { applyDecorators, SetMetadata } from '@nestjs/common';
import { SchedulerType } from '../enums/scheduler-type.enum';
import {
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 {
/**
* Specify the name of your cron job. This will allow to inject your cron job reference through `@InjectCronRef`.
*/
name?: string;
/**
* 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;
/**
* 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;
/**
* 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;
}
/**
* 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 options Job execution options.
*/
export function Cron(
cronTime: string | Date,
options: CronOptions = {},
): MethodDecorator {
const name = options && options.name;
return applyDecorators(
SetMetadata(SCHEDULE_CRON_OPTIONS, {
...options,
cronTime,
}),
SetMetadata(SCHEDULER_NAME, name),
SetMetadata(SCHEDULER_TYPE, SchedulerType.CRON),
);
}