@warlock.js/scheduler 4.0.174 → 4.1.2

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.
Files changed (69) hide show
  1. package/README.md +60 -1
  2. package/cjs/index.cjs +1132 -0
  3. package/cjs/index.cjs.map +1 -0
  4. package/esm/cron-parser.d.mts +129 -0
  5. package/esm/cron-parser.d.mts.map +1 -0
  6. package/esm/cron-parser.mjs +210 -0
  7. package/esm/cron-parser.mjs.map +1 -0
  8. package/esm/index.d.mts +5 -0
  9. package/esm/index.mjs +5 -0
  10. package/esm/job.d.mts +386 -0
  11. package/esm/job.d.mts.map +1 -0
  12. package/esm/job.mjs +633 -0
  13. package/esm/job.mjs.map +1 -0
  14. package/esm/scheduler.d.mts +193 -0
  15. package/esm/scheduler.d.mts.map +1 -0
  16. package/esm/scheduler.mjs +261 -0
  17. package/esm/scheduler.mjs.map +1 -0
  18. package/esm/types.d.mts +70 -0
  19. package/esm/types.d.mts.map +1 -0
  20. package/llms-full.txt +888 -0
  21. package/llms.txt +15 -0
  22. package/package.json +40 -28
  23. package/skills/configure-retry-and-overlap/SKILL.md +137 -0
  24. package/skills/observe-scheduler/SKILL.md +153 -0
  25. package/skills/overview/SKILL.md +92 -0
  26. package/skills/pin-schedule-timezone/SKILL.md +114 -0
  27. package/skills/schedule-fluently/SKILL.md +141 -0
  28. package/skills/schedule-with-cron/SKILL.md +123 -0
  29. package/skills/scheduler-basics/SKILL.md +94 -0
  30. package/cjs/cron-parser.d.ts +0 -98
  31. package/cjs/cron-parser.d.ts.map +0 -1
  32. package/cjs/cron-parser.js +0 -193
  33. package/cjs/cron-parser.js.map +0 -1
  34. package/cjs/index.d.ts +0 -44
  35. package/cjs/index.d.ts.map +0 -1
  36. package/cjs/index.js +0 -1
  37. package/cjs/index.js.map +0 -1
  38. package/cjs/job.d.ts +0 -332
  39. package/cjs/job.d.ts.map +0 -1
  40. package/cjs/job.js +0 -616
  41. package/cjs/job.js.map +0 -1
  42. package/cjs/scheduler.d.ts +0 -182
  43. package/cjs/scheduler.d.ts.map +0 -1
  44. package/cjs/scheduler.js +0 -316
  45. package/cjs/scheduler.js.map +0 -1
  46. package/cjs/types.d.ts +0 -63
  47. package/cjs/types.d.ts.map +0 -1
  48. package/cjs/utils.d.ts +0 -3
  49. package/cjs/utils.d.ts.map +0 -1
  50. package/esm/cron-parser.d.ts +0 -98
  51. package/esm/cron-parser.d.ts.map +0 -1
  52. package/esm/cron-parser.js +0 -193
  53. package/esm/cron-parser.js.map +0 -1
  54. package/esm/index.d.ts +0 -44
  55. package/esm/index.d.ts.map +0 -1
  56. package/esm/index.js +0 -1
  57. package/esm/index.js.map +0 -1
  58. package/esm/job.d.ts +0 -332
  59. package/esm/job.d.ts.map +0 -1
  60. package/esm/job.js +0 -616
  61. package/esm/job.js.map +0 -1
  62. package/esm/scheduler.d.ts +0 -182
  63. package/esm/scheduler.d.ts.map +0 -1
  64. package/esm/scheduler.js +0 -316
  65. package/esm/scheduler.js.map +0 -1
  66. package/esm/types.d.ts +0 -63
  67. package/esm/types.d.ts.map +0 -1
  68. package/esm/utils.d.ts +0 -3
  69. package/esm/utils.d.ts.map +0 -1
