UNPKG

6.46 kBTypeScriptView Raw
1// Type definitions for node-schedule 2.1
2// Project: https://github.com/node-schedule/node-schedule
3// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>
4// Florian Plattner <https://github.com/flowpl>
5// Tieu Philippe Khim <https://github.com/spike008t>
6// Seohyun Yoon <https://github.com/seohyun0120>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9/// <reference types="node" />
10
11import { EventEmitter } from 'events';
12
13/** The callback executed by a Job */
14export type JobCallback = (fireDate: Date) => void | Promise<any>;
15
16/** Scheduler jobs. */
17export class Job extends EventEmitter {
18 readonly name: string;
19
20 /**
21 * Use the function scheduleJob() to create new Job objects.
22 *
23 * @internal
24 * @param name either an optional name for this Job or this Job's callback
25 * @param job either this Job's callback or an optional callback function
26 * @param callback optional callback that is executed right before the JobCallback
27 */
28 constructor(name: string|JobCallback, job?: JobCallback|(() => void), callback?: () => void);
29
30 /**
31 * Adds an Invocation to this job. For internal use.
32 * @internal
33 * @return whether the invocation could be added
34 */
35 trackInvocation(invocation: Invocation): boolean;
36
37 /**
38 * removes an Invocation from this Job's tracking list. For internal use.
39 * @internal
40 * @return boolean whether the invocation was successful. Removing an Invocation that doesn't exist, returns false.
41 */
42 stopTrackingInvocation(invocation: Invocation): boolean;
43
44 /**
45 * @internal
46 * @return the number of currently running instances of this Job.
47 */
48 triggeredJobs(): number;
49
50 /**
51 * set the number of currently running Jobs.
52 * @internal
53 */
54 setTriggeredJobs(triggeredJobs: number): void;
55
56 /**
57 * cancel all pending Invocations of this Job.
58 * @param reschedule whether to reschedule the canceled Invocations.
59 */
60 cancel(reschedule?: boolean): boolean;
61
62 /**
63 * cancel the next Invocation of this Job.
64 * @param reschedule whether to reschedule the canceled Invocation.
65 * @return whether cancelation was successful
66 */
67 cancelNext(reschedule?: boolean): boolean;
68
69 /**
70 * Changes the scheduling information for this Job.
71 * @return whether the reschedule was successful
72 */
73 reschedule(spec: RecurrenceRule|string|number): boolean;
74
75 /** The Date on which this Job will be run next. */
76 nextInvocation(): Date;
77
78 /** A list of all pending Invocations */
79 pendingInvocations(): Invocation[];
80
81 /**
82 * run this Job immediately.
83 */
84 invoke(): void;
85
86 /** schedule this Job to be run on the specified date. */
87 runOnDate(date: Date): void;
88
89 /** set scheduling information */
90 schedule(date: Date|string|number): boolean;
91}
92
93export class Range {
94 constructor(start?: number, end?: number, step?: number);
95
96 /** Whether the class contains the specified value. */
97 contains(value: number): boolean;
98}
99
100export type Recurrence = number | Range | string;
101export type RecurrenceSegment = Recurrence | Recurrence[];
102export type Timezone = string;
103
104export class RecurrenceRule {
105 /**
106 * Day of the month.
107 */
108 date: RecurrenceSegment;
109 dayOfWeek: RecurrenceSegment;
110 hour: RecurrenceSegment;
111 minute: RecurrenceSegment;
112 month: RecurrenceSegment;
113 second: RecurrenceSegment;
114 year: RecurrenceSegment;
115 tz: Timezone;
116
117 constructor(
118 year?: RecurrenceSegment,
119 month?: RecurrenceSegment,
120 date?: RecurrenceSegment,
121 dayOfWeek?: RecurrenceSegment,
122 hour?: RecurrenceSegment,
123 minute?: RecurrenceSegment,
124 second?: RecurrenceSegment,
125 tz?: Timezone,
126 );
127
128 nextInvocationDate(base: Date): Date;
129}
130
131/**
132 * Recurrence rule specification using a date range and cron expression.
133 */
134export interface RecurrenceSpecDateRange {
135 /**
136 * Starting date in date range.
137 */
138 start?: Date | string | number | undefined;
139 /**
140 * Ending date in date range.
141 */
142 end?: Date | string | number | undefined;
143 /**
144 * Cron expression string.
145 */
146 rule: string;
147 /**
148 * Timezone
149 */
150 tz?: Timezone | undefined;
151}
152
153/**
154 * Recurrence rule specification using object literal syntax.
155 */
156export interface RecurrenceSpecObjLit {
157 /**
158 * Day of the month.
159 */
160 date?: RecurrenceSegment | undefined;
161 dayOfWeek?: RecurrenceSegment | undefined;
162 hour?: RecurrenceSegment | undefined;
163 minute?: RecurrenceSegment | undefined;
164 month?: RecurrenceSegment | undefined;
165 second?: RecurrenceSegment | undefined;
166 year?: RecurrenceSegment | undefined;
167 /**
168 * Timezone
169 */
170 tz?: Timezone | undefined;
171}
172
173export class Invocation {
174 fireDate: Date;
175 job: Job;
176 recurrenceRule: RecurrenceRule;
177 timerID: number;
178 constructor(job: Job, fireDate: Date, recurrenceRule: RecurrenceRule);
179}
180
181/**
182 * Create a schedule job.
183 *
184 * @param name name for the new Job
185 * @param rule scheduling info
186 * @param callback callback to be executed on each invocation
187 */
188export function scheduleJob(name: string, rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number, callback: JobCallback): Job;
189
190/**
191 * Create a schedule job.
192 *
193 * @param rule scheduling info
194 * @param callback callback to be executed on each invocation
195 */
196export function scheduleJob(rule: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number, callback: JobCallback): Job;
197
198/**
199 * Changes the timing of a Job, canceling all pending invocations.
200 *
201 * @param spec The new timing for this Job.
202 * @return if the job could be rescheduled, {null} otherwise.
203 */
204export function rescheduleJob(job: Job | string, spec: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string): Job;
205
206/** Dictionary of all Jobs, accessible by name. */
207export let scheduledJobs: {[jobName: string]: Job};
208
209/**
210 * Cancels the job.
211 *
212 * @returns Whether the job has been cancelled with success.
213 */
214export function cancelJob(job: Job|string): boolean;
215
216/**
217 * Gracefullly cancels all jobs.
218 *
219 * @returns Promise that resolves when all running jobs have stopped.
220 */
221 export function gracefulShutdown(): Promise<void>;