@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.
- package/README.md +60 -1
- package/cjs/index.cjs +1132 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/cron-parser.d.mts +129 -0
- package/esm/cron-parser.d.mts.map +1 -0
- package/esm/cron-parser.mjs +210 -0
- package/esm/cron-parser.mjs.map +1 -0
- package/esm/index.d.mts +5 -0
- package/esm/index.mjs +5 -0
- package/esm/job.d.mts +386 -0
- package/esm/job.d.mts.map +1 -0
- package/esm/job.mjs +633 -0
- package/esm/job.mjs.map +1 -0
- package/esm/scheduler.d.mts +193 -0
- package/esm/scheduler.d.mts.map +1 -0
- package/esm/scheduler.mjs +261 -0
- package/esm/scheduler.mjs.map +1 -0
- package/esm/types.d.mts +70 -0
- package/esm/types.d.mts.map +1 -0
- package/llms-full.txt +888 -0
- package/llms.txt +15 -0
- package/package.json +40 -28
- package/skills/configure-retry-and-overlap/SKILL.md +137 -0
- package/skills/observe-scheduler/SKILL.md +153 -0
- package/skills/overview/SKILL.md +92 -0
- package/skills/pin-schedule-timezone/SKILL.md +114 -0
- package/skills/schedule-fluently/SKILL.md +141 -0
- package/skills/schedule-with-cron/SKILL.md +123 -0
- package/skills/scheduler-basics/SKILL.md +94 -0
- package/cjs/cron-parser.d.ts +0 -98
- package/cjs/cron-parser.d.ts.map +0 -1
- package/cjs/cron-parser.js +0 -193
- package/cjs/cron-parser.js.map +0 -1
- package/cjs/index.d.ts +0 -44
- package/cjs/index.d.ts.map +0 -1
- package/cjs/index.js +0 -1
- package/cjs/index.js.map +0 -1
- package/cjs/job.d.ts +0 -332
- package/cjs/job.d.ts.map +0 -1
- package/cjs/job.js +0 -616
- package/cjs/job.js.map +0 -1
- package/cjs/scheduler.d.ts +0 -182
- package/cjs/scheduler.d.ts.map +0 -1
- package/cjs/scheduler.js +0 -316
- package/cjs/scheduler.js.map +0 -1
- package/cjs/types.d.ts +0 -63
- package/cjs/types.d.ts.map +0 -1
- package/cjs/utils.d.ts +0 -3
- package/cjs/utils.d.ts.map +0 -1
- package/esm/cron-parser.d.ts +0 -98
- package/esm/cron-parser.d.ts.map +0 -1
- package/esm/cron-parser.js +0 -193
- package/esm/cron-parser.js.map +0 -1
- package/esm/index.d.ts +0 -44
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -1
- package/esm/index.js.map +0 -1
- package/esm/job.d.ts +0 -332
- package/esm/job.d.ts.map +0 -1
- package/esm/job.js +0 -616
- package/esm/job.js.map +0 -1
- package/esm/scheduler.d.ts +0 -182
- package/esm/scheduler.d.ts.map +0 -1
- package/esm/scheduler.js +0 -316
- package/esm/scheduler.js.map +0 -1
- package/esm/types.d.ts +0 -63
- package/esm/types.d.ts.map +0 -1
- package/esm/utils.d.ts +0 -3
- package/esm/utils.d.ts.map +0 -1
package/llms-full.txt
ADDED
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
# Warlock Scheduler — full skills
|
|
2
|
+
|
|
3
|
+
> Package: `@warlock.js/scheduler`
|
|
4
|
+
|
|
5
|
+
> Generated artifact. Concatenates every SKILL.md and reference file under `@warlock.js/scheduler/skills/`. Re-run `node scripts/generate-llms.mjs` after any change.
|
|
6
|
+
|
|
7
|
+
## configure-retry-and-overlap `@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
name: configure-retry-and-overlap
|
|
11
|
+
description: 'Configure `.retry(maxRetries, delay?, backoffMultiplier?)` for fixed / exponential backoff and `.preventOverlap()` for external-invocation safety. Triggers: `.retry`, `.preventOverlap`, `maxRetries`, `backoffMultiplier`, `JobResult.retries`, `job:error`, `job:skip`; "how do I retry a failed job", "exponential backoff for scheduled job", "prevent concurrent job runs", "stop firing after N consecutive failures"; typical import `import { job, scheduler } from "@warlock.js/scheduler"`. Skip: events / shutdown — `@warlock.js/scheduler/observe-scheduler/SKILL.md`; building the schedule itself — `@warlock.js/scheduler/schedule-fluently/SKILL.md`; competing libs `p-retry`, `async-retry`, `bullmq`.'
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# Retry, backoff, and overlap
|
|
15
|
+
|
|
16
|
+
Two independent execution-control concerns on a single `Job`. They compose cleanly.
|
|
17
|
+
|
|
18
|
+
## `.retry(maxRetries, delay?, backoffMultiplier?)`
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
job("send-report", sendReport)
|
|
22
|
+
.daily()
|
|
23
|
+
.at("08:00")
|
|
24
|
+
.retry(3); // 3 retries, 1000ms apart (default delay)
|
|
25
|
+
|
|
26
|
+
job("sync", syncInventory)
|
|
27
|
+
.everyHour()
|
|
28
|
+
.retry(5, 2000); // 5 retries, 2000ms each
|
|
29
|
+
|
|
30
|
+
job("queue", processQueue)
|
|
31
|
+
.everyMinutes(10)
|
|
32
|
+
.retry(5, 1000, 2); // exponential: 1s → 2s → 4s → 8s → 16s
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Signature:** `retry(maxRetries: number, delay = 1000, backoffMultiplier?: number): this`
|
|
36
|
+
|
|
37
|
+
**Formula:** delay before attempt `N+1` = `delay × backoffMultiplier^(N-1)`. Without a multiplier, all retries wait `delay` ms.
|
|
38
|
+
|
|
39
|
+
**Validation.** Throws at definition time on negative `maxRetries`, negative `delay`, or zero/negative `backoffMultiplier`. `retry(0)` is valid — means "no retries, single attempt."
|
|
40
|
+
|
|
41
|
+
## Retry count surfaces in `JobResult`
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
scheduler.on("job:complete", (name, result) => {
|
|
45
|
+
if (result.retries && result.retries > 0) {
|
|
46
|
+
log.warn({ name, retries: result.retries }, "succeeded after retries");
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
scheduler.on("job:error", (name, error) => {
|
|
51
|
+
// Fires once, AFTER all retries are exhausted.
|
|
52
|
+
log.error({ name, error }, "failed permanently");
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`JobResult.retries`:
|
|
57
|
+
- On **success**: the number of failed attempts before the eventual success (0 if first attempt succeeded).
|
|
58
|
+
- On **failure**: equals the configured `maxRetries` (all of them were used).
|
|
59
|
+
|
|
60
|
+
## After permanent failure — `nextRun` advances normally
|
|
61
|
+
|
|
62
|
+
This is load-bearing: a job that exhausts every retry and still throws does **not** re-fire on the next tick. The scheduler:
|
|
63
|
+
|
|
64
|
+
1. Emits `job:error` once with the final error.
|
|
65
|
+
2. Advances `nextRun` by the job's interval, same as success.
|
|
66
|
+
|
|
67
|
+
Both branches go through the same `finally`-block path in `Job.run()`. There is no "stuck on retry" mode.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
// Fires every 10 minutes, retries 3 times per fire.
|
|
71
|
+
// If the 10:00 run exhausts all retries, next run is at 10:10. NOT at 10:00:00.001.
|
|
72
|
+
job("flaky", fn).everyMinutes(10).retry(3, 1000);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
To **stop** firing after N consecutive failures, do it in user code:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
let consecutiveFailures = 0;
|
|
79
|
+
scheduler.on("job:error", name => {
|
|
80
|
+
if (name !== "flaky") return;
|
|
81
|
+
consecutiveFailures++;
|
|
82
|
+
if (consecutiveFailures >= 5) {
|
|
83
|
+
scheduler.removeJob("flaky");
|
|
84
|
+
alert.critical("flaky disabled after 5 failures");
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
scheduler.on("job:complete", name => {
|
|
88
|
+
if (name === "flaky") consecutiveFailures = 0;
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## `.preventOverlap(skip = true)`
|
|
93
|
+
|
|
94
|
+
Tells the scheduler to skip a tick if the job is already running.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
job("queue", processQueue)
|
|
98
|
+
.everyMinutes(5)
|
|
99
|
+
.preventOverlap();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Important nuance.** The scheduler awaits each tick's jobs before scheduling the next tick — so **concurrent same-job runs from the scheduler's own loop are structurally impossible**, regardless of `preventOverlap()`. Where it actually matters: jobs whose callbacks ALSO get invoked outside the scheduler.
|
|
103
|
+
|
|
104
|
+
Real-world shapes that benefit:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
// 1) Boot-time recovery sweep before normal scheduling.
|
|
108
|
+
const queueJob = job("queue", processQueueOnce).everyMinutes(5).preventOverlap();
|
|
109
|
+
scheduler.addJob(queueJob);
|
|
110
|
+
queueJob.run().catch(err => log.error({ err }, "boot sweep failed"));
|
|
111
|
+
scheduler.start();
|
|
112
|
+
|
|
113
|
+
// 2) Admin-triggered manual re-run from a dashboard endpoint.
|
|
114
|
+
app.post("/admin/jobs/:name/run", async (req, res) => {
|
|
115
|
+
const j = scheduler.getJob(req.params.name);
|
|
116
|
+
await j?.run();
|
|
117
|
+
res.sendStatus(204);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// 3) Multiple scheduler instances pointed at the same Job (rare).
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
If a tick lands while one of these external runs is mid-flight:
|
|
124
|
+
- Scheduler emits `job:skip` with reason `"Job is already running"`
|
|
125
|
+
- The tick proceeds without re-entering the job
|
|
126
|
+
|
|
127
|
+
For a single-process app where the callback is only ever invoked via the scheduler, `preventOverlap()` is a no-op — but it's free, defensive documentation. Keep it on for any job that touches shared state.
|
|
128
|
+
|
|
129
|
+
## Combining retry + preventOverlap
|
|
130
|
+
|
|
131
|
+
They compose cleanly. Retry happens WITHIN one fire — if all retries fail, the tick ends and `nextRun` advances. `preventOverlap` matters only between fires.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
job("long-flaky", processQueue)
|
|
135
|
+
.everyMinutes(5)
|
|
136
|
+
.preventOverlap()
|
|
137
|
+
.retry(3, 1000, 2); // up to 1 + 2 + 4 = 7 s of retry delay per fire
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
If retries push the total work past the next interval, `preventOverlap()` ensures the next scheduled tick skips instead of stacking.
|
|
141
|
+
|
|
142
|
+
## See also
|
|
143
|
+
|
|
144
|
+
- [`@warlock.js/scheduler/observe-scheduler/SKILL.md`](@warlock.js/scheduler/observe-scheduler/SKILL.md) — full event reference, `JobResult` shape, lifecycle
|
|
145
|
+
- [`@warlock.js/scheduler/schedule-fluently/SKILL.md`](@warlock.js/scheduler/schedule-fluently/SKILL.md) — the scheduling methods these compose with
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
## observe-scheduler `@warlock.js/scheduler/observe-scheduler/SKILL.md`
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
name: observe-scheduler
|
|
152
|
+
description: 'Subscribe to scheduler lifecycle and job events for logging / metrics / alerting / graceful shutdown — seven typed `SchedulerEvents`, `JobResult` payload, `start()` / `stop()` / `shutdown()`. Triggers: `scheduler.on`, `scheduler.shutdown`, `JobResult`, `job:complete`, `job:error`, `scheduler:started`, `scheduler.getJob`; "graceful SIGTERM shutdown", "did this job run", "scheduler metrics and alerts", "subscribe to job events"; typical import `import { scheduler, type JobResult } from "@warlock.js/scheduler"`. Skip: retry / overlap — `@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`; building schedules — `@warlock.js/scheduler/schedule-fluently/SKILL.md`; native `EventEmitter`, `process.on`.'
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
# Observability and lifecycle
|
|
156
|
+
|
|
157
|
+
The `Scheduler` extends Node's `EventEmitter` with a fully-typed surface. Wire listeners before calling `.start()`.
|
|
158
|
+
|
|
159
|
+
## All seven events
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
type SchedulerEvents = {
|
|
163
|
+
"job:start": [jobName: string];
|
|
164
|
+
"job:complete": [jobName: string, result: JobResult];
|
|
165
|
+
"job:error": [jobName: string, error: unknown];
|
|
166
|
+
"job:skip": [jobName: string, reason: string];
|
|
167
|
+
"scheduler:started": [];
|
|
168
|
+
"scheduler:stopped": [];
|
|
169
|
+
"scheduler:tick": [timestamp: Date];
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
| Event | When it fires |
|
|
174
|
+
| -------------------- | -------------------------------------------------------------------------------------- |
|
|
175
|
+
| `scheduler:started` | Once, after `.start()` enters the tick loop |
|
|
176
|
+
| `scheduler:stopped` | When `.stop()` halts the loop. Not emitted if `.stop()` was a no-op (never started) |
|
|
177
|
+
| `scheduler:tick` | Every tick — once per `runEvery()` interval (default 1 s). Keep handlers lightweight. |
|
|
178
|
+
| `job:start` | Just before a job's callback is first invoked (NOT once per retry) |
|
|
179
|
+
| `job:complete` | When a job finishes successfully (possibly after retries) |
|
|
180
|
+
| `job:error` | When a job exhausts all retries and the final attempt still throws |
|
|
181
|
+
| `job:skip` | When a tick finds the job already running (see retry-and-overlap for when this occurs) |
|
|
182
|
+
|
|
183
|
+
**Contract for retries.** `job:start` and `job:complete`/`job:error` fire **once per fire**, not once per retry attempt. The retry count surfaces inside the `JobResult` passed to `job:complete`, or as `result.retries === maxRetries` on the failure path.
|
|
184
|
+
|
|
185
|
+
## `JobResult` shape
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
type JobResult = {
|
|
189
|
+
success: boolean;
|
|
190
|
+
duration: number; // milliseconds, end-to-end including retry waits
|
|
191
|
+
error?: unknown; // present when success === false
|
|
192
|
+
retries?: number; // attempts that failed before the final outcome
|
|
193
|
+
};
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`duration` is wall-clock from the first attempt's start to the final settlement — useful for both p50 metrics and capacity planning.
|
|
197
|
+
|
|
198
|
+
## Subscribing
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import type { JobResult } from "@warlock.js/scheduler";
|
|
202
|
+
import { scheduler } from "@warlock.js/scheduler";
|
|
203
|
+
|
|
204
|
+
scheduler.on("job:start", name => log.debug({ name }, "job start"));
|
|
205
|
+
scheduler.on("job:complete", (name, result: JobResult) => {
|
|
206
|
+
log.info({ name, duration: result.duration, retries: result.retries }, "job complete");
|
|
207
|
+
});
|
|
208
|
+
scheduler.on("job:error", (name, error) => log.error({ name, error }, "job failed"));
|
|
209
|
+
scheduler.on("job:skip", (name, reason) => log.warn({ name, reason }, "job skipped"));
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`.on()`, `.once()`, `.off()` are all type-narrowed via `SchedulerEvents`.
|
|
213
|
+
|
|
214
|
+
## Lifecycle methods
|
|
215
|
+
|
|
216
|
+
### `.start()`
|
|
217
|
+
|
|
218
|
+
Prepares every registered job (computes initial `nextRun`) and enters the tick loop. Emits `scheduler:started`.
|
|
219
|
+
|
|
220
|
+
Throws if:
|
|
221
|
+
- already running (`"Scheduler is already running."`)
|
|
222
|
+
- zero jobs registered (`"Cannot start scheduler with no jobs."`)
|
|
223
|
+
|
|
224
|
+
### `.stop()`
|
|
225
|
+
|
|
226
|
+
Immediately clears the next-tick timer. Does NOT wait for jobs currently mid-execution. Emits `scheduler:stopped`. **No-op if not running** — calling `.stop()` on a never-started or already-stopped scheduler is safe and silent.
|
|
227
|
+
|
|
228
|
+
### `.shutdown(timeout = 30000)`
|
|
229
|
+
|
|
230
|
+
Graceful equivalent of `.stop()`:
|
|
231
|
+
|
|
232
|
+
1. Marks the scheduler as shutting down (no new ticks scheduled).
|
|
233
|
+
2. Calls `.stop()` internally (so `scheduler:stopped` fires).
|
|
234
|
+
3. Awaits every currently-running job's `waitForCompletion()`, capped by `timeout`.
|
|
235
|
+
4. Resolves either when all jobs finish or when the timeout elapses.
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
process.on("SIGTERM", async () => {
|
|
239
|
+
await scheduler.shutdown(30_000);
|
|
240
|
+
process.exit(0);
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The timeout is a HARD cap — jobs still running after it are abandoned (their promises keep resolving in the background, but the scheduler doesn't await them). The package does not currently support per-job timeouts or `AbortSignal` cancellation.
|
|
245
|
+
|
|
246
|
+
## Production observer pattern
|
|
247
|
+
|
|
248
|
+
Wire all observers before `start()`, in one place:
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
import { scheduler } from "@warlock.js/scheduler";
|
|
252
|
+
import { logger } from "./logger";
|
|
253
|
+
import { metrics } from "./metrics";
|
|
254
|
+
import { alerts } from "./alerts";
|
|
255
|
+
|
|
256
|
+
scheduler.on("job:start", name => metrics.increment(`job.start.${name}`));
|
|
257
|
+
|
|
258
|
+
scheduler.on("job:complete", (name, result) => {
|
|
259
|
+
metrics.increment(`job.success.${name}`);
|
|
260
|
+
metrics.timing(`job.duration.${name}`, result.duration);
|
|
261
|
+
if (result.retries) metrics.increment(`job.retries.${name}`, result.retries);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
scheduler.on("job:error", (name, error) => {
|
|
265
|
+
logger.error({ name, error }, "Job failed permanently");
|
|
266
|
+
alerts.critical(`Scheduler job "${name}" failed`, error);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
scheduler.on("job:skip", (name, reason) => {
|
|
270
|
+
logger.warn({ name, reason }, "Job skipped — likely external invocation in flight");
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
scheduler.on("scheduler:started", () => logger.info("scheduler up"));
|
|
274
|
+
scheduler.on("scheduler:stopped", () => logger.info("scheduler down"));
|
|
275
|
+
|
|
276
|
+
// Register jobs, then:
|
|
277
|
+
scheduler.start();
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Inspection at runtime
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
scheduler.isRunning; // boolean
|
|
284
|
+
scheduler.jobCount; // number of registered jobs
|
|
285
|
+
scheduler.list(); // readonly Job[]
|
|
286
|
+
scheduler.getJob("name"); // Job | undefined
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
And on each `Job`:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
job.nextRun; // Dayjs | null
|
|
293
|
+
job.lastRun; // Dayjs | null — success OR failure
|
|
294
|
+
job.isRunning; // boolean
|
|
295
|
+
job.intervals; // readonly schedule config
|
|
296
|
+
job.cronExpression; // string | null
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## See also
|
|
300
|
+
|
|
301
|
+
- [`@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`](@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md) — what triggers each event in detail
|
|
302
|
+
- [`@warlock.js/scheduler/schedule-fluently/SKILL.md`](@warlock.js/scheduler/schedule-fluently/SKILL.md) — the methods that mutate the state these getters expose
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
## overview `@warlock.js/scheduler/overview/SKILL.md`
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
name: overview
|
|
309
|
+
description: 'Front-door orientation for `@warlock.js/scheduler` — cron-like job scheduling with fluent schedule API (`.daily().at()`, `.weekly().on()`, `.cron("...")`), retry with backoff, overlap prevention, IANA timezone pinning, and seven typed lifecycle events. UTC default; opt into local time per job. TRIGGER when: code imports anything from `@warlock.js/scheduler`; user asks "what does @warlock.js/scheduler do", "compare with node-cron / agenda / bull", "schedule a cron job in Node", "how do I prevent overlapping runs", "how do I retry a failed job", "what timezone does the scheduler use"; package.json adds `@warlock.js/scheduler`. Skip: specific task already known — load the matching task skill directly (`scheduler-basics`, `schedule-fluently`, `schedule-with-cron`, `configure-retry-and-overlap`, `pin-schedule-timezone`, `observe-scheduler`); ad-hoc `setTimeout` / `setInterval` for one-off delays (no cron / retry / observability needed).'
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
# `@warlock.js/scheduler` — overview
|
|
313
|
+
|
|
314
|
+
Production-ready job scheduler with a cron engine, fluent schedule builder, retry-with-backoff, overlap prevention, IANA timezone pinning, and seven typed lifecycle events. Two primitives — `job(name, fn)` and the `scheduler` singleton — that compose into everything else.
|
|
315
|
+
|
|
316
|
+
## When to reach for it
|
|
317
|
+
|
|
318
|
+
- You have **recurring work** in a Node service (cleanups, reports, syncs, polling) and want it scheduled inside the app process — no external cron, no docker-cron sidecar.
|
|
319
|
+
- You'd reach for **node-cron** or **agenda** but want a typed fluent API, retry-with-backoff, overlap prevention, and a working observability story built in — without bringing Redis (agenda) or hand-rolling retry yourself (node-cron).
|
|
320
|
+
- You're inside a `@warlock.js/*` project — the framework already uses this for built-in maintenance jobs (token cleanup, etc.).
|
|
321
|
+
|
|
322
|
+
Skip if you need a **distributed** job queue with persistence, retries across processes, and worker pools — reach for **BullMQ** or **Temporal**. This package runs jobs **in-process**; if the process dies before the next tick, the schedule resumes from now, not from the missed fire time.
|
|
323
|
+
|
|
324
|
+
## The mental model in one paragraph
|
|
325
|
+
|
|
326
|
+
Define a job with `job("name", async () => { ... })`. Attach a schedule via the fluent API (`.daily().at("03:00")`) or a cron string (`.cron("0 3 * * *")`). Optionally pin a timezone (`.inTimezone("America/New_York")`), prevent concurrent runs (`.preventOverlap()`), and configure retries (`.retry(3, 1000)`). Register the job with `scheduler.addJob(job)`, call `scheduler.start()`, and listen on the seven lifecycle events (`job:start` / `complete` / `error` / `skip`, `scheduler:started` / `stopped` / `tick`) for logging, metrics, and alerting. `scheduler.shutdown()` drains in-flight jobs before exit.
|
|
327
|
+
|
|
328
|
+
## Skills index
|
|
329
|
+
|
|
330
|
+
Six task skills cover the full surface. Most callers only need `scheduler-basics` + `schedule-fluently` + `observe-scheduler`.
|
|
331
|
+
|
|
332
|
+
### Foundations
|
|
333
|
+
|
|
334
|
+
#### [`scheduler-basics`](@warlock.js/scheduler/scheduler-basics/SKILL.md)
|
|
335
|
+
Start here. The `job(name, fn)` factory, the `scheduler` singleton, the 2-primitive surface, UTC default, why the API is factory-first.
|
|
336
|
+
|
|
337
|
+
### Scheduling
|
|
338
|
+
|
|
339
|
+
#### [`schedule-fluently`](@warlock.js/scheduler/schedule-fluently/SKILL.md)
|
|
340
|
+
Build schedules without writing cron: `.everyMinutes(N)`, `.daily().at("03:00")`, `.weekly().on("monday")`, `.monthly()`, `.beginOf("month")`, `.endOf("year")`, and friends. Reach for this 80% of the time.
|
|
341
|
+
|
|
342
|
+
#### [`schedule-with-cron`](@warlock.js/scheduler/schedule-with-cron/SKILL.md)
|
|
343
|
+
Drop down to `.cron("0 9 * * 1-5")` when the fluent API can't express it (Vixie OR semantics for DOM/DOW, complex multi-value lists). Includes `parseCron()` for previewing next-run times before deploy.
|
|
344
|
+
|
|
345
|
+
### Production concerns
|
|
346
|
+
|
|
347
|
+
#### [`configure-retry-and-overlap`](@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md)
|
|
348
|
+
`.retry(maxRetries, delay?, backoffMultiplier?)` for fixed or exponential backoff; `.preventOverlap()` so a slow run never collides with the next tick. Reach for both anytime a job calls external services.
|
|
349
|
+
|
|
350
|
+
#### [`pin-schedule-timezone`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md)
|
|
351
|
+
`.inTimezone("America/New_York")` pins wall-clock fire times across DST and across servers. The default is UTC — if your job description includes "9 AM," you almost certainly want this skill.
|
|
352
|
+
|
|
353
|
+
### Observability + shutdown
|
|
354
|
+
|
|
355
|
+
#### [`observe-scheduler`](@warlock.js/scheduler/observe-scheduler/SKILL.md)
|
|
356
|
+
The seven typed `SchedulerEvents` (`job:start` / `complete` / `error` / `skip`, `scheduler:started` / `stopped` / `tick`), the `JobResult` payload, `start()` / `stop()` / `shutdown()`. Wire this for logging, metrics, alerting, and graceful SIGTERM handling.
|
|
357
|
+
|
|
358
|
+
## Quick taste
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
import { Scheduler, job } from "@warlock.js/scheduler";
|
|
362
|
+
|
|
363
|
+
const scheduler = new Scheduler();
|
|
364
|
+
|
|
365
|
+
scheduler.on("job:error", (name, error) => {
|
|
366
|
+
console.error(`${name} failed:`, error);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
scheduler.addJob(
|
|
370
|
+
job("cleanup", async () => {
|
|
371
|
+
await cleanupExpiredTokens();
|
|
372
|
+
})
|
|
373
|
+
.daily()
|
|
374
|
+
.at("03:00")
|
|
375
|
+
.inTimezone("America/New_York")
|
|
376
|
+
.preventOverlap()
|
|
377
|
+
.retry(3, 1000),
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
scheduler.addJob(
|
|
381
|
+
job("reports", sendReports).cron("0 9 * * 1-5"), // 9 AM weekdays
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
scheduler.start();
|
|
385
|
+
process.on("SIGTERM", () => scheduler.shutdown());
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
## What this package deliberately doesn't do
|
|
389
|
+
|
|
390
|
+
- **Distributed scheduling across processes.** Jobs run in the calling process. For multi-instance deployments, either run the scheduler on one instance (with a leader election) or reach for BullMQ / Temporal.
|
|
391
|
+
- **Persistent job state across restarts.** A missed fire while the process was down is missed — it doesn't catch up. For exactly-once-eventually semantics, use a queue.
|
|
392
|
+
- **Job priority queues or fan-out.** One job = one function. For fan-out, your job dispatches to a queue.
|
|
393
|
+
- **Cron-syntax extensions** beyond the standard 5-field form (no `@daily` macros, no seconds field). Use the fluent API for human-readable schedules instead.
|
|
394
|
+
|
|
395
|
+
## See also
|
|
396
|
+
|
|
397
|
+
- [`@warlock.js/core/overview/SKILL.md`](@warlock.js/core/overview/SKILL.md) — the parent framework that scheduled jobs typically run alongside.
|
|
398
|
+
- `mongez-agent-kit-authoring-skills` (load via agent-kit sync) — how this `overview/SKILL.md` becomes the front-door skill in `.claude/skills/warlock-js-scheduler-overview/`. Every cross-link above uses the `@warlock.js/scheduler/<skill>/SKILL.md` name form so it survives that flattening.
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
## pin-schedule-timezone `@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`
|
|
402
|
+
|
|
403
|
+
---
|
|
404
|
+
name: pin-schedule-timezone
|
|
405
|
+
description: 'Pin a job to a specific IANA timezone via `.inTimezone(zone)` so its wall-clock fire time stays correct regardless of server location / DST. Triggers: `.inTimezone`, `America/New_York`, `Europe/Berlin`, `Asia/Tokyo`, `NODE_ICU_DATA`; "schedule job in a timezone", "DST drift on non-UTC server", "multi-region fan-out scheduling", "fire at 9am ET regardless of server"; typical import `import { job, scheduler } from "@warlock.js/scheduler"`. Skip: fluent interval methods — `@warlock.js/scheduler/schedule-fluently/SKILL.md`; cron syntax — `@warlock.js/scheduler/schedule-with-cron/SKILL.md`; competing libs `dayjs-timezone`, `luxon`, `date-fns-tz`.'
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
# Per-job timezones
|
|
409
|
+
|
|
410
|
+
Every `Job` has its own timezone. The default is **UTC** — `.daily().at("09:00")` fires at 09:00 UTC, regardless of the server's locale or system timezone. Pin to wall-clock time with `.inTimezone(IANA string)`.
|
|
411
|
+
|
|
412
|
+
## Basic usage
|
|
413
|
+
|
|
414
|
+
```ts
|
|
415
|
+
job("morning-digest", sendDailyDigest)
|
|
416
|
+
.daily()
|
|
417
|
+
.at("08:00")
|
|
418
|
+
.inTimezone("America/New_York"); // 8 AM ET, not 8 AM UTC
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
`.inTimezone()` is chainable — typically placed after the time/day methods.
|
|
422
|
+
|
|
423
|
+
## Why the default is UTC
|
|
424
|
+
|
|
425
|
+
A server runs wherever ops put it (Frankfurt, Virginia, GCP us-central1, …). System-local time is unpredictable; UTC is stable. The framework picks the stable default so that `daily().at("09:00")` without further config is reproducible across deployments. Pin to wall-clock time when business hours actually matter.
|
|
426
|
+
|
|
427
|
+
## Multi-region fan-out
|
|
428
|
+
|
|
429
|
+
Same logical task, three regions:
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
import { scheduler, job } from "@warlock.js/scheduler";
|
|
433
|
+
|
|
434
|
+
const regions = [
|
|
435
|
+
{ name: "us-east", tz: "America/New_York" },
|
|
436
|
+
{ name: "eu-west", tz: "Europe/Berlin" },
|
|
437
|
+
{ name: "asia", tz: "Asia/Tokyo" },
|
|
438
|
+
];
|
|
439
|
+
|
|
440
|
+
for (const { name, tz } of regions) {
|
|
441
|
+
scheduler.addJob(
|
|
442
|
+
job(`morning-report-${name}`, () => sendReport(name))
|
|
443
|
+
.daily()
|
|
444
|
+
.at("09:00")
|
|
445
|
+
.inTimezone(tz)
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
scheduler.start();
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
Three separate `Job` instances, three separate `nextRun` calculations. Each fires once per day at its region's 09:00.
|
|
453
|
+
|
|
454
|
+
## DST is automatic
|
|
455
|
+
|
|
456
|
+
dayjs handles transitions correctly. A daily 09:00 job in `America/New_York` shifts between 13:00 UTC (EST winter) and 14:00 UTC (EDT summer) without intervention. The same job in a fixed-offset region (`Asia/Tokyo`, `Africa/Algiers`) stays at a constant UTC offset.
|
|
457
|
+
|
|
458
|
+
## Common IANA strings
|
|
459
|
+
|
|
460
|
+
| Region | TZ string |
|
|
461
|
+
| ------------------- | --------------------------- |
|
|
462
|
+
| UTC | `UTC` |
|
|
463
|
+
| US Eastern | `America/New_York` |
|
|
464
|
+
| US Central | `America/Chicago` |
|
|
465
|
+
| US Mountain | `America/Denver` |
|
|
466
|
+
| US Pacific | `America/Los_Angeles` |
|
|
467
|
+
| UK / Ireland | `Europe/London` |
|
|
468
|
+
| Central Europe | `Europe/Berlin` |
|
|
469
|
+
| Eastern Europe | `Europe/Kyiv` |
|
|
470
|
+
| India | `Asia/Kolkata` |
|
|
471
|
+
| Japan | `Asia/Tokyo` |
|
|
472
|
+
| Australia (Sydney) | `Australia/Sydney` |
|
|
473
|
+
| Egypt | `Africa/Cairo` |
|
|
474
|
+
|
|
475
|
+
Full list: [IANA Time Zone Database](https://www.iana.org/time-zones).
|
|
476
|
+
|
|
477
|
+
## Interaction with `at()`, `on()`, cron
|
|
478
|
+
|
|
479
|
+
The timezone applies to every interpretation of "now" and every constraint:
|
|
480
|
+
|
|
481
|
+
```ts
|
|
482
|
+
job("monday-standup", task)
|
|
483
|
+
.weekly()
|
|
484
|
+
.on("monday")
|
|
485
|
+
.at("09:00")
|
|
486
|
+
.inTimezone("Europe/Berlin");
|
|
487
|
+
// "Monday in Berlin local time" + "09:00 Berlin local time"
|
|
488
|
+
// — automatically shifts CET ↔ CEST as DST changes.
|
|
489
|
+
|
|
490
|
+
job("nightly-cron", task)
|
|
491
|
+
.cron("0 3 * * *")
|
|
492
|
+
.inTimezone("Asia/Tokyo");
|
|
493
|
+
// "0 3 * * *" interpreted in Tokyo local time → 03:00 JST = 18:00 UTC.
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
## Validation
|
|
497
|
+
|
|
498
|
+
`.inTimezone()` stores the string as-is, then immediately recomputes `nextRun` — and that recompute calls dayjs with the zone. An invalid IANA string makes dayjs throw a `RangeError: Invalid time zone specified` **synchronously, at that point in the chain**:
|
|
499
|
+
|
|
500
|
+
```ts
|
|
501
|
+
// Throws right here — the .inTimezone() call recomputes nextRun, which
|
|
502
|
+
// hits dayjs().tz("Asia/Whatever") and raises RangeError immediately.
|
|
503
|
+
const j = job("t", task).daily().at("09:00").inTimezone("Asia/Whatever");
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
A valid zone computes `nextRun` on the spot (non-null after the chain). So a typo fails fast at definition time, not at the first tick — no special unit test needed to surface it.
|
|
507
|
+
|
|
508
|
+
## Server-side caveats
|
|
509
|
+
|
|
510
|
+
Node ships full ICU on most platforms, so all IANA zones work out of the box. If you're running a small-ICU build (alpine without full ICU, or old Node versions with `--icu=small`), only a limited set of zones is recognized. Set the `NODE_ICU_DATA` env var or use the `full-icu` package in that case.
|
|
511
|
+
|
|
512
|
+
## See also
|
|
513
|
+
|
|
514
|
+
- [`@warlock.js/scheduler/schedule-fluently/SKILL.md`](@warlock.js/scheduler/schedule-fluently/SKILL.md) — `at()`, `on()`, `daily()`, etc.
|
|
515
|
+
- [`@warlock.js/scheduler/schedule-with-cron/SKILL.md`](@warlock.js/scheduler/schedule-with-cron/SKILL.md) — cron schedules are timezone-aware too
|
|
516
|
+
- [`@warlock.js/scheduler/observe-scheduler/SKILL.md`](@warlock.js/scheduler/observe-scheduler/SKILL.md) — `job.nextRun.toISOString()` always renders in UTC; format with `.tz(zone)` to see local time
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
## schedule-fluently `@warlock.js/scheduler/schedule-fluently/SKILL.md`
|
|
520
|
+
|
|
521
|
+
---
|
|
522
|
+
name: schedule-fluently
|
|
523
|
+
description: 'Build a job schedule via `every*` / `daily` / `weekly` / `monthly` / `at` / `on` / `beginOf` / `endOf`. Triggers: `.everyMinutes`, `.daily`, `.weekly`, `.monthly`, `.at`, `.on`, `.every`, `.beginOf`, `.endOf`, `.twiceDaily`, `scheduler.newJob`; "run every 5 minutes", "daily at 3am", "monday standup", "first of every month", "last day of month"; typical import `import { job, scheduler } from "@warlock.js/scheduler"`. Skip: cron — `@warlock.js/scheduler/schedule-with-cron/SKILL.md`; timezone — `@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`; retry — `@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`; competing `node-cron`, `node-schedule`, `agenda`; native `setInterval`.'
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
# Fluent scheduling API
|
|
527
|
+
|
|
528
|
+
Chainable methods on a `Job` instance. Each method returns `this` for chaining; each call recomputes `job.nextRun` on the spot.
|
|
529
|
+
|
|
530
|
+
## Preset intervals
|
|
531
|
+
|
|
532
|
+
| Method | Equivalent of | Notes |
|
|
533
|
+
| -------------------- | ------------------------ | ------------------------------ |
|
|
534
|
+
| `.everySecond()` | `.every(1, "second")` | High frequency — use sparingly |
|
|
535
|
+
| `.everySeconds(N)` | `.every(N, "second")` | |
|
|
536
|
+
| `.everyMinute()` | `.every(1, "minute")` | |
|
|
537
|
+
| `.everyMinutes(N)` | `.every(N, "minute")` | |
|
|
538
|
+
| `.everyHour()` | `.every(1, "hour")` | |
|
|
539
|
+
| `.everyHours(N)` | `.every(N, "hour")` | |
|
|
540
|
+
| `.everyDay()` | `.every(1, "day")` | Same as `.daily()` |
|
|
541
|
+
| `.daily()` | `.every(1, "day")` | |
|
|
542
|
+
| `.twiceDaily()` | `.every(12, "hour")` | Every 12 hours |
|
|
543
|
+
| `.everyWeek()` | `.every(1, "week")` | Same as `.weekly()` |
|
|
544
|
+
| `.weekly()` | `.every(1, "week")` | |
|
|
545
|
+
| `.everyMonth()` | `.every(1, "month")` | Same as `.monthly()` |
|
|
546
|
+
| `.monthly()` | `.every(1, "month")` | |
|
|
547
|
+
| `.everyYear()` | `.every(1, "year")` | Same as `.yearly()` |
|
|
548
|
+
| `.yearly()` | `.every(1, "year")` | |
|
|
549
|
+
| `.always()` | `.every(1, "minute")` | Continuous "tick" jobs |
|
|
550
|
+
|
|
551
|
+
## Custom intervals — `.every(value, unit)`
|
|
552
|
+
|
|
553
|
+
```ts
|
|
554
|
+
job("t", task).every(5, "minute");
|
|
555
|
+
job("t", task).every(2, "hour");
|
|
556
|
+
job("t", task).every(3, "day");
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
Units: `"second" | "minute" | "hour" | "day" | "week" | "month" | "year"`.
|
|
560
|
+
|
|
561
|
+
**Validation.** `every(value, unit)` throws at definition time if `value` is `0`, negative, `NaN`, or `Infinity`. This guards against the misconfigured-interval class of bugs that would otherwise spin the scheduler.
|
|
562
|
+
|
|
563
|
+
## Target a specific time — `.at("HH:mm" | "HH:mm:ss")`
|
|
564
|
+
|
|
565
|
+
```ts
|
|
566
|
+
job("nightly", fn).daily().at("03:00");
|
|
567
|
+
job("midday", fn).daily().at("12:30:15");
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
**Validation.** `.at()` throws if the format is malformed (`"foo"`, `"9-30"`), or any component is out of range (hour > 23, minute > 59, second > 59).
|
|
571
|
+
|
|
572
|
+
## Target a specific day — `.on(day)`
|
|
573
|
+
|
|
574
|
+
Day-of-week (string) or day-of-month (number 1–31):
|
|
575
|
+
|
|
576
|
+
```ts
|
|
577
|
+
job("monday-standup", task).weekly().on("monday");
|
|
578
|
+
job("mid-month-sync", task).monthly().on(15);
|
|
579
|
+
job("end-of-month", task).monthly().on(31); // see beginOf/endOf for the right way
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
Valid day-of-week strings: `"sunday"`, `"monday"`, `"tuesday"`, `"wednesday"`, `"thursday"`, `"friday"`, `"saturday"`.
|
|
583
|
+
|
|
584
|
+
**Validation.** Numeric `on(N)` throws if `N < 1 || N > 31`.
|
|
585
|
+
|
|
586
|
+
**Gotcha.** `monthly().on(31)` clamps to the actual month length in dayjs — February runs are unpredictable. Use `endOf("month")` for "last day" semantics instead.
|
|
587
|
+
|
|
588
|
+
## Boundary shortcuts — `.beginOf(type)` / `.endOf(type)`
|
|
589
|
+
|
|
590
|
+
Both accept `"day" | "month" | "year"`.
|
|
591
|
+
|
|
592
|
+
| Method | Fires at |
|
|
593
|
+
| -------------------- | ------------------------------------------------- |
|
|
594
|
+
| `beginOf("day")` | 00:00 every day |
|
|
595
|
+
| `endOf("day")` | 23:59 every day |
|
|
596
|
+
| `beginOf("month")` | 1st of every month at 00:00 |
|
|
597
|
+
| `endOf("month")` | Last day of every month at 23:59 (**dynamic**) |
|
|
598
|
+
| `beginOf("year")` | January 1 at 00:00 every year |
|
|
599
|
+
| `endOf("year")` | December 31 at 23:59 every year |
|
|
600
|
+
|
|
601
|
+
**`endOf("month")` is dynamic** — recomputes per cycle, so a job defined in February (28 days) still fires on March 31, April 30, etc. Leap years pick Feb 29 correctly.
|
|
602
|
+
|
|
603
|
+
**`beginOf("year")` / `endOf("year")` lock the month** — always Jan 1 / Dec 31, never "1st/31st of whatever month the job was defined in."
|
|
604
|
+
|
|
605
|
+
## Cron is an alternative, not an addition
|
|
606
|
+
|
|
607
|
+
`.cron("…")` clears any prior interval/`at`/`on`/`beginOf`/`endOf` config — and vice versa. They're mutually exclusive. See [`@warlock.js/scheduler/schedule-with-cron/SKILL.md`](@warlock.js/scheduler/schedule-with-cron/SKILL.md).
|
|
608
|
+
|
|
609
|
+
## Chaining order doesn't matter (for the most part)
|
|
610
|
+
|
|
611
|
+
Every fluent method recomputes `nextRun` at the end. Putting `.at()` before `.daily()` produces the same schedule as putting `.daily()` before `.at()` — but for readability, the conventional order is **interval → day → time → timezone → execution options**:
|
|
612
|
+
|
|
613
|
+
```ts
|
|
614
|
+
job("good", fn)
|
|
615
|
+
.weekly() // interval
|
|
616
|
+
.on("monday") // day
|
|
617
|
+
.at("09:00") // time
|
|
618
|
+
.inTimezone("UTC") // timezone
|
|
619
|
+
.preventOverlap() // execution
|
|
620
|
+
.retry(3, 1000); // execution
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
## Inline registration — `scheduler.newJob()`
|
|
624
|
+
|
|
625
|
+
For one-liners, the scheduler exposes a `newJob()` shortcut that creates, registers, and returns the job in one call:
|
|
626
|
+
|
|
627
|
+
```ts
|
|
628
|
+
scheduler
|
|
629
|
+
.newJob("cleanup", cleanupFn)
|
|
630
|
+
.daily()
|
|
631
|
+
.at("03:00");
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
To register several pre-built jobs at once, `scheduler.addJobs([...])` is the batch counterpart to `addJob` (both are chainable and preserve insertion order):
|
|
635
|
+
|
|
636
|
+
```ts
|
|
637
|
+
scheduler.addJobs([
|
|
638
|
+
job("cleanup", cleanupFn).daily().at("03:00"),
|
|
639
|
+
job("reports", sendReports).weekly().on("monday").at("09:00"),
|
|
640
|
+
]);
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
If the scheduler is already running, every job added via `addJob` / `addJobs` is prepared on the spot (its `nextRun` is computed) so it fires on the next tick.
|
|
644
|
+
|
|
645
|
+
## Reading state at runtime
|
|
646
|
+
|
|
647
|
+
```ts
|
|
648
|
+
const j = scheduler.getJob("nightly-cleanup");
|
|
649
|
+
|
|
650
|
+
j?.nextRun?.toISOString(); // next scheduled run (Dayjs)
|
|
651
|
+
j?.lastRun?.toISOString(); // last attempt — success OR failure
|
|
652
|
+
j?.isRunning; // currently executing?
|
|
653
|
+
j?.intervals; // { every?, day?, dayOfMonthMode?, month?, time? } (readonly)
|
|
654
|
+
j?.cronExpression; // null if using fluent API
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
## See also
|
|
658
|
+
|
|
659
|
+
- [`@warlock.js/scheduler/schedule-with-cron/SKILL.md`](@warlock.js/scheduler/schedule-with-cron/SKILL.md) — when the fluent API isn't enough
|
|
660
|
+
- [`@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`](@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md) — `.retry()` and `.preventOverlap()` on the same job
|
|
661
|
+
- [`@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md) — `.inTimezone()`
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
## schedule-with-cron `@warlock.js/scheduler/schedule-with-cron/SKILL.md`
|
|
665
|
+
|
|
666
|
+
---
|
|
667
|
+
name: schedule-with-cron
|
|
668
|
+
description: 'Write `.cron(''…'')` expressions — 5-field syntax, operators (`*` `,` `-` `/`), Vixie OR semantics for DOM / DOW, `parseCron()` preview utility. Triggers: `.cron`, `parseCron`, `CronParser`, `*/5 * * * *`, `0 9 * * 1-5`; "write a cron expression", "every weekday at 9am cron", "preview next cron run", "migrate crontab(5) entry"; typical import `import { parseCron } from "@warlock.js/scheduler"`. Skip: human-readable schedules — `@warlock.js/scheduler/schedule-fluently/SKILL.md`; timezone — `@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`; competing libs `node-cron`, `cron`, `croner`, `cronstrue`.'
|
|
669
|
+
---
|
|
670
|
+
|
|
671
|
+
# Cron expressions
|
|
672
|
+
|
|
673
|
+
The escape hatch when the fluent API can't express a schedule. Activating `.cron()` clears any prior fluent config — the two are mutually exclusive.
|
|
674
|
+
|
|
675
|
+
## 5-field syntax
|
|
676
|
+
|
|
677
|
+
```
|
|
678
|
+
┌───────────── minute (0-59)
|
|
679
|
+
│ ┌───────────── hour (0-23)
|
|
680
|
+
│ │ ┌───────────── day-of-month (1-31)
|
|
681
|
+
│ │ │ ┌───────────── month (1-12)
|
|
682
|
+
│ │ │ │ ┌───────────── day-of-week (0-6, Sunday = 0)
|
|
683
|
+
│ │ │ │ │
|
|
684
|
+
* * * * *
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
**Note:** Sunday is `0` only. Some cron dialects also accept `7` for Sunday — this parser does NOT.
|
|
688
|
+
|
|
689
|
+
## Operators
|
|
690
|
+
|
|
691
|
+
| Syntax | Meaning | Example |
|
|
692
|
+
| --------- | ----------------------------- | ---------------- |
|
|
693
|
+
| `*` | Any value (full range) | `* * * * *` |
|
|
694
|
+
| `5` | Single value | `30 14 * * *` |
|
|
695
|
+
| `1,3,5` | List | `0,30 * * * *` |
|
|
696
|
+
| `1-5` | Inclusive range | `0 9-17 * * *` |
|
|
697
|
+
| `*/N` | Step over the wildcard | `*/5 * * * *` |
|
|
698
|
+
| `A-B/N` | Step over a range | `0 1-10/2 * * *` |
|
|
699
|
+
|
|
700
|
+
**Not supported (yet):** named tokens (`MON-FRI`, `JAN-DEC`), special strings (`@daily`, `@hourly`, `@reboot`), seconds field (6-field), Quartz modifiers (`L`, `W`, `#`).
|
|
701
|
+
|
|
702
|
+
## Day-of-month + day-of-week — Vixie OR semantics
|
|
703
|
+
|
|
704
|
+
When **both** `dayOfMonth` AND `dayOfWeek` are restricted (neither is `*`), the date matches if **either** constraint matches. When one is `*` and the other is restricted, only the restricted one matters.
|
|
705
|
+
|
|
706
|
+
```ts
|
|
707
|
+
// "1st of month OR any Monday" — fires on the 1st even if not a Monday,
|
|
708
|
+
// AND fires on any Monday even if not the 1st.
|
|
709
|
+
job("digest", sendDigest).cron("0 0 1 * 1");
|
|
710
|
+
|
|
711
|
+
// "15th of month" — DOW is *, so only DOM applies. Fires only on the 15th.
|
|
712
|
+
job("midmonth", task).cron("0 0 15 * *");
|
|
713
|
+
|
|
714
|
+
// "Every Monday" — DOM is *, so only DOW applies. Fires only on Mondays.
|
|
715
|
+
job("monday-only", task).cron("0 0 * * 1");
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
This matches Vixie cron (the de facto standard on Unix). Migrating an existing cron table from `crontab(5)` should "just work" semantically.
|
|
719
|
+
|
|
720
|
+
## Common recipes
|
|
721
|
+
|
|
722
|
+
```ts
|
|
723
|
+
// Every 5 minutes
|
|
724
|
+
.cron("*/5 * * * *")
|
|
725
|
+
|
|
726
|
+
// Top of every hour
|
|
727
|
+
.cron("0 * * * *")
|
|
728
|
+
|
|
729
|
+
// Every weekday at 9 AM
|
|
730
|
+
.cron("0 9 * * 1-5")
|
|
731
|
+
|
|
732
|
+
// 2:30 PM on the 15th of every month
|
|
733
|
+
.cron("30 14 15 * *")
|
|
734
|
+
|
|
735
|
+
// Every 2 hours, on the hour
|
|
736
|
+
.cron("0 */2 * * *")
|
|
737
|
+
|
|
738
|
+
// First day of each month at midnight
|
|
739
|
+
.cron("0 0 1 * *")
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
## Validation
|
|
743
|
+
|
|
744
|
+
`new CronParser(expr)` and `.cron(expr)` both throw at definition time on:
|
|
745
|
+
|
|
746
|
+
- Wrong field count (must be exactly 5)
|
|
747
|
+
- Non-numeric values, out-of-range values, inverted ranges (`5-1`)
|
|
748
|
+
- Step `<= 0`, non-numeric step
|
|
749
|
+
- Impossible day-of-month / month combinations — a date that can never occur, e.g. `0 0 30 2 *` (Feb 30) or `0 0 31 4 *` (April has 30 days). Rejected with an `Impossible cron expression` error.
|
|
750
|
+
|
|
751
|
+
```ts
|
|
752
|
+
new CronParser("0 0 30 2 *"); // throws: Impossible cron expression — Feb never has a 30th
|
|
753
|
+
new CronParser("0 0 31 4 *"); // throws: April has only 30 days
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
**Leap years and the OR escape hatch.** `0 0 29 2 *` (Feb 29) is **accepted** — it occurs in leap years. And when day-of-week is also restricted, Vixie OR semantics keep the schedule alive via the weekday path, so the combo is never rejected: `0 0 30 2 1` (Feb 30 OR any Monday) parses fine and fires on Mondays.
|
|
757
|
+
|
|
758
|
+
Throws are eager — bad expressions fail fast at construction, not at first tick.
|
|
759
|
+
|
|
760
|
+
## Standalone preview — `parseCron()`
|
|
761
|
+
|
|
762
|
+
The `CronParser` class is exported as a utility for ad-hoc next-run calculation, separate from any job:
|
|
763
|
+
|
|
764
|
+
```ts
|
|
765
|
+
import { parseCron } from "@warlock.js/scheduler";
|
|
766
|
+
import dayjs from "dayjs";
|
|
767
|
+
|
|
768
|
+
const parser = parseCron("0 9 * * 1-5");
|
|
769
|
+
|
|
770
|
+
parser.nextRun().toISOString(); // next weekday at 9 AM
|
|
771
|
+
parser.nextRun(dayjs("2026-12-31")).format(); // from a specific anchor
|
|
772
|
+
|
|
773
|
+
parser.matches(dayjs()); // is "now" a fire moment?
|
|
774
|
+
|
|
775
|
+
parser.fields; // parsed numeric arrays
|
|
776
|
+
parser.expression; // original string
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
Impossible expressions are rejected eagerly at construction (see Validation), so `nextRun(from?)` always resolves a satisfiable expression within a year — its one-year scan bound is just a defensive backstop, never the path that catches a bad expression.
|
|
780
|
+
|
|
781
|
+
## Timezone interaction
|
|
782
|
+
|
|
783
|
+
If the job has `.inTimezone(tz)`, the cron parser receives a timezone-aware `dayjs` object — so `0 9 * * *` with `.inTimezone("Asia/Tokyo")` fires at 09:00 JST. See [`@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md).
|
|
784
|
+
|
|
785
|
+
## See also
|
|
786
|
+
|
|
787
|
+
- [`@warlock.js/scheduler/schedule-fluently/SKILL.md`](@warlock.js/scheduler/schedule-fluently/SKILL.md) — the higher-level alternative for human-readable schedules
|
|
788
|
+
- [`@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md) — pinning a cron schedule to a wall-clock timezone
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
## scheduler-basics `@warlock.js/scheduler/scheduler-basics/SKILL.md`
|
|
792
|
+
|
|
793
|
+
---
|
|
794
|
+
name: scheduler-basics
|
|
795
|
+
description: 'Start with `@warlock.js/scheduler` — `job()` factory + `scheduler` singleton, 2-primitive surface, UTC default, factory-first API. Triggers: `job`, `scheduler`, `scheduler.addJob`, `scheduler.start`, `scheduler.newJob`; "schedule a recurring job", "how to use warlock scheduler", "where do I start"; typical import `import { job, scheduler } from "@warlock.js/scheduler"`. Skip: fluent — `@warlock.js/scheduler/schedule-fluently/SKILL.md`; cron — `@warlock.js/scheduler/schedule-with-cron/SKILL.md`; retry — `@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`; events — `@warlock.js/scheduler/observe-scheduler/SKILL.md`; timezone — `@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`; competing `bullmq`, `agenda`, `node-cron`; native `setInterval`.'
|
|
796
|
+
---
|
|
797
|
+
|
|
798
|
+
# Schedule recurring jobs
|
|
799
|
+
|
|
800
|
+
In-process recurring job scheduler. Built on `dayjs`. Two primitives, factory-first API, type-safe events.
|
|
801
|
+
|
|
802
|
+
> This skill is the scheduler **map** — read it first, then load the specific skill for the task.
|
|
803
|
+
|
|
804
|
+
## The 2-primitive surface
|
|
805
|
+
|
|
806
|
+
```
|
|
807
|
+
Job → the scheduled unit (factory: `job(name, callback)`)
|
|
808
|
+
Scheduler → the runtime / tick loop (singleton: `scheduler` | class: `new Scheduler()`)
|
|
809
|
+
```
|
|
810
|
+
|
|
811
|
+
A `Job` carries the schedule (interval or cron), retry config, overlap rule, timezone, and the callback. A `Scheduler` owns the tick loop, the registered jobs, the parallel/sequential execution mode, and emits lifecycle events. The exported `CronParser` is a utility for ad-hoc cron preview — not part of the scheduling flow.
|
|
812
|
+
|
|
813
|
+
## Install
|
|
814
|
+
|
|
815
|
+
```bash
|
|
816
|
+
yarn add @warlock.js/scheduler
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
## Foundations
|
|
820
|
+
|
|
821
|
+
The 9 things that are true in every scheduler use:
|
|
822
|
+
|
|
823
|
+
1. **Factory-first.** `import { job, scheduler } from "@warlock.js/scheduler"`. Users do not call `new Job()` directly (it works, but `job()` is the documented surface).
|
|
824
|
+
2. **Default timezone is UTC.** `daily().at("09:00")` fires at 09:00 UTC, regardless of the server's clock. Pin a job to wall-clock with `.inTimezone("America/New_York")`. See [`@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md).
|
|
825
|
+
3. **The scheduler awaits each tick.** Concurrent same-job runs from the scheduler's own loop are structurally impossible. `preventOverlap()` is for jobs that ALSO get invoked outside the loop. See [`@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`](@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md).
|
|
826
|
+
4. **Cron uses 5-field Vixie semantics.** When both `dayOfMonth` and `dayOfWeek` are restricted (neither is `*`), a date matches if EITHER constraint matches. Standard-cron compatible. See [`@warlock.js/scheduler/schedule-with-cron/SKILL.md`](@warlock.js/scheduler/schedule-with-cron/SKILL.md).
|
|
827
|
+
5. **`nextRun` always advances after a run — success OR failure.** A permanently failing job re-fires on its next scheduled slot, not on every tick.
|
|
828
|
+
6. **Validation throws at definition time.** `every(0)`, `at("24:00")`, `on(32)`, `retry(-1)`, malformed cron — all throw immediately when you wire the job, not at runtime.
|
|
829
|
+
7. **`Job.run()` returns a `JobResult`, never throws.** Errors funnel into `result.error`. The scheduler emits `job:error` after all retries are exhausted.
|
|
830
|
+
8. **Parallel mode is within a tick, not across.** Even with `runInParallel(true)`, the NEXT tick waits for the current tick's jobs to all settle.
|
|
831
|
+
9. **Tick interval is drift-compensated.** A tick that takes 200 ms is followed by an 800 ms delay so the cadence between tick *starts* averages `tickInterval`, not `tickInterval + work-time`.
|
|
832
|
+
|
|
833
|
+
## 30-second example
|
|
834
|
+
|
|
835
|
+
```ts
|
|
836
|
+
import { scheduler, job } from "@warlock.js/scheduler";
|
|
837
|
+
|
|
838
|
+
scheduler.on("job:error", (name, error) => logger.error({ name, error }));
|
|
839
|
+
|
|
840
|
+
scheduler.addJob(
|
|
841
|
+
job("nightly-cleanup", async () => {
|
|
842
|
+
await db.deleteExpiredTokens();
|
|
843
|
+
})
|
|
844
|
+
.daily()
|
|
845
|
+
.at("03:00")
|
|
846
|
+
.inTimezone("America/New_York")
|
|
847
|
+
.preventOverlap()
|
|
848
|
+
.retry(3, 1000)
|
|
849
|
+
);
|
|
850
|
+
|
|
851
|
+
scheduler.start();
|
|
852
|
+
|
|
853
|
+
process.on("SIGTERM", async () => {
|
|
854
|
+
await scheduler.shutdown(30_000);
|
|
855
|
+
process.exit(0);
|
|
856
|
+
});
|
|
857
|
+
```
|
|
858
|
+
|
|
859
|
+
## Pick a skill
|
|
860
|
+
|
|
861
|
+
| If the task is about… | Load |
|
|
862
|
+
| --- | --- |
|
|
863
|
+
| Building schedules via `every*`/`daily`/`weekly`/`monthly`/`at`/`on`/`beginOf`/`endOf` | [`@warlock.js/scheduler/schedule-fluently/SKILL.md`](@warlock.js/scheduler/schedule-fluently/SKILL.md) |
|
|
864
|
+
| Writing `.cron("…")` expressions, debugging DOM/DOW behavior, using `parseCron()` for preview | [`@warlock.js/scheduler/schedule-with-cron/SKILL.md`](@warlock.js/scheduler/schedule-with-cron/SKILL.md) |
|
|
865
|
+
| Configuring `.retry()` / exponential backoff, `.preventOverlap()`, understanding failure rescheduling | [`@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md`](@warlock.js/scheduler/configure-retry-and-overlap/SKILL.md) |
|
|
866
|
+
| Subscribing to scheduler events, reading `JobResult`, lifecycle, graceful shutdown | [`@warlock.js/scheduler/observe-scheduler/SKILL.md`](@warlock.js/scheduler/observe-scheduler/SKILL.md) |
|
|
867
|
+
| Per-job `.inTimezone()`, multi-region patterns, DST handling | [`@warlock.js/scheduler/pin-schedule-timezone/SKILL.md`](@warlock.js/scheduler/pin-schedule-timezone/SKILL.md) |
|
|
868
|
+
|
|
869
|
+
## When NOT to use this skill
|
|
870
|
+
|
|
871
|
+
- Jobs imported from `bullmq`, `agenda`, `node-cron`, etc. — those are different libraries.
|
|
872
|
+
- Long-running queue workers (consumer processes) — this is a scheduler, not a queue.
|
|
873
|
+
- One-off "run this at a specific date once" — currently not supported (on the backlog as `runAt()`).
|
|
874
|
+
- Multi-replica deployments needing leader election — also on the backlog (distributed locking).
|
|
875
|
+
|
|
876
|
+
## Package structure
|
|
877
|
+
|
|
878
|
+
```
|
|
879
|
+
@warlock.js/scheduler
|
|
880
|
+
src/
|
|
881
|
+
index.ts — barrel: Scheduler, scheduler, Job, job, CronParser, parseCron, types
|
|
882
|
+
scheduler.ts — Scheduler class + default singleton
|
|
883
|
+
job.ts — Job class + job() factory
|
|
884
|
+
cron-parser.ts — CronParser class + parseCron() factory
|
|
885
|
+
types.ts — TimeType, Day, JobIntervals, JobResult, RetryConfig, SchedulerEvents
|
|
886
|
+
```
|
|
887
|
+
|
|
888
|
+
|