@warlock.js/scheduler 4.0.47 → 4.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/cron-parser.d.ts +98 -0
- package/cjs/cron-parser.d.ts.map +1 -0
- package/cjs/cron-parser.js +193 -0
- package/cjs/cron-parser.js.map +1 -0
- package/cjs/index.d.ts +44 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +1 -34
- package/cjs/index.js.map +1 -1
- package/cjs/job.d.ts +332 -0
- package/cjs/job.d.ts.map +1 -0
- package/cjs/job.js +611 -0
- package/cjs/job.js.map +1 -0
- package/cjs/scheduler.d.ts +182 -0
- package/cjs/scheduler.d.ts.map +1 -0
- package/cjs/scheduler.js +313 -0
- package/cjs/scheduler.js.map +1 -0
- package/cjs/types.d.ts +63 -0
- package/cjs/types.d.ts.map +1 -0
- package/cjs/utils.d.ts +3 -0
- package/cjs/utils.d.ts.map +1 -0
- package/esm/cron-parser.d.ts +98 -0
- package/esm/cron-parser.d.ts.map +1 -0
- package/esm/cron-parser.js +193 -0
- package/esm/cron-parser.js.map +1 -0
- package/esm/index.d.ts +44 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1 -5
- package/esm/index.js.map +1 -1
- package/esm/job.d.ts +332 -0
- package/esm/job.d.ts.map +1 -0
- package/esm/job.js +611 -0
- package/esm/job.js.map +1 -0
- package/esm/scheduler.d.ts +182 -0
- package/esm/scheduler.d.ts.map +1 -0
- package/esm/scheduler.js +313 -0
- package/esm/scheduler.js.map +1 -0
- package/esm/types.d.ts +63 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/utils.d.ts +3 -0
- package/esm/utils.d.ts.map +1 -0
- package/package.json +27 -40
package/cjs/job.d.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { type Dayjs } from "dayjs";
|
|
2
|
+
import type { Day, JobIntervals, JobResult, TimeType } from "./types";
|
|
3
|
+
export type JobCallback = (job: Job) => Promise<any>;
|
|
4
|
+
/**
|
|
5
|
+
* Job class represents a scheduled task with configurable timing and execution options.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const job = new Job("cleanup", async () => {
|
|
10
|
+
* await cleanupOldFiles();
|
|
11
|
+
* })
|
|
12
|
+
* .everyDay()
|
|
13
|
+
* .at("03:00")
|
|
14
|
+
* .inTimezone("America/New_York")
|
|
15
|
+
* .preventOverlap()
|
|
16
|
+
* .retry(3, 1000);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare class Job {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
private readonly _callback;
|
|
22
|
+
/**
|
|
23
|
+
* Interval configuration for scheduling
|
|
24
|
+
*/
|
|
25
|
+
private _intervals;
|
|
26
|
+
/**
|
|
27
|
+
* Last execution timestamp
|
|
28
|
+
*/
|
|
29
|
+
private _lastRun;
|
|
30
|
+
/**
|
|
31
|
+
* Whether the job is currently executing
|
|
32
|
+
*/
|
|
33
|
+
private _isRunning;
|
|
34
|
+
/**
|
|
35
|
+
* Skip execution if job is already running
|
|
36
|
+
*/
|
|
37
|
+
private _skipIfRunning;
|
|
38
|
+
/**
|
|
39
|
+
* Retry configuration
|
|
40
|
+
*/
|
|
41
|
+
private _retryConfig;
|
|
42
|
+
/**
|
|
43
|
+
* Timezone for scheduling (defaults to system timezone)
|
|
44
|
+
*/
|
|
45
|
+
private _timezone;
|
|
46
|
+
/**
|
|
47
|
+
* Cron expression parser (mutually exclusive with interval config)
|
|
48
|
+
*/
|
|
49
|
+
private _cronParser;
|
|
50
|
+
/**
|
|
51
|
+
* Promise resolver for completion waiting
|
|
52
|
+
*/
|
|
53
|
+
private _completionResolver;
|
|
54
|
+
/**
|
|
55
|
+
* Next scheduled execution time
|
|
56
|
+
*/
|
|
57
|
+
nextRun: Dayjs | null;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new Job instance
|
|
60
|
+
*
|
|
61
|
+
* @param name - Unique identifier for the job
|
|
62
|
+
* @param callback - Function to execute when the job runs
|
|
63
|
+
*/
|
|
64
|
+
constructor(name: string, _callback: JobCallback);
|
|
65
|
+
/**
|
|
66
|
+
* Returns true if the job is currently executing
|
|
67
|
+
*/
|
|
68
|
+
get isRunning(): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Returns the last execution timestamp
|
|
71
|
+
*/
|
|
72
|
+
get lastRun(): Dayjs | null;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the current interval configuration (readonly)
|
|
75
|
+
*/
|
|
76
|
+
get intervals(): Readonly<JobIntervals>;
|
|
77
|
+
/**
|
|
78
|
+
* Set a custom interval for job execution
|
|
79
|
+
*
|
|
80
|
+
* @param value - Number of time units
|
|
81
|
+
* @param timeType - Type of time unit
|
|
82
|
+
* @returns this for chaining
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* job.every(5, "minute"); // Run every 5 minutes
|
|
87
|
+
* job.every(2, "hour"); // Run every 2 hours
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
every(value: number, timeType: TimeType): this;
|
|
91
|
+
/**
|
|
92
|
+
* Run job every second (use with caution - high frequency)
|
|
93
|
+
*/
|
|
94
|
+
everySecond(): this;
|
|
95
|
+
/**
|
|
96
|
+
* Run job every specified number of seconds
|
|
97
|
+
*/
|
|
98
|
+
everySeconds(seconds: number): this;
|
|
99
|
+
/**
|
|
100
|
+
* Run job every minute
|
|
101
|
+
*/
|
|
102
|
+
everyMinute(): this;
|
|
103
|
+
/**
|
|
104
|
+
* Run job every specified number of minutes
|
|
105
|
+
*/
|
|
106
|
+
everyMinutes(minutes: number): this;
|
|
107
|
+
/**
|
|
108
|
+
* Run job every hour
|
|
109
|
+
*/
|
|
110
|
+
everyHour(): this;
|
|
111
|
+
/**
|
|
112
|
+
* Run job every specified number of hours
|
|
113
|
+
*/
|
|
114
|
+
everyHours(hours: number): this;
|
|
115
|
+
/**
|
|
116
|
+
* Run job every day at midnight
|
|
117
|
+
*/
|
|
118
|
+
everyDay(): this;
|
|
119
|
+
/**
|
|
120
|
+
* Alias for everyDay()
|
|
121
|
+
*/
|
|
122
|
+
daily(): this;
|
|
123
|
+
/**
|
|
124
|
+
* Run job twice a day (every 12 hours)
|
|
125
|
+
*/
|
|
126
|
+
twiceDaily(): this;
|
|
127
|
+
/**
|
|
128
|
+
* Run job every week
|
|
129
|
+
*/
|
|
130
|
+
everyWeek(): this;
|
|
131
|
+
/**
|
|
132
|
+
* Alias for everyWeek()
|
|
133
|
+
*/
|
|
134
|
+
weekly(): this;
|
|
135
|
+
/**
|
|
136
|
+
* Run job every month
|
|
137
|
+
*/
|
|
138
|
+
everyMonth(): this;
|
|
139
|
+
/**
|
|
140
|
+
* Alias for everyMonth()
|
|
141
|
+
*/
|
|
142
|
+
monthly(): this;
|
|
143
|
+
/**
|
|
144
|
+
* Run job every year
|
|
145
|
+
*/
|
|
146
|
+
everyYear(): this;
|
|
147
|
+
/**
|
|
148
|
+
* Alias for everyYear()
|
|
149
|
+
*/
|
|
150
|
+
yearly(): this;
|
|
151
|
+
/**
|
|
152
|
+
* Alias for everyMinute() - job runs continuously every minute
|
|
153
|
+
*/
|
|
154
|
+
always(): this;
|
|
155
|
+
/**
|
|
156
|
+
* Schedule job using a cron expression
|
|
157
|
+
*
|
|
158
|
+
* Supports standard 5-field cron syntax:
|
|
159
|
+
* ```
|
|
160
|
+
* ┌───────────── minute (0-59)
|
|
161
|
+
* │ ┌───────────── hour (0-23)
|
|
162
|
+
* │ │ ┌───────────── day of month (1-31)
|
|
163
|
+
* │ │ │ ┌───────────── month (1-12)
|
|
164
|
+
* │ │ │ │ ┌───────────── day of week (0-6, Sunday = 0)
|
|
165
|
+
* │ │ │ │ │
|
|
166
|
+
* * * * * *
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* Supports:
|
|
170
|
+
* - '*' - any value
|
|
171
|
+
* - '5' - specific value
|
|
172
|
+
* - '1,3,5' - list of values
|
|
173
|
+
* - '1-5' - range of values
|
|
174
|
+
* - 'x/5' - step values (every 5)
|
|
175
|
+
* - '1-10/2' - range with step
|
|
176
|
+
*
|
|
177
|
+
* @param expression - Standard 5-field cron expression
|
|
178
|
+
* @returns this for chaining
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* job.cron("0 9 * * 1-5"); // 9 AM weekdays
|
|
183
|
+
* job.cron("x/5 * * * *"); // Every 5 minutes
|
|
184
|
+
* job.cron("0 0 1 * *"); // First day of month at midnight
|
|
185
|
+
* job.cron("0 x/2 * * *"); // Every 2 hours
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
cron(expression: string): this;
|
|
189
|
+
/**
|
|
190
|
+
* Get the cron expression if one is set
|
|
191
|
+
*/
|
|
192
|
+
get cronExpression(): string | null;
|
|
193
|
+
/**
|
|
194
|
+
* Schedule job on a specific day
|
|
195
|
+
*
|
|
196
|
+
* @param day - Day of week (string) or day of month (number 1-31)
|
|
197
|
+
* @returns this for chaining
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* job.on("monday"); // Run on Mondays
|
|
202
|
+
* job.on(15); // Run on the 15th of each month
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
on(day: Day | number): this;
|
|
206
|
+
/**
|
|
207
|
+
* Schedule job at a specific time
|
|
208
|
+
*
|
|
209
|
+
* @param time - Time in HH:mm or HH:mm:ss format
|
|
210
|
+
* @returns this for chaining
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* job.daily().at("09:00"); // Run daily at 9 AM
|
|
215
|
+
* job.weekly().at("14:30"); // Run weekly at 2:30 PM
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
at(time: string): this;
|
|
219
|
+
/**
|
|
220
|
+
* Run task at the beginning of the specified time period
|
|
221
|
+
*
|
|
222
|
+
* @param type - Time type (day, month, year)
|
|
223
|
+
*/
|
|
224
|
+
beginOf(type: TimeType): this;
|
|
225
|
+
/**
|
|
226
|
+
* Run task at the end of the specified time period
|
|
227
|
+
*
|
|
228
|
+
* @param type - Time type (day, month, year)
|
|
229
|
+
*/
|
|
230
|
+
endOf(type: TimeType): this;
|
|
231
|
+
/**
|
|
232
|
+
* Set the timezone for this job's scheduling
|
|
233
|
+
*
|
|
234
|
+
* @param tz - IANA timezone string (e.g., "America/New_York", "Europe/London")
|
|
235
|
+
* @returns this for chaining
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* job.daily().at("09:00").inTimezone("America/New_York");
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
inTimezone(tz: string): this;
|
|
243
|
+
/**
|
|
244
|
+
* Prevent overlapping executions of this job
|
|
245
|
+
*
|
|
246
|
+
* When enabled, if the job is already running when it's scheduled to run again,
|
|
247
|
+
* the new execution will be skipped.
|
|
248
|
+
*
|
|
249
|
+
* @param skip - Whether to skip if already running (default: true)
|
|
250
|
+
* @returns this for chaining
|
|
251
|
+
*/
|
|
252
|
+
preventOverlap(skip?: boolean): this;
|
|
253
|
+
/**
|
|
254
|
+
* Configure automatic retry on failure
|
|
255
|
+
*
|
|
256
|
+
* @param maxRetries - Maximum number of retry attempts
|
|
257
|
+
* @param delay - Delay between retries in milliseconds
|
|
258
|
+
* @param backoffMultiplier - Optional multiplier for exponential backoff
|
|
259
|
+
* @returns this for chaining
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* job.retry(3, 1000); // Retry 3 times with 1s delay
|
|
264
|
+
* job.retry(5, 1000, 2); // Exponential backoff: 1s, 2s, 4s, 8s, 16s
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
retry(maxRetries: number, delay?: number, backoffMultiplier?: number): this;
|
|
268
|
+
/**
|
|
269
|
+
* Terminate the job and clear all scheduling data
|
|
270
|
+
*/
|
|
271
|
+
terminate(): this;
|
|
272
|
+
/**
|
|
273
|
+
* Prepare the job by calculating the next run time
|
|
274
|
+
* Called by the scheduler when starting
|
|
275
|
+
*/
|
|
276
|
+
prepare(): void;
|
|
277
|
+
/**
|
|
278
|
+
* Determine if the job should run now
|
|
279
|
+
*
|
|
280
|
+
* @returns true if the job should execute
|
|
281
|
+
*/
|
|
282
|
+
shouldRun(): boolean;
|
|
283
|
+
/**
|
|
284
|
+
* Execute the job
|
|
285
|
+
*
|
|
286
|
+
* @returns Promise resolving to the job result
|
|
287
|
+
*/
|
|
288
|
+
run(): Promise<JobResult>;
|
|
289
|
+
/**
|
|
290
|
+
* Wait for the job to complete
|
|
291
|
+
* Useful for graceful shutdown
|
|
292
|
+
*
|
|
293
|
+
* @returns Promise that resolves when the job completes
|
|
294
|
+
*/
|
|
295
|
+
waitForCompletion(): Promise<void>;
|
|
296
|
+
/**
|
|
297
|
+
* Get current time, respecting timezone if set
|
|
298
|
+
*/
|
|
299
|
+
private _now;
|
|
300
|
+
/**
|
|
301
|
+
* Execute the callback with retry logic
|
|
302
|
+
*/
|
|
303
|
+
private _executeWithRetry;
|
|
304
|
+
/**
|
|
305
|
+
* Calculate retry delay with optional exponential backoff
|
|
306
|
+
*/
|
|
307
|
+
private _calculateRetryDelay;
|
|
308
|
+
/**
|
|
309
|
+
* Sleep for specified milliseconds
|
|
310
|
+
*/
|
|
311
|
+
private _sleep;
|
|
312
|
+
/**
|
|
313
|
+
* Calculate the next run time based on interval or cron configuration
|
|
314
|
+
*/
|
|
315
|
+
private _determineNextRun;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Factory function to create a new Job instance
|
|
319
|
+
*
|
|
320
|
+
* @param name - Unique identifier for the job
|
|
321
|
+
* @param callback - Function to execute when the job runs
|
|
322
|
+
* @returns New Job instance
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```typescript
|
|
326
|
+
* const cleanupJob = job("cleanup", async () => {
|
|
327
|
+
* await db.deleteExpiredTokens();
|
|
328
|
+
* }).daily().at("03:00");
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
export declare function job(name: string, callback: JobCallback): Job;
|
|
332
|
+
//# sourceMappingURL=job.d.ts.map
|
package/cjs/job.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job.d.ts","sourceRoot":"","sources":["../src/job.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,KAAK,KAAK,EAAE,MAAM,OAAO,CAAC;AAI1C,OAAO,KAAK,EAAE,GAAG,EAAE,YAAY,EAAE,SAAS,EAAe,QAAQ,EAAE,MAAM,SAAS,CAAC;AAMnF,MAAM,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAerD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,GAAG;aAiEI,IAAI,EAAE,MAAM;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS;IA7D5B;;OAEG;IACH,OAAO,CAAC,UAAU,CAAoB;IAEtC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAsB;IAEtC;;OAEG;IACH,OAAO,CAAC,UAAU,CAAS;IAE3B;;OAEG;IACH,OAAO,CAAC,cAAc,CAAS;IAE/B;;OAEG;IACH,OAAO,CAAC,YAAY,CAA4B;IAEhD;;OAEG;IACH,OAAO,CAAC,SAAS,CAAuB;IAExC;;OAEG;IACH,OAAO,CAAC,WAAW,CAA2B;IAE9C;;OAEG;IACH,OAAO,CAAC,mBAAmB,CAA6B;IAMxD;;OAEG;IACI,OAAO,EAAE,KAAK,GAAG,IAAI,CAAQ;IAMpC;;;;;OAKG;gBAEe,IAAI,EAAE,MAAM,EACX,SAAS,EAAE,WAAW;IAOzC;;OAEG;IACH,IAAW,SAAS,IAAI,OAAO,CAE9B;IAED;;OAEG;IACH,IAAW,OAAO,IAAI,KAAK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