package/esm/job.d.mts ADDED
@@ -0,0 +1,386 @@
1
+ import { Day, JobIntervals, JobResult, TimeType } from "./types.mjs";
2
+ import { Dayjs } from "dayjs";
3
+
4
+ //#region ../../@warlock.js/scheduler/src/job.d.ts
5
+ type JobCallback = (job: Job) => Promise<any>;
6
+ /**
7
+ * Job class represents a scheduled task with configurable timing and execution options.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const job = new Job("cleanup", async () => {
12
+ * await cleanupOldFiles();
13
+ * })
14
+ * .everyDay()
15
+ * .at("03:00")
16
+ * .inTimezone("America/New_York")
17
+ * .preventOverlap()
18
+ * .retry(3, 1000);
19
+ * ```
20
+ */
21
+ declare class Job {
22
+ readonly name: string;
23
+ private readonly _callback;
24
+ /**
25
+ * Interval configuration for scheduling
26
+ */
27
+ private _intervals;
28
+ /**
29
+ * Last execution timestamp.
30
+ *
31
+ * Updated after every run attempt — successful or failed — so the next-run
32
+ * calculation always advances even when a job throws. Use the `job:complete`
33
+ * / `job:error` events if you need to distinguish success from failure.
34
+ */
35
+ private _lastRun;
36
+ /**
37
+ * Whether the job is currently executing
38
+ */
39
+ private _isRunning;
40
+ /**
41
+ * Skip execution if job is already running
42
+ */
43
+ private _skipIfRunning;
44
+ /**
45
+ * Retry configuration
46
+ */
47
+ private _retryConfig;
48
+ /**
49
+ * Timezone for scheduling (defaults to UTC)
50
+ */
51
+ private _timezone;
52
+ /**
53
+ * Cron expression parser (mutually exclusive with interval config)
54
+ */
55
+ private _cronParser;
56
+ /**
57
+ * All pending `waitForCompletion()` resolvers — drained on every run end.
58
+ */
59
+ private _completionResolvers;
60
+ /**
61
+ * Next scheduled execution time
62
+ */
63
+ nextRun: Dayjs | null;
64
+ /**
65
+ * Creates a new Job instance
66
+ *
67
+ * @param name - Unique identifier for the job
68
+ * @param callback - Function to execute when the job runs
69
+ */
70
+ constructor(name: string, _callback: JobCallback);
71
+ /**
72
+ * Returns true if the job is currently executing
73
+ */
74
+ get isRunning(): boolean;
75
+ /**
76
+ * Returns the last execution timestamp (success OR failure).
77
+ */
78
+ get lastRun(): Dayjs | null;
79
+ /**
80
+ * Returns the current interval configuration (readonly)
81
+ */
82
+ get intervals(): Readonly<JobIntervals>;
83
+ /**
84
+ * Set a custom interval for job execution
85
+ *
86
+ * @param value - Number of time units (must be > 0)
87
+ * @param timeType - Type of time unit
88
+ * @returns this for chaining
89
+ * @throws Error if `value` is not a positive finite number
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * job.every(5, "minute"); // Run every 5 minutes
94
+ * job.every(2, "hour"); // Run every 2 hours
95
+ * ```
96
+ */
97
+ every(value: number, timeType: TimeType): this;
98
+ /**
99
+ * Run job every second (use with caution - high frequency)
100
+ */
101
+ everySecond(): this;
102
+ /**
103
+ * Run job every specified number of seconds
104
+ */
105
+ everySeconds(seconds: number): this;
106
+ /**
107
+ * Run job every minute
108
+ */
109
+ everyMinute(): this;
110
+ /**
111
+ * Run job every specified number of minutes
112
+ */
113
+ everyMinutes(minutes: number): this;
114
+ /**
115
+ * Run job every hour
116
+ */
117
+ everyHour(): this;
118
+ /**
119
+ * Run job every specified number of hours
120
+ */
121
+ everyHours(hours: number): this;
122
+ /**
123
+ * Run job every day at midnight
124
+ */
125
+ everyDay(): this;
126
+ /**
127
+ * Alias for everyDay()
128
+ */
129
+ daily(): this;
130
+ /**
131
+ * Run job twice a day (every 12 hours)
132
+ */
133
+ twiceDaily(): this;
134
+ /**
135
+ * Run job every week
136
+ */
137
+ everyWeek(): this;
138
+ /**
139
+ * Alias for everyWeek()
140
+ */
141
+ weekly(): this;
142
+ /**
143
+ * Run job every month
144
+ */
145
+ everyMonth(): this;
146
+ /**
147
+ * Alias for everyMonth()
148
+ */
149
+ monthly(): this;
150
+ /**
151
+ * Run job every year
152
+ */
153
+ everyYear(): this;
154
+ /**
155
+ * Alias for everyYear()
156
+ */
157
+ yearly(): this;
158
+ /**
159
+ * Alias for everyMinute() - job runs continuously every minute
160
+ */
161
+ always(): this;
162
+ /**
163
+ * Schedule job using a cron expression
164
+ *
165
+ * Supports standard 5-field cron syntax:
166
+ * ```
167
+ * ┌───────────── minute (0-59)
168
+ * │ ┌───────────── hour (0-23)
169
+ * │ │ ┌───────────── day of month (1-31)
170
+ * │ │ │ ┌───────────── month (1-12)
171
+ * │ │ │ │ ┌───────────── day of week (0-6, Sunday = 0)
172
+ * │ │ │ │ │
173
+ * * * * * *
174
+ * ```
175
+ *
176
+ * Supports:
177
+ * - '*' - any value
178
+ * - '5' - specific value
179
+ * - '1,3,5' - list of values
180
+ * - '1-5' - range of values
181
+ * - '*‍/5' - step values (every 5)
182
+ * - '1-10/2' - range with step
183
+ *
184
+ * @param expression - Standard 5-field cron expression
185
+ * @returns this for chaining
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * job.cron("0 9 * * 1-5"); // 9 AM weekdays
190
+ * job.cron("*‍/5 * * * *"); // Every 5 minutes
191
+ * job.cron("0 0 1 * *"); // First day of month at midnight
192
+ * job.cron("0 *‍/2 * * *"); // Every 2 hours
193
+ * ```
194
+ */
195
+ cron(expression: string): this;
196
+ /**
197
+ * Get the cron expression if one is set
198
+ */
199
+ get cronExpression(): string | null;
200
+ /**
201
+ * Schedule job on a specific day
202
+ *
203
+ * @param day - Day of week (string) or day of month (number 1-31)
204
+ * @returns this for chaining
205
+ *
206
+ * @example
207
+ * ```typescript
208
+ * job.on("monday"); // Run on Mondays
209
+ * job.on(15); // Run on the 15th of each month
210
+ * ```
211
+ */
212
+ on(day: Day | number): this;
213
+ /**
214
+ * Schedule job at a specific time
215
+ *
216
+ * @param time - Time in HH:mm or HH:mm:ss format
217
+ * @returns this for chaining
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * job.daily().at("09:00"); // Run daily at 9 AM
222
+ * job.weekly().at("14:30"); // Run weekly at 2:30 PM
223
+ * ```
224
+ */
225
+ at(time: string): this;
226
+ /**
227
+ * Run task at the beginning of the specified time period.
228
+ *
229
+ * - `"day"` → 00:00 every day
230
+ * - `"month"` → 1st of every month at 00:00
231
+ * - `"year"` → January 1st at 00:00 every year
232
+ *
233
+ * @param type - Time type (day, month, year)
234
+ */
235
+ beginOf(type: TimeType): this;
236
+ /**
237
+ * Run task at the end of the specified time period.
238
+ *
239
+ * - `"day"` → 23:59 every day
240
+ * - `"month"` → last day of every month at 23:59 (recomputed each cycle —
241
+ * correct in February vs. March)
242
+ * - `"year"` → December 31st at 23:59 every year
243
+ *
244
+ * @param type - Time type (day, month, year)
245
+ */
246
+ endOf(type: TimeType): this;
247
+ /**
248
+ * Set the timezone for this job's scheduling
249
+ *
250
+ * @param tz - IANA timezone string (e.g., "America/New_York", "Europe/London")
251
+ * @returns this for chaining
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * job.daily().at("09:00").inTimezone("America/New_York");
256
+ * ```
257
+ */
258
+ inTimezone(tz: string): this;
259
+ /**
260
+ * Prevent overlapping executions of this job.
261
+ *
262
+ * When enabled, if the job is already running when it's scheduled to run again,
263
+ * the new execution will be skipped.
264
+ *
265
+ * @param skip - Whether to skip if already running (default: true)
266
+ * @returns this for chaining
267
+ */
268
+ preventOverlap(skip?: boolean): this;
269
+ /**
270
+ * Configure automatic retry on failure
271
+ *
272
+ * @param maxRetries - Maximum number of retry attempts (must be ≥ 0)
273
+ * @param delay - Delay between retries in milliseconds (must be ≥ 0)
274
+ * @param backoffMultiplier - Optional multiplier for exponential backoff (must be > 0)
275
+ * @returns this for chaining
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * job.retry(3, 1000); // Retry 3 times with 1s delay
280
+ * job.retry(5, 1000, 2); // Exponential backoff: 1s, 2s, 4s, 8s, 16s
281
+ * ```
282
+ */
283
+ retry(maxRetries: number, delay?: number, backoffMultiplier?: number): this;
284
+ /**
285
+ * Terminate the job and clear all scheduling data
286
+ */
287
+ terminate(): this;
288
+ /**
289
+ * Prepare the job by calculating the next run time
290
+ * Called by the scheduler when starting
291
+ */
292
+ prepare(): void;
293
+ /**
294
+ * Returns true if the job's `nextRun` has arrived, regardless of whether
295
+ * it is currently running. Used by the scheduler to decide whether a
296
+ * tick is "due" before checking overlap state.
297
+ */
298
+ isDue(): boolean;
299
+ /**
300
+ * Determine if the job should run now.
301
+ *
302
+ * Combines `isDue()` with the overlap-prevention rule: when
303
+ * `preventOverlap()` is on and the job is already running, this returns
304
+ * false even if the next-run time has arrived.
305
+ *
306
+ * @returns true if the job should execute
307
+ */
308
+ shouldRun(): boolean;
309
+ /**
310
+ * Execute the job once.
311
+ *
312
+ * Always advances `lastRun` and recalculates `nextRun` (success OR failure)
313
+ * so a permanently failing job does not re-fire on every scheduler tick.
314
+ *
315
+ * @returns Promise resolving to the job result
316
+ */
317
+ run(): Promise<JobResult>;
318
+ /**
319
+ * Wait for the currently executing run to complete.
320
+ *
321
+ * Multiple concurrent waiters are all resolved when the run finishes.
322
+ * Useful for graceful shutdown.
323
+ *
324
+ * @returns Promise that resolves when the job completes
325
+ */
326
+ waitForCompletion(): Promise<void>;
327
+ /**
328
+ * Get current time, respecting the configured timezone.
329
+ */
330
+ private _now;
331
+ /**
332
+ * Execute the callback with retry logic
333
+ */
334
+ private _executeWithRetry;
335
+ /**
336
+ * Calculate retry delay with optional exponential backoff
337
+ */
338
+ private _calculateRetryDelay;
339
+ /**
340
+ * Sleep for specified milliseconds
341
+ */
342
+ private _sleep;
343
+ /**
344
+ * Apply month / day-of-month / day-of-week / time constraints to a Dayjs.
345
+ *
346
+ * Re-applied after every interval advance inside `_determineNextRun` so
347
+ * dynamic constraints (`dayOfMonthMode: "last"` recomputed per month, month
348
+ * lock for `beginOf/endOf("year")`) stay correct as the candidate moves
349
+ * forward.
350
+ */
351
+ private _applyConstraints;
352
+ /**
353
+ * Calculate the next run time based on interval or cron configuration.
354
+ *
355
+ * Strategy: apply all constraints (month, day, time) ONCE before the
356
+ * advance loop, then advance by `every` until the candidate is in the
357
+ * future. Static constraints (numeric day, fixed month, fixed time)
358
+ * survive `dayjs.add()` automatically. The only dynamic constraint —
359
+ * `dayOfMonthMode: "last"` — is re-applied inside the loop so the
360
+ * candidate always lands on the *new* month's last day after each
361
+ * advance.
362
+ *
363
+ * Re-applying time/day inside the loop would deadlock: with
364
+ * `twiceDaily().at("06:00")` we'd advance 06:00 → 18:00 → snap back to
365
+ * 06:00 → 18:00 → ... forever.
366
+ */
367
+ private _determineNextRun;
368
+ }
369
+ /**
370
+ * Factory function to create a new Job instance
371
+ *
372
+ * @param name - Unique identifier for the job
373
+ * @param callback - Function to execute when the job runs
374
+ * @returns New Job instance
375
+ *
376
+ * @example
377
+ * ```typescript
378
+ * const cleanupJob = job("cleanup", async () => {
379
+ * await db.deleteExpiredTokens();
380
+ * }).daily().at("03:00");
381
+ * ```
382
+ */
383
+ declare function job(name: string, callback: JobCallback): Job;
384
+ //#endregion
385
+ export { Job, JobCallback, job };
386
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