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