UNPKG

6.14 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { EventEmitter } from "events";
4
5/** The callback executed by a Job */
6// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
7export type JobCallback = (fireDate: Date) => void | Promise<any>;
8
9/** The Spec that is used as parms for schedule to deside when it needs to be ran */
10export type Spec = RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string | number;
11/** Scheduler jobs. */
12export class Job extends EventEmitter {
13 readonly name: string;
14
15 /**
16 * Use the function scheduleJob() to create new Job objects.
17 *
18 * @internal
19 * @param name either an optional name for this Job or this Job's callback
20 * @param job either this Job's callback or an optional callback function
21 * @param callback optional callback that is executed right before the JobCallback
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
86export 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
93export type Recurrence = number | Range | string;
94export type RecurrenceSegment = Recurrence | Recurrence[];
95export type Timezone = string;
96
97export class RecurrenceRule {
98 /**
99 * Day of the month.
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 */
128export interface RecurrenceSpecDateRange {
129 /**
130 * Starting date in date range.
131 */
132 start?: Date | string | number | undefined;
133 /**
134 * Ending date in date range.
135 */
136 end?: Date | string | number | undefined;
137 /**
138 * Cron expression string.
139 */
140 rule: string;
141 /**
142 * Timezone
143 */
144 tz?: Timezone | undefined;
145}
146
147/**
148 * Recurrence rule specification using object literal syntax.
149 */
150export interface RecurrenceSpecObjLit {
151 /**
152 * Day of the month.
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 * Timezone
163 */
164 tz?: Timezone | undefined;
165}
166
167export 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 */
182export 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 */
194export 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 */
205export function rescheduleJob(
206 job: Job | string,
207 spec: RecurrenceRule | RecurrenceSpecDateRange | RecurrenceSpecObjLit | Date | string,
208): Job;
209
210/** Dictionary of all Jobs, accessible by name. */
211export let scheduledJobs: { [jobName: string]: Job };
212
213/**
214 * Cancels the job.
215 *
216 * @returns Whether the job has been cancelled with success.
217 */
218export function cancelJob(job: Job | string): boolean;
219
220/**
221 * Gracefullly cancels all jobs.
222 *
223 * @returns Promise that resolves when all running jobs have stopped.
224 */
225export function gracefulShutdown(): Promise<void>;