UNPKG

8.84 kBTypeScriptView Raw
1// Type definitions for cron 1.7
2// Project: https://www.npmjs.com/package/cron
3// Definitions by: Hiroki Horiuchi <https://github.com/horiuchi>
4// Lundarl Gholoi <https://github.com/winup>
5// koooge <https://github.com/koooge>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/// <reference types="node" />
9
10import { Moment } from 'moment';
11import { SpawnOptions } from "child_process";
12
13export declare type CronCommand = (() => void) | string | { command: string, args?: ReadonlyArray<string> | undefined, options?: SpawnOptions | undefined};
14
15export declare class CronTime {
16 /**
17 * Create a new ```CronTime```.
18 * @param source The time to fire off your job. This can be in the form of cron syntax or a JS ```Date``` object.
19 * @param zone Timezone name. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/).
20 * @param utcOffset UTC offset. Don't use both ```zone``` and ```utcOffset``` together or weird things may happen.
21 */
22 constructor(source: string | Date | Moment, zone?: string, utcOffset?: string | number);
23
24 /**
25 * Tells you when ```CronTime``` will be run.
26 */
27 public sendAt(): Moment;
28 /**
29 * Tells you when ```CronTime``` will be run.
30 * @param i Indicate which turn of run after now. If not given return next run time.
31 * @returns A `Moment` when the source passed in the constructor is a `Date` or a `Moment` and an array of `Moment` when the source is a string
32 */
33 public sendAt(i?: number): Moment | Moment[];
34 /**
35 * Get the number of milliseconds in the future at which to fire our callbacks.
36 */
37 public getTimeout(): number;
38}
39
40export declare interface CronJobParameters {
41 /**
42 * The time to fire off your job. This can be in the form of cron syntax or a JS ```Date``` object.
43 */
44 cronTime: string | Date | Moment;
45 /**
46 * The function to fire at the specified time. If an ```onComplete``` callback was provided, ```onTick``` will receive it as an argument. ```onTick``` may call ```onComplete``` when it has finished its work.
47 */
48 onTick: CronCommand;
49 /**
50 * A function that will fire when the job is stopped with ```job.stop()```, and may also be called by ```onTick``` at the end of each run.
51 */
52 onComplete?: CronCommand | null | undefined;
53 /**
54 * Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call ```job.start()``` in order to start the job (assuming ```job``` is the variable you set the cronjob to). This does not immediately fire your ```onTick``` function, it just gives you more control over the behavior of your jobs.
55 */
56 start?: boolean | undefined;
57 /**
58 * 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.
59 */
60 timeZone?: string | undefined;
61 /**
62 * The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call ```this.stop()```. However, if you change this you'll have access to the functions and values within your context object.
63 */
64 context?: any;
65 /**
66 * This will immediately fire your ```onTick``` function as soon as the requisit initialization has happened. This option is set to ```false``` by default for backwards compatibility.
67 */
68 runOnInit?: boolean | undefined;
69 /**
70 * 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.
71 */
72 utcOffset?: string | number | undefined;
73 /**
74 * 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.
75 */
76 unrefTimeout?: boolean | undefined;
77}
78
79export declare class CronJob {
80 /**
81 * Return ```true``` if job is running.
82 */
83 public running: boolean | undefined;
84 /**
85 * Function using to fire ```onTick```, default set to an inner private function. Overwrite this only if you have a really good reason to do so.
86 */
87 public fireOnTick: Function;
88
89 /**
90 * Create a new ```CronJob```.
91 * @param cronTime The time to fire off your job. This can be in the form of cron syntax or a JS ```Date``` object.
92 * @param onTick The function to fire at the specified time.
93 * @param onComplete A function that will fire when the job is complete, when it is stopped.
94 * @param start Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call ```job.start()``` in order to start the job (assuming ```job``` is the variable you set the cronjob to). This does not immediately fire your onTick function, it just gives you more control over the behavior of your jobs.
95 * @param timeZone 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/).
96 * @param context The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call ```this.stop()```. However, if you change this you'll have access to the functions and values within your context object.
97 * @param runOnInit This will immediately fire your ```onTick``` function as soon as the requisit initialization has happened. This option is set to ```false``` by default for backwards compatibility.
98 * @param utcOffset 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.
99 * @param unrefTimeout 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.
100 */
101 constructor(cronTime: string | Date | Moment, onTick: CronCommand, onComplete?: CronCommand | null, start?: boolean, timeZone?: string, context?: any, runOnInit?: boolean, utcOffset?: string | number, unrefTimeout?: boolean);
102 /**
103 * Create a new ```CronJob```.
104 * @param options Job parameters.
105 */
106 constructor(options: CronJobParameters);
107
108 /**
109 * Runs your job.
110 */
111 public start(): void;
112 /**
113 * Stops your job.
114 */
115 public stop(): void;
116 /**
117 * Change the time for the ```CronJob```.
118 * @param time Target time.
119 */
120 public setTime(time: CronTime): void;
121 /**
122 * Tells you the last execution date.
123 */
124 public lastDate(): Date;
125 /**
126 * Tells you when a ```CronTime``` will be run.
127 */
128 public nextDate(): Moment;
129 public nextDates(): Moment;
130 /**
131 * Tells you when a ```CronTime``` will be run.
132 * @param i Indicate which turn of run after now. If not given return next run time.
133 * @returns A `Moment` when the cronTime passed in the constructor is a `Date` or a `Moment` and an array of `Moment` when the cronTime is a string
134 */
135 public nextDates(i?: number): Moment | Moment[];
136 /**
137 * Add another ```onTick``` function.
138 * @param callback Target function.
139 */
140 public addCallback(callback: Function): void;
141}
142
143export declare function job(options: CronJobParameters): CronJob;
144export declare function job(cronTime: string | Date | Moment, onTick: () => void, onComplete?: CronCommand | null, start?: boolean, timeZone?: string, context?: any, runOnInit?: boolean, utcOffset?: string | number, unrefTimeout?: boolean): CronJob;
145export declare function time(source: string | Date | Moment, zone?: string): CronTime;
146export declare function sendAt(cronTime: string | Date | Moment): Moment;
147export declare function timeout(cronTime: string | Date | Moment): number;
148
\No newline at end of file