@stonyx/cron 0.2.1-alpha.6 → 0.2.1-alpha.61

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 CHANGED
@@ -35,20 +35,79 @@ When a job is executed, its next trigger time is updated, and it is re-inserted
35
35
  | `register` | `key: string, callback: Function, interval: number, runOnInit?: boolean` | Register a new job with a given interval in seconds. If `runOnInit` is true, the job runs immediately upon registration. |
36
36
  | `unregister` | `key: string` | Remove a previously registered job. |
37
37
 
38
+ > **Callback semantics.** Callbacks are invoked fire-and-forget: `Cron` never waits for one to settle, and reschedules a job *before* invoking it. Two *different* jobs that fall due on the same tick may therefore overlap.
39
+ >
40
+ > A job that is still running when it next falls due is skipped — and **keeps** being skipped until that invocation settles. `Cron` provides no timeout by design, so **bounding your own callback is your responsibility**: a promise that never settles means that job never runs again for the lifetime of the process, even though the scheduler stays healthy and the job stays visible in `jobs` and in the heap. Other jobs are unaffected.
41
+ >
42
+ > One warning is emitted per stuck run (not per tick), including how long the invocation has been running. That warning goes to `log.warn` and is **not** gated by `config.cron.log` — a dropped execution reported on a channel a config flag can silence would be indistinguishable from a healthy scheduler.
43
+ >
44
+ > The same-job guarantee holds for the lifetime of a **registration**, not of a key: `unregister` followed by `register` on a key whose invocation is still in flight builds a fresh job object with a fresh guard, so the replacement can run alongside the abandoned invocation. That is also the only way to recover a permanently stuck job.
45
+ >
46
+ > Synchronous throws and asynchronous rejections are both caught and reported through `log.error`, with the error's stack interpolated into the message. Neither can stop the scheduler. Note that a rejection which previously escaped `register()` as an unhandled rejection — process-fatal under Node's default — is now swallowed into `log.error`.
47
+
38
48
  > `MinHeap` is also exported as a public subpath (`@stonyx/cron/min-heap`) and can be imported directly for advanced usage.
39
49
 
50
+ ## CronService
51
+
52
+ The default export above is `Cron`: a fire-and-forget interval registry. `@stonyx/cron/service` is a separate, heavier class for jobs that need CRUD, persistence, a run log and error backoff. It is not a drop-in replacement and the two do not share a scheduler.
53
+
54
+ The two classes agree on the guarantee — the same job is never run concurrently with itself, and different jobs may overlap — but not on the mechanism or on what you can observe. `Cron` invokes callbacks fire-and-forget and reports a skipped run only as an ungated `log.warn` line, one per stuck run, never as a value. `CronService` **awaits** `onJobDue`, its return value shapes `status`/`error`/`summary`, and a refused run comes back to the caller as a value that no log setting can suppress — it is not logged. The two therefore agree on one more thing than the contrast suggests: neither can be made silent by `config.cron.log`.
55
+
56
+ ```js
57
+ import CronService from '@stonyx/cron/service';
58
+
59
+ const service = new CronService();
60
+ service.onJobDue = async (job) => ({ status: 'ok', summary: 'done' });
61
+
62
+ await service.start();
63
+ const job = await service.add({ name: 'Nightly', schedule: { kind: 'every', everyMs: 86_400_000 }, payload: { kind: 'agentTurn', message: 'go' } });
64
+
65
+ const result = await service.run(job.id, 'force');
66
+ ```
67
+
68
+ ### The `run()` contract
69
+
70
+ `run(id, mode)` resolves with an `ExecuteResult`. It **never** invokes the callback twice for one job, and it will refuse rather than queue:
71
+
72
+ | `status` | `reason` | Meaning |
73
+ | :---------- | :------------------ | :----------------------------------------------------------------------------- |
74
+ | `'ok'` | — | The callback resolved. `summary` and `durationMs` are set. |
75
+ | `'error'` | — | The callback threw or rejected. `error` carries the message; backoff is applied. |
76
+ | `'skipped'` | `'not due'` | `mode` was `'due'` and the job's next run time has not arrived. Use `'force'` to run anyway. |
77
+ | `'skipped'` | `'already running'` | A previous invocation of **this** job has not settled. The call is refused, not queued, and nothing is logged. |
78
+ | `'skipped'` | `'removed'` | The job was removed between the lookup and the claim. The callback did not fire. |
79
+
80
+ `run()` throws (rather than returning a result) when `id` is not a registered job: `Error: Job not found: <id>`.
81
+
82
+ **Concurrency.** A job is bounded to one in-flight invocation on every path — manual `run()` and the timer both claim it first. **Different** jobs are not bounded: the callback is deliberately invoked outside the internal lock, so N concurrent `run()` calls on N distinct jobs produce N concurrent callbacks. The scheduler itself never generates that fan-out (its timer path invokes a due batch sequentially); only a caller can. If you drive `run()` from a request handler, bound it on your side. Taking the callback out of the lock is what stops a callback that never settles from blocking `add`/`update`/`remove`; restoring the bound by putting it back would restore that deadlock.
83
+
84
+ ### Breaking changes in this line
85
+
86
+ Four consumer-visible changes landed with the phase split (#34). All are measured against the emitted `dist/service.d.ts`:
87
+
88
+ 1. **`ExecuteResult.reason` narrowed** from `string` to `'not due' | 'already running' | 'removed'`, and gained the `'removed'` member. Comparing it against a literal outside the union, or `switch`ing on one, is now a compile error (`TS2367` / `TS2678`). Assigning it into `string | undefined` and spreading it are unaffected. The type is exported as `SkipReason`.
89
+ 2. **`CronService` is nominally typed.** It carries ECMAScript hard-private members, so the declarations emit `#private;` and a structurally hand-built test double no longer assigns to `CronService` (`TS2741: Property '#private' is missing`). The break is one-directional: `class X extends CronService` still compiles, and assigning a real `CronService` to your own hand-written interface still compiles. **Migration:** declare your own interface and depend on that instead of a `CronService`-typed mock.
90
+ 3. **`claimJob`, `settleJob` and `executeClaimed` are not published.** They were never a supported API; a claim taken without its matching settle strands the job permanently.
91
+ 4. **`run()` no longer serializes across jobs** — see the concurrency note above.
92
+
93
+ `SkipReason`, `ExecuteResult`, `JobDueResult`, `ServiceStatus`, `ListOptions` and `OnJobDueCallback` are all exported from `@stonyx/cron/service`, so an exhaustive handler over `reason` is expressible.
94
+
40
95
  ## Configuration
41
96
 
42
- Optionally, logging and debugging can be enabled through `config.cron`:
97
+ Optionally, informational logging and debugging can be controlled through `config.cron`:
43
98
 
44
99
  ```js
45
100
  config.cron = {
46
- log: true // enable cron job logs
101
+ log: true // informational cron job logs; defaults to true
47
102
  };
48
103
 
49
104
  config.debug = true; // optional: debug logs for job registration and execution
50
105
  ```
51
106
 
107
+ `config.cron.log` gates **informational** messages only. Error reports and the
108
+ stuck-job warning described above are never gated by it, so setting it to `false`
109
+ cannot make a dropped execution silent.
110
+
52
111
  ## License
53
112
 
54
113
  Apache — do what you want, just keep attribution.
package/dist/main.d.ts CHANGED
@@ -3,6 +3,24 @@ interface CronJob extends HeapItem {
3
3
  callback: () => void | Promise<void>;
4
4
  interval: string;
5
5
  key: string;
6
+ /**
7
+ * Timestamp (ms) at which the current invocation started; `undefined` when the
8
+ * job is idle. Optional so the emitted `CronJob` stays assignable from a job
9
+ * object built by a consumer — `jobs`, `heap` and `setNextTrigger` all expose
10
+ * this interface structurally, so a required field is a breaking type change.
11
+ *
12
+ * A timestamp rather than a boolean, mirroring `job.state.runningAtMs` in the
13
+ * service tier (`markRunning` / `applyResult` / `isDue` in `src/job.ts`), and
14
+ * carrying the one fact a stuck-job warning needs: how long it has been stuck.
15
+ * `CronService.running` is a class-level re-entrancy flag and a different
16
+ * concept; reusing that word here would collide.
17
+ */
18
+ runningAtMs?: number;
19
+ /**
20
+ * True once a skip has been reported for the *current* invocation. Bounds the
21
+ * still-running warning to one line per stuck run instead of one per tick.
22
+ */
23
+ skipReported?: boolean;
6
24
  }
7
25
  export default class Cron {
8
26
  static instance: Cron | null;
@@ -10,10 +28,38 @@ export default class Cron {
10
28
  heap: MinHeap<CronJob>;
11
29
  timer: ReturnType<typeof setTimeout> | null;
12
30
  constructor();
31
+ init(): Promise<void>;
13
32
  scheduleNextRun(): void;
14
33
  runDueJobs(): Promise<void>;
15
34
  register(key: string, callback: () => void | Promise<void>, interval: string, runOnInit?: boolean): void;
16
35
  unregister(key: string): void;
36
+ /**
37
+ * The one place this class invokes a consumer callback.
38
+ *
39
+ * Never blocks the caller, catches synchronous throws and asynchronous
40
+ * rejections identically, and skips the invocation entirely while the job's
41
+ * previous invocation has not settled (fire-and-forget would otherwise let a
42
+ * slow job stack invocations on itself).
43
+ *
44
+ * Everything that touches the callback — including the thenable probe and the
45
+ * handler attachment — is inside the `try`. A callback may return an object
46
+ * whose `then` is a throwing getter, and reading it outside the guard would
47
+ * abort the drain loop before `scheduleNextRun()`, which is defect #36 again.
48
+ */
49
+ invokeJob(job: CronJob, runOnInit?: boolean): void;
50
+ /**
51
+ * Report a scheduler-level message without ever letting the logger's own
52
+ * failure reach the caller.
53
+ *
54
+ * `@stonyx/logs` convenience methods return a promise and write to disk
55
+ * through an unguarded `mkdirSync` + `fsp.appendFile`. On a read-only or full
56
+ * log volume that promise rejects; an unobserved rejection raised from inside
57
+ * the handler that exists to prevent unhandled rejections would re-create
58
+ * exactly the defect this class was fixed for.
59
+ */
60
+ report(level: 'error' | 'warn', message: string): void;
61
+ /** Release a job's in-flight guard. Only ever called for the job it belongs to. */
62
+ release(job: CronJob): void;
17
63
  setNextTrigger(job: CronJob): void;
18
64
  log(text: string, key?: string | null): void;
19
65
  }
package/dist/main.js CHANGED
@@ -17,6 +17,68 @@ import config from 'stonyx/config';
17
17
  import log from 'stonyx/log';
18
18
  import { getTimestamp } from '@stonyx/utils/date';
19
19
  import MinHeap from './min-heap.js';
20
+ /** Longest error text that may reach a log line. Anything past this is truncated. */
21
+ const MAX_LOGGED_ERROR_LENGTH = 512;
22
+ /**
23
+ * Flatten a value for interpolation into a single log line.
24
+ *
25
+ * `@stonyx/logs` writes `${timestamp} ${content}\n` to a newline-delimited
26
+ * file, so any `\r` or `\n` inside `content` ends the record early and
27
+ * everything after it is read back as a separate entry — including a forged
28
+ * `[timestamp] ...` prefix that is indistinguishable from a real one. Newlines
29
+ * become the literal two characters so the content survives for a reader, and
30
+ * the length cap keeps one pathological value from swamping the file.
31
+ *
32
+ * Kept byte-identical to the `forLog` landing in `src/service.ts` on #34: the
33
+ * two tiers render the same untrusted values into the same log file, and a
34
+ * reader diagnosing a forged record should not have to know which tier wrote
35
+ * it. Duplicated rather than shared because the two land on separate branches;
36
+ * folding them into one helper is a follow-up once both are on `dev`, not a
37
+ * cross-PR dependency that would make either unmergeable alone.
38
+ */
39
+ function forLog(value, maxLength) {
40
+ const flattened = value.replace(/\r\n|[\r\n\u2028\u2029]/g, '\\n');
41
+ return flattened.length > maxLength ? `${flattened.slice(0, maxLength)}...` : flattened;
42
+ }
43
+ /**
44
+ * Render an unknown thrown value as log text. Total by construction.
45
+ *
46
+ * `@stonyx/logs` reads a second argument as `logToFile`, not as a format
47
+ * argument, so `log.error(message, err)` discards the error entirely *and*
48
+ * forces a disk write on every failure. The error has to be interpolated into
49
+ * the message instead — the shape `CronService.executeJob` already uses.
50
+ *
51
+ * Every read below touches a consumer-controlled value and can therefore throw:
52
+ * `instanceof` runs a proxy's `getPrototypeOf` trap, `stack`/`name`/`message`
53
+ * can be accessor properties, and `String(Object.create(null))` throws outright.
54
+ * This function runs *inside* `invokeJob`'s catch — the one place whose job is
55
+ * to stop a callback failure from reaching the scheduler — so a throw here
56
+ * escapes that catch and skips `scheduleNextRun()`, which is defect #36.
57
+ */
58
+ function describeError(err) {
59
+ try {
60
+ if (err instanceof Error) {
61
+ return forLog(err.stack ?? `${err.name}: ${err.message}`, MAX_LOGGED_ERROR_LENGTH);
62
+ }
63
+ return forLog(String(err), MAX_LOGGED_ERROR_LENGTH);
64
+ }
65
+ catch {
66
+ // Deliberately not re-entrant: describing the failure to describe the error
67
+ // would be the same read that just threw.
68
+ return '<thrown value could not be rendered>';
69
+ }
70
+ }
71
+ /**
72
+ * Render a consumer-supplied job key safely for log output.
73
+ *
74
+ * Keys reach the log verbatim, so a key containing a newline can forge a
75
+ * complete, well-formed log line (`'a:\n[FORGED] Cron::admin - all jobs
76
+ * healthy'`). `JSON.stringify` quotes the value and escapes the control
77
+ * characters, which is also how the key is rendered one tier up.
78
+ */
79
+ function describeKey(key) {
80
+ return JSON.stringify(key);
81
+ }
20
82
  export default class Cron {
21
83
  static instance;
22
84
  jobs = {};
@@ -27,6 +89,12 @@ export default class Cron {
27
89
  return Cron.instance;
28
90
  Cron.instance = this;
29
91
  }
92
+ async init() {
93
+ // Self-register so log.cron works even when @stonyx/cron is in the
94
+ // consumer's `dependencies` (stonyx loader only merges devDependencies).
95
+ const { logColor = '#888', logMethod = 'cron' } = config.cron ?? {};
96
+ log.defineType(logMethod, logColor);
97
+ }
30
98
  scheduleNextRun() {
31
99
  if (this.timer)
32
100
  clearTimeout(this.timer);
@@ -37,28 +105,41 @@ export default class Cron {
37
105
  if (!nextJob)
38
106
  return;
39
107
  const delay = Math.max(0, nextJob.nextTrigger - getTimestamp()) * 1000;
40
- this.timer = setTimeout(() => this.runDueJobs(), delay);
108
+ // Terminal catch: `runDueJobs` is async, so anything that escapes it would
109
+ // otherwise become an unhandled rejection raised from a bare timer callback
110
+ // — the very failure mode this class was fixed for.
111
+ this.timer = setTimeout(() => {
112
+ this.runDueJobs().catch((err) => {
113
+ this.report('error', `Cron scheduler tick failed: ${describeError(err)}`);
114
+ });
115
+ }, delay);
41
116
  }
42
117
  async runDueJobs() {
43
118
  const now = getTimestamp();
44
119
  const { heap } = this;
45
- while (!heap.isEmpty()) {
46
- const next = heap.peek();
47
- if (!next || next.nextTrigger > now)
48
- break;
49
- const job = heap.pop();
50
- if (config.debug)
51
- this.log('job has been triggered', job.key);
52
- try {
53
- await job.callback();
120
+ // `finally`, not a trailing statement: `scheduleNextRun()` running on every
121
+ // exit from the drain loop is the invariant this whole fix is about. If
122
+ // anything in the loop body ever throws, the scheduler must still re-arm
123
+ // rather than stopping silently while `timer` still holds a fired handle.
124
+ try {
125
+ while (!heap.isEmpty()) {
126
+ const next = heap.peek();
127
+ if (!next || next.nextTrigger > now)
128
+ break;
129
+ const job = heap.pop();
130
+ if (config.debug)
131
+ this.log('job has been triggered', job.key);
132
+ // Reschedule before invoking: a consumer callback is never awaited here,
133
+ // so a callback that hangs or rejects can no longer starve the drain loop
134
+ // or leave the job orphaned outside the heap.
135
+ this.setNextTrigger(job);
136
+ heap.push(job);
137
+ this.invokeJob(job);
54
138
  }
55
- catch (err) {
56
- log.error(`Cron job "${job.key}" failed:`, err);
57
- }
58
- this.setNextTrigger(job);
59
- heap.push(job);
60
139
  }
61
- this.scheduleNextRun();
140
+ finally {
141
+ this.scheduleNextRun();
142
+ }
62
143
  }
63
144
  register(key, callback, interval, runOnInit = false) {
64
145
  const job = { callback, interval, key, nextTrigger: 0 };
@@ -68,15 +149,18 @@ export default class Cron {
68
149
  if (config.debug) {
69
150
  this.log(`job has been registered with interval: ${interval}`, key);
70
151
  }
71
- if (runOnInit) {
72
- try {
73
- callback();
74
- }
75
- catch (err) {
76
- log.error(`Cron job "${key}" failed on init:`, err);
77
- }
152
+ // `finally`, not a trailing statement, for the same reason as `runDueJobs`:
153
+ // a job that is registered but never scheduled is defect #36's terminal
154
+ // state reached through the other entry point. `invokeJob` is total, so
155
+ // this guard should be unreachable — which is exactly why it is a guard and
156
+ // not an assumption.
157
+ try {
158
+ if (runOnInit)
159
+ this.invokeJob(job, true);
160
+ }
161
+ finally {
162
+ this.scheduleNextRun();
78
163
  }
79
- this.scheduleNextRun();
80
164
  }
81
165
  unregister(key) {
82
166
  const { heap, jobs } = this;
@@ -89,13 +173,104 @@ export default class Cron {
89
173
  this.log('job has been unregistered', key);
90
174
  this.scheduleNextRun();
91
175
  }
176
+ /**
177
+ * The one place this class invokes a consumer callback.
178
+ *
179
+ * Never blocks the caller, catches synchronous throws and asynchronous
180
+ * rejections identically, and skips the invocation entirely while the job's
181
+ * previous invocation has not settled (fire-and-forget would otherwise let a
182
+ * slow job stack invocations on itself).
183
+ *
184
+ * Everything that touches the callback — including the thenable probe and the
185
+ * handler attachment — is inside the `try`. A callback may return an object
186
+ * whose `then` is a throwing getter, and reading it outside the guard would
187
+ * abort the drain loop before `scheduleNextRun()`, which is defect #36 again.
188
+ */
189
+ invokeJob(job, runOnInit = false) {
190
+ const { key } = job;
191
+ const context = runOnInit ? 'failed on init:' : 'failed:';
192
+ // The in-flight guard lives on the job object, not in a module-level set
193
+ // keyed by string. Object identity is invocation identity: the only thing
194
+ // that clears the guard is the settle handler of the invocation that set it,
195
+ // and that handler closes over this exact job object, so a stale handler can
196
+ // never release a later invocation's guard.
197
+ if (job.runningAtMs !== undefined) {
198
+ // Bounded to one line per stuck run, not one per tick. A permanently hung
199
+ // job is re-pushed and re-skipped every interval forever; at the 1s
200
+ // interval this class's own tests use that measures 43,200 lines/day per
201
+ // job — a disk-fill and ingest-cost vector whose natural operator response
202
+ // is to silence the only signal that the job is dead.
203
+ if (!job.skipReported) {
204
+ job.skipReported = true;
205
+ const runningForSeconds = Math.max(0, Math.round((Date.now() - job.runningAtMs) / 1000));
206
+ // Ungated, deliberately, matching the sibling `CronService` handler. A
207
+ // skipped run is a *lost* execution, and `runDueJobs`/`register` both
208
+ // return `void`, so this is the legacy class's only wedged-job channel.
209
+ // Routing it through `this.log` would put it behind `config.cron.log`,
210
+ // where a permanently dead job is indistinguishable from a healthy one.
211
+ this.report('warn', `Cron job ${describeKey(key)} is still running after ${runningForSeconds}s; skipping this `
212
+ + 'tick and any further ticks until it settles (this warning is not repeated for this run)');
213
+ }
214
+ return;
215
+ }
216
+ job.runningAtMs = Date.now();
217
+ job.skipReported = false;
218
+ try {
219
+ const result = job.callback();
220
+ if (result && typeof result.then === 'function') {
221
+ Promise.resolve(result)
222
+ .catch((err) => {
223
+ // Braces matter: returning `report`'s value would put it back into
224
+ // the chain, and `.finally` passes a rejection straight through.
225
+ this.report('error', `Cron job ${describeKey(key)} ${context} ${describeError(err)}`);
226
+ })
227
+ .finally(() => { this.release(job); })
228
+ // Backstop: a throw inside the error handler or the release must not
229
+ // re-create the unhandled rejection this helper exists to prevent.
230
+ .catch(() => { });
231
+ return;
232
+ }
233
+ this.release(job);
234
+ }
235
+ catch (err) {
236
+ this.release(job);
237
+ this.report('error', `Cron job ${describeKey(key)} ${context} ${describeError(err)}`);
238
+ }
239
+ }
240
+ /**
241
+ * Report a scheduler-level message without ever letting the logger's own
242
+ * failure reach the caller.
243
+ *
244
+ * `@stonyx/logs` convenience methods return a promise and write to disk
245
+ * through an unguarded `mkdirSync` + `fsp.appendFile`. On a read-only or full
246
+ * log volume that promise rejects; an unobserved rejection raised from inside
247
+ * the handler that exists to prevent unhandled rejections would re-create
248
+ * exactly the defect this class was fixed for.
249
+ */
250
+ report(level, message) {
251
+ try {
252
+ const result = level === 'error' ? log.error(message) : log.warn(message);
253
+ void Promise.resolve(result).catch(() => { });
254
+ }
255
+ catch {
256
+ // Nowhere left to report to; the logger must never stop the scheduler.
257
+ }
258
+ }
259
+ /** Release a job's in-flight guard. Only ever called for the job it belongs to. */
260
+ release(job) {
261
+ job.runningAtMs = undefined;
262
+ job.skipReported = false;
263
+ }
92
264
  setNextTrigger(job) {
93
265
  job.nextTrigger = getTimestamp() + parseInt(job.interval, 10);
94
266
  }
95
267
  log(text, key = null) {
96
268
  if (!config.cron?.log)
97
269
  return;
98
- const tag = key ? `Cron::${key}` : `Cron`;
270
+ // The key is consumer-controlled and reaches the log verbatim. Strip the
271
+ // line terminators so a key cannot forge a second, well-formed log line;
272
+ // the surrounding format is unchanged.
273
+ const tag = key ? `Cron::${key.replace(/[\r\n]+/g, ' ')}` : `Cron`;
99
274
  log.cron(`${tag} - ${text}:`);
100
275
  }
101
276
  }
package/dist/service.d.ts CHANGED
@@ -4,29 +4,37 @@ import RunLog from './run-log.js';
4
4
  interface HeapEntry extends HeapItem {
5
5
  key: string;
6
6
  }
7
- interface JobDueResult {
7
+ /**
8
+ * Why a `run()` did not invoke the callback. Exported so a consumer can write a
9
+ * total handler over it: the union is closed and narrowed (it was `string`
10
+ * before #34), so an exhaustive `switch` is now both possible and expected.
11
+ */
12
+ export type SkipReason = 'not due' | 'already running' | 'removed';
13
+ export interface JobDueResult {
8
14
  status?: string;
9
15
  error?: string;
10
16
  summary?: string;
11
17
  }
12
- interface ExecuteResult {
18
+ export interface ExecuteResult {
13
19
  status: string;
14
20
  error?: string;
15
21
  summary?: string;
16
22
  durationMs?: number;
17
23
  deleted?: boolean;
18
- reason?: string;
24
+ /** Only set when `status` is `'skipped'`. */
25
+ reason?: SkipReason;
19
26
  }
20
- interface ServiceStatus {
27
+ export interface ServiceStatus {
21
28
  started: boolean;
22
29
  jobCount: number;
23
30
  nextWakeAtMs: number | undefined;
24
31
  }
25
- interface ListOptions {
32
+ export interface ListOptions {
26
33
  includeDisabled?: boolean;
27
34
  }
28
- type OnJobDueCallback = (job: Job) => Promise<JobDueResult | void> | JobDueResult | void;
35
+ export type OnJobDueCallback = (job: Job) => Promise<JobDueResult | void> | JobDueResult | void;
29
36
  export default class CronService {
37
+ #private;
30
38
  jobs: Map<string, Job>;
31
39
  heap: MinHeap<HeapEntry>;
32
40
  timer: ReturnType<typeof setTimeout> | null;
@@ -36,7 +44,38 @@ export default class CronService {
36
44
  onJobDue: OnJobDueCallback | null;
37
45
  constructor();
38
46
  /**
39
- * Start the service. Loads jobs from store (if any), arms timer.
47
+ * Start the service. Loads jobs from store (if any), arms timer. A no-op if
48
+ * already started.
49
+ *
50
+ * `initialJobs` crosses a serialization boundary — it is whatever the
51
+ * consumer's store handed back — so `Job[]` is a compile-time claim about
52
+ * runtime data. Three behaviours follow from that and are worth knowing
53
+ * before you call this, because all three are deliberate and two of them
54
+ * differ from a plain "load and arm":
55
+ *
56
+ * 1. WRITES TO `row.state`. A STALE claim (`state.runningAtMs` set by a
57
+ * process that is gone) is released, because nothing else ever will —
58
+ * there is no lease on the field (#35) — and left in place it is a job
59
+ * that is dead forever while `status()` reports it healthy. A LIVE claim,
60
+ * held by an invocation still running in this process, is left alone:
61
+ * releasing it would let the timer start a second concurrent invocation of
62
+ * a job that is already running.
63
+ *
64
+ * 2. THROWS on a row this class cannot use, rather than accepting it. A row
65
+ * whose `state` is missing, or frozen (`structuredClone` + `Object.freeze`
66
+ * is an ordinary defensive rehydration), throws out of `start()` where the
67
+ * caller's own `await` can catch it. The alternative is a `TypeError` from
68
+ * inside a bare timer callback later — an unhandled rejection, and
69
+ * process-fatal under Node's default.
70
+ *
71
+ * 3. ARMS THE TIMER EVEN IF IT THROWS. The rows loaded before the throw are
72
+ * registered and scheduled. Without this, a throw leaves `started: true`
73
+ * (so a retry is a no-op) with jobs in the heap and no timer: nothing ever
74
+ * fires and `status()` still reports healthy.
75
+ *
76
+ * Hand it deserialized rows and all three are invisible. Hand it live `Job`
77
+ * objects this service is currently executing and only 1 is observable, by
78
+ * design.
40
79
  */
41
80
  start(initialJobs?: Job[]): Promise<void>;
42
81
  /**
@@ -69,6 +108,37 @@ export default class CronService {
69
108
  remove(id: string): Promise<void>;
70
109
  /**
71
110
  * Manually trigger a job.
111
+ *
112
+ * Returns `{ status: 'skipped', reason }` without invoking the callback when
113
+ * the job is not due (`mode: 'due'`), is already in flight
114
+ * (`'already running'`), or was removed before the claim landed
115
+ * (`'removed'`). Before the phase split, a forced run against an in-flight
116
+ * job launched a second concurrent invocation.
117
+ *
118
+ * THROWS (rather than returning a skip) when `id` is not a registered job:
119
+ * `Error("Job not found: <id>")`. A job that disappears between this lookup
120
+ * and the claim is the `'removed'` skip above, not a throw — the two differ
121
+ * only by the timing of a race, and the second is a legitimate outcome
122
+ * whereas the first is a caller error.
123
+ *
124
+ * CONCURRENCY: the same job is bounded to one in-flight invocation on every
125
+ * path, and the timer path invokes due jobs one at a time. `run()` fan-out
126
+ * across DIFFERENT jobs is deliberately unbounded — N concurrent `run()`
127
+ * calls produce N concurrent consumer callbacks. Before the phase split
128
+ * these serialized behind the module-global lock; that serialization was the
129
+ * bug rather than the feature (one hung callback wedged every other caller),
130
+ * so it is not restored here. The fan-out is caller-driven and the scheduler
131
+ * never produces it on its own. A per-invoke bound belongs above this layer;
132
+ * it is tracked on stonyx-cron#35 alongside the execution timeout.
133
+ *
134
+ * NOT FIXED HERE: the phase split fixes the LOCK wedge, not the TIMER. A
135
+ * callback that never settles still stops `onTimer`'s sequential loop
136
+ * forever — `running` stays true, every later tick early-returns and re-arms,
137
+ * and the hung job's batch siblings stay claimed and off-heap having never
138
+ * been invoked. CRUD still resolves and `status()` still reports
139
+ * `started: true`, so that failure is now silent where it used to be loud.
140
+ * Bounding the callback and releasing batch siblings is stonyx-cron#35. Do
141
+ * not read this method's doc as "the hang is fixed".
72
142
  */
73
143
  run(id: string, mode?: 'due' | 'force'): Promise<ExecuteResult>;
74
144
  /**
@@ -78,6 +148,25 @@ export default class CronService {
78
148
  armTimer(): void;
79
149
  onTimer(): Promise<void>;
80
150
  findDueJobs(nowMs: number): Job[];
151
+ /**
152
+ * Execute a job in three phases:
153
+ *
154
+ * 1. claim (locked) — take ownership of the job, detach it from the heap
155
+ * 2. invoke (UNLOCKED) — await the consumer callback
156
+ * 3. settle (locked) — apply the result, log it, re-insert into the heap
157
+ *
158
+ * The critical section deliberately excludes phase 2. `onJobDue` is
159
+ * arbitrary, unbounded consumer code; awaiting it under the module-global
160
+ * lock is what wedged every subsequent `locked()` call (add/update/remove)
161
+ * when a callback never settled.
162
+ *
163
+ * `onTimer` performs the batch claim (`findDueJobs` + `markRunning`) for all
164
+ * due jobs under a single lock and then enters at phase 2 via
165
+ * `#executeClaimed`. That entry point is `#private` rather than a parameter
166
+ * on this method: as a published `alreadyClaimed` flag it would be a
167
+ * supported way to skip phase 1 entirely, defeating the claim guard and
168
+ * allowing concurrent `onJobDue` invocations for the same job.
169
+ */
81
170
  executeJob(job: Job): Promise<ExecuteResult>;
82
171
  removeFromHeap(id: string): void;
83
172
  log(message: string): void;
package/dist/service.js CHANGED
@@ -2,7 +2,21 @@
2
2
  * CronService - the main API for advanced job scheduling.
3
3
  *
4
4
  * Manages jobs in memory with a min-heap for efficient next-job lookup.
5
- * All state mutations are serialized via async locking.
5
+ *
6
+ * Async locking, but never around the consumer callback. Execution is split
7
+ * into three phases: claim (locked), invoke (UNLOCKED), settle (locked). Only
8
+ * phases 1 and 3 are serialized; the critical section deliberately excludes
9
+ * phase 2, so a consumer callback that never settles cannot wedge the lock
10
+ * chain and block add/update/remove. See `run()` and `#executeClaimed`.
11
+ *
12
+ * CONSUMER NOTE — this class is NOMINALLY typed. It carries ECMAScript hard-
13
+ * private members, so `dist/service.d.ts` emits `#private;` on the class and a
14
+ * structurally-built test double will not assign to `CronService`
15
+ * (`TS2741: Property '#private' is missing`). The break is one-directional and
16
+ * has a zero-cost workaround: `extends CronService` still compiles, and
17
+ * assigning a real `CronService` to your own hand-written interface still
18
+ * compiles. Declare your own interface and depend on that rather than
19
+ * hand-building a `CronService`-typed mock. See README's `CronService` section.
6
20
  */
7
21
  import config from 'stonyx/config';
8
22
  import log from 'stonyx/log';
@@ -12,6 +26,121 @@ import { locked } from './locked.js';
12
26
  import { normalizeJobInput, recoverFlatParams } from './normalize.js';
13
27
  import RunLog from './run-log.js';
14
28
  const MAX_TIMER_DELAY_MS = 60_000;
29
+ /** Longest error text that may reach a log line. Anything past this is truncated. */
30
+ const MAX_LOGGED_ERROR_LENGTH = 512;
31
+ /** Longest job name that may reach a log line. Anything past this is truncated. */
32
+ const MAX_LOGGED_NAME_LENGTH = 120;
33
+ /**
34
+ * Flatten a value for interpolation into a single log line.
35
+ *
36
+ * Chronicle writes `${timestamp} ${content}\n` to a newline-delimited file, so
37
+ * any `\r` or `\n` inside `content` ends the record early and everything after
38
+ * it is read back as a separate, attacker-shaped entry — including a forged
39
+ * `[timestamp] Cron — ...` prefix that is indistinguishable from a real one.
40
+ * Both values that reach these lines are untrusted: `job.name` is passed
41
+ * through `createJob` unvalidated and `normalize.ts` exists specifically to
42
+ * accept AI-shaped input, and an error message is arbitrary consumer-callback
43
+ * text. Newlines become the literal two characters so the content survives for
44
+ * a reader, and the length cap keeps one pathological value from swamping the
45
+ * file.
46
+ *
47
+ * TOTAL, for the same reason `describeError` below is total, and the parameter
48
+ * is coerced even though it is typed `string`. `job.name` is typed `string` but
49
+ * that is a compile-time claim about runtime data: `normalize.ts` only
50
+ * GENERATES a name when the field is falsy, so `add({ name: 12345 })` stores a
51
+ * number through the public API, and `start(initialJobs)` takes names verbatim
52
+ * from the consumer's store \u2014 the same untrusted boundary `start()` already
53
+ * hardens `state` against.
54
+ *
55
+ * Fixed HERE rather than by coercing in `normalize`, deliberately. Coercing at
56
+ * `normalize` closes the `add()` path only; the rehydration path bypasses both
57
+ * `normalize` and `createJob` entirely, and that is the path this class already
58
+ * treats as hostile. And this helper is on the ERROR path \u2014 a helper that
59
+ * throws while building an error report destroys the report it exists to
60
+ * produce. Measured pre-fix: `run()` rejected with `TypeError: value.replace is
61
+ * not a function` instead of returning an `ExecuteResult`, and on the timer
62
+ * path the failure record was swallowed entirely (0 records for a job that
63
+ * failed) because the throw happened inside the reporter's own `try`.
64
+ *
65
+ * `String(value)` alone is NOT enough: a value whose `toString` or
66
+ * `Symbol.toPrimitive` throws raises out of the coercion itself, so the `try`
67
+ * is load-bearing and not belt-and-braces. Kept typed `string` rather than
68
+ * widened to `unknown` so call sites still get compile-time pressure; the
69
+ * runtime coercion is the defence, not the signature.
70
+ */
71
+ function forLog(value, maxLength) {
72
+ let flattened;
73
+ try {
74
+ flattened = String(value).replace(/\r\n|[\r\n\u2028\u2029]/g, '\\n');
75
+ }
76
+ catch {
77
+ return '<unrenderable value>';
78
+ }
79
+ return flattened.length > maxLength ? `${flattened.slice(0, maxLength)}...` : flattened;
80
+ }
81
+ /**
82
+ * Describe a thrown value without ever throwing.
83
+ *
84
+ * `String(err)` is not total: a null-prototype object, or any object whose
85
+ * `toString`/`Symbol.toPrimitive` throws, raises "Cannot convert object to
86
+ * primitive value". Consumer callbacks throw arbitrary values, so the error
87
+ * handler must not become a second failure source of its own.
88
+ *
89
+ * The `instanceof Error` branch needs the same guard as the other one. `Error`
90
+ * is subclassable and `message` is a plain writable property, so a consumer can
91
+ * hand back an `Error` whose `message` is a getter that throws, or one that is
92
+ * an object whose `toString` throws — `Error.prototype.message` is typed
93
+ * `string`, so TypeScript sees nothing wrong and the coercion is deferred to
94
+ * the caller's template literal, outside every guard here. That made `run()`
95
+ * reject instead of returning an `ExecuteResult`: a contract violation in the
96
+ * function written to prevent exactly that. Reading and coercing `message`
97
+ * inside the `try` is what closes it.
98
+ *
99
+ * `Cron` in `main.ts` carries its own `describeError` and the two deliberately
100
+ * differ: it renders for a log line only, so it prefers `err.stack`; this one
101
+ * is also returned to the caller as `ExecuteResult.error` and persisted in a
102
+ * per-job run log, where a stack would be an unbounded blob in every stored
103
+ * failure. Recorded in `docs/architecture.md` under Code Patterns &
104
+ * Conventions -> Private Members -> "Two `describeError` helpers, deliberately
105
+ * not shared (#34 / #36)" — do not merge them into a shared helper without
106
+ * reading that first. NOT the `### Error Handling` section further down, which
107
+ * is about the legacy `Cron` and does not carry this decision.
108
+ */
109
+ function describeError(err) {
110
+ try {
111
+ return err instanceof Error ? String(err.message) : String(err);
112
+ }
113
+ catch {
114
+ return 'unknown error';
115
+ }
116
+ }
117
+ /**
118
+ * Job objects whose claim is held by an invocation still running IN THIS
119
+ * PROCESS. Added by phase 1, removed by phase 3 (or by the one hand-release in
120
+ * `#executeClaimed`), so membership is exactly "a settle is still coming".
121
+ *
122
+ * This exists so `start()` can tell a STALE claim from a LIVE one. Both look
123
+ * identical in `job.state.runningAtMs` — a number — but they need opposite
124
+ * treatment: a stale claim must be released (it is #34's permanently-dead job,
125
+ * and nothing reaps it) while a live one must be left alone (releasing it lets
126
+ * the timer launch a second concurrent invocation of a job that is already
127
+ * running, breaking the one invariant this class advertises).
128
+ *
129
+ * Keyed on the Job OBJECT, not the id, and module-level rather than per
130
+ * instance, for the two reasons that make those the only workable choices:
131
+ *
132
+ * - Object identity is what makes it correct across `CronService` instances.
133
+ * A second service handed live rows sees the same objects, so it inherits
134
+ * the answer rather than guessing; a per-instance set would report "not in
135
+ * flight" for a claim a sibling instance is holding. `locked()`'s chain is
136
+ * already module-global for the same reason.
137
+ * - Identity is also what makes it correct after a real crash. A restart
138
+ * deserializes its rows, so those are new objects and are never members —
139
+ * the #34 release still fires, which is the whole point of it.
140
+ *
141
+ * `WeakSet`, so a job that is removed and dropped mid-flight is not retained.
142
+ */
143
+ const inFlight = new WeakSet();
15
144
  export default class CronService {
16
145
  jobs;
17
146
  heap;
@@ -32,21 +161,114 @@ export default class CronService {
32
161
  }
33
162
  // -- Lifecycle -------------------------------------------------------
34
163
  /**
35
- * Start the service. Loads jobs from store (if any), arms timer.
164
+ * Start the service. Loads jobs from store (if any), arms timer. A no-op if
165
+ * already started.
166
+ *
167
+ * `initialJobs` crosses a serialization boundary — it is whatever the
168
+ * consumer's store handed back — so `Job[]` is a compile-time claim about
169
+ * runtime data. Three behaviours follow from that and are worth knowing
170
+ * before you call this, because all three are deliberate and two of them
171
+ * differ from a plain "load and arm":
172
+ *
173
+ * 1. WRITES TO `row.state`. A STALE claim (`state.runningAtMs` set by a
174
+ * process that is gone) is released, because nothing else ever will —
175
+ * there is no lease on the field (#35) — and left in place it is a job
176
+ * that is dead forever while `status()` reports it healthy. A LIVE claim,
177
+ * held by an invocation still running in this process, is left alone:
178
+ * releasing it would let the timer start a second concurrent invocation of
179
+ * a job that is already running.
180
+ *
181
+ * 2. THROWS on a row this class cannot use, rather than accepting it. A row
182
+ * whose `state` is missing, or frozen (`structuredClone` + `Object.freeze`
183
+ * is an ordinary defensive rehydration), throws out of `start()` where the
184
+ * caller's own `await` can catch it. The alternative is a `TypeError` from
185
+ * inside a bare timer callback later — an unhandled rejection, and
186
+ * process-fatal under Node's default.
187
+ *
188
+ * 3. ARMS THE TIMER EVEN IF IT THROWS. The rows loaded before the throw are
189
+ * registered and scheduled. Without this, a throw leaves `started: true`
190
+ * (so a retry is a no-op) with jobs in the heap and no timer: nothing ever
191
+ * fires and `status()` still reports healthy.
192
+ *
193
+ * Hand it deserialized rows and all three are invisible. Hand it live `Job`
194
+ * objects this service is currently executing and only 1 is observable, by
195
+ * design.
36
196
  */
37
197
  async start(initialJobs) {
38
198
  if (this.started)
39
199
  return;
40
200
  this.started = true;
41
- if (initialJobs) {
42
- for (const job of initialJobs) {
43
- this.jobs.set(job.id, job);
44
- if (job.enabled && job.state.nextRunAtMs) {
45
- this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
201
+ // `finally`, not a trailing statement. Reconciled with the same guard #53
202
+ // puts around `register`'s `runOnInit` invocation: a scheduler that is
203
+ // marked started but never armed is the terminal state both fixes exist to
204
+ // remove, reached here through the other entry point. `initialJobs` crosses
205
+ // a serialization boundary — it is whatever the consumer's store handed
206
+ // back — so `Job[]` is a compile-time claim about runtime data, and a row
207
+ // missing `state` throws mid-loop. Without this, `start()` leaves
208
+ // `started: true` (so it is now a no-op), the rows registered before the
209
+ // throw sitting in the heap, and NO timer: measured, `status()` then
210
+ // reports `{ started: true, jobCount: 1, nextWakeAtMs: <real> }` while
211
+ // nothing will ever fire. Silent and healthy-looking, again.
212
+ try {
213
+ if (initialJobs) {
214
+ for (const job of initialJobs) {
215
+ // A STALE `runningAtMs` records a claim taken by a process that is
216
+ // gone. Nothing will ever settle it, and nothing reaps it — there is
217
+ // no lease on the field (tracked on #35). Left in place it is a
218
+ // permanently dead job that still reports healthy: `isDue` returns
219
+ // false forever because of the flag, `run()` answers
220
+ // `'already running'` forever, `update()` never touches
221
+ // `state.runningAtMs`, and `status()` counts it like any other. The
222
+ // consumer's only recovery would be remove() + add(), losing the job
223
+ // id and its run history.
224
+ //
225
+ // Same hazard, same treatment as the hand-release on the `'removed'`
226
+ // path in `#executeClaimed`: a claim with no reachable settle must be
227
+ // released. Assigned directly rather than via `applyResult` for the same
228
+ // reason — this releases the claim and nothing else. The job did not
229
+ // run, so it gets no run-log row, no `lastStatus`, and no recomputed
230
+ // `nextRunAtMs`; it is rescheduled from the store's own value below.
231
+ //
232
+ // But NOT every `runningAtMs` here is stale, and the field cannot
233
+ // tell you which — it is a number either way. `start()` early-returns
234
+ // when `started`, so reaching this with a LIVE claim needs `stop()`
235
+ // then `start(sameObjects)` (an in-process restart against a store
236
+ // that hands back references rather than fresh rows) or a second
237
+ // `CronService` handed live rows. Measured on the unconditional
238
+ // version: the release cleared a live claim, the timer then found the
239
+ // job due, and one job got TWO concurrent in-flight callbacks. That
240
+ // is the single invariant this class advertises and that #34/#35
241
+ // exist to protect, so the release is guarded on `inFlight` —
242
+ // authoritative object identity, not a heuristic on the timestamp.
243
+ // See `inFlight`'s docblock for why identity is also what keeps the
244
+ // stale case working after a real crash.
245
+ //
246
+ // The guard is deliberately NOT a `job.state.runningAtMs` read.
247
+ // Guarding on THAT makes `start()` accept a row whose `state` is
248
+ // frozen — `structuredClone` + `Object.freeze` is an ordinary
249
+ // defensive rehydration — and that row is not usable by this class at
250
+ // all: `markRunning` writes the same field on every execution.
251
+ // Measured, that guard moves the failure from a throw out of
252
+ // `start()`, which the consumer's own `await` can catch, to a
253
+ // TypeError raised inside `onTimer`'s batch claim — a bare timer
254
+ // callback, so it surfaces as an unhandled rejection and is
255
+ // process-fatal under Node's default. `inFlight` does not have that
256
+ // problem: a deserialized row is never a member, so the write still
257
+ // happens and the frozen row still fails loudly at the boundary. Both
258
+ // properties are tested; do not collapse the two guards into one.
259
+ if (!inFlight.has(job)) {
260
+ job.state.runningAtMs = undefined;
261
+ }
262
+ this.jobs.set(job.id, job);
263
+ if (job.enabled && job.state.nextRunAtMs) {
264
+ this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
265
+ }
46
266
  }
47
267
  }
48
268
  }
49
- this.armTimer();
269
+ finally {
270
+ this.armTimer();
271
+ }
50
272
  }
51
273
  /**
52
274
  * Stop the service. Clears timer.
@@ -136,6 +358,37 @@ export default class CronService {
136
358
  }
137
359
  /**
138
360
  * Manually trigger a job.
361
+ *
362
+ * Returns `{ status: 'skipped', reason }` without invoking the callback when
363
+ * the job is not due (`mode: 'due'`), is already in flight
364
+ * (`'already running'`), or was removed before the claim landed
365
+ * (`'removed'`). Before the phase split, a forced run against an in-flight
366
+ * job launched a second concurrent invocation.
367
+ *
368
+ * THROWS (rather than returning a skip) when `id` is not a registered job:
369
+ * `Error("Job not found: <id>")`. A job that disappears between this lookup
370
+ * and the claim is the `'removed'` skip above, not a throw — the two differ
371
+ * only by the timing of a race, and the second is a legitimate outcome
372
+ * whereas the first is a caller error.
373
+ *
374
+ * CONCURRENCY: the same job is bounded to one in-flight invocation on every
375
+ * path, and the timer path invokes due jobs one at a time. `run()` fan-out
376
+ * across DIFFERENT jobs is deliberately unbounded — N concurrent `run()`
377
+ * calls produce N concurrent consumer callbacks. Before the phase split
378
+ * these serialized behind the module-global lock; that serialization was the
379
+ * bug rather than the feature (one hung callback wedged every other caller),
380
+ * so it is not restored here. The fan-out is caller-driven and the scheduler
381
+ * never produces it on its own. A per-invoke bound belongs above this layer;
382
+ * it is tracked on stonyx-cron#35 alongside the execution timeout.
383
+ *
384
+ * NOT FIXED HERE: the phase split fixes the LOCK wedge, not the TIMER. A
385
+ * callback that never settles still stops `onTimer`'s sequential loop
386
+ * forever — `running` stays true, every later tick early-returns and re-arms,
387
+ * and the hung job's batch siblings stay claimed and off-heap having never
388
+ * been invoked. CRUD still resolves and `status()` still reports
389
+ * `started: true`, so that failure is now silent where it used to be loud.
390
+ * Bounding the callback and releasing batch siblings is stonyx-cron#35. Do
391
+ * not read this method's doc as "the hang is fixed".
139
392
  */
140
393
  async run(id, mode = 'force') {
141
394
  const job = this.jobs.get(id);
@@ -144,6 +397,10 @@ export default class CronService {
144
397
  if (mode === 'due' && !isDue(job, Date.now())) {
145
398
  return { status: 'skipped', reason: 'not due' };
146
399
  }
400
+ // Deliberately NOT wrapped in locked(): executeJob takes the lock itself,
401
+ // for its claim and settle phases only. Wrapping here would re-create the
402
+ // wedge through a second door, because the consumer callback would once
403
+ // again be awaited while a lock is held.
147
404
  return this.executeJob(job);
148
405
  }
149
406
  /**
@@ -172,16 +429,78 @@ export default class CronService {
172
429
  }
173
430
  this.running = true;
174
431
  try {
175
- await locked(async () => {
432
+ // -- Phase 1: claim (locked), batched --
433
+ // Collecting due jobs pops them off the heap, and marking them running
434
+ // makes them un-claimable by anyone else. Both must happen under the
435
+ // same lock turn, or a concurrent run() could claim a job this batch has
436
+ // already detached.
437
+ //
438
+ // This is the SECOND claim implementation — `#claimJob` is the other, and
439
+ // the two reach the same state by different routes. `#claimJob` guards
440
+ // with an explicit `job.state.runningAtMs` check; this path has no such
441
+ // check and relies entirely on `isDue`'s `!job.state.runningAtMs` clause
442
+ // (`job.ts`) to keep `findDueJobs` from re-claiming a job that `run()`
443
+ // already holds. THAT CLAUSE IS LOAD-BEARING HERE, not an optimisation:
444
+ // drop it and the timer path silently double-invokes a job that `run()`
445
+ // is mid-flight on, while `run()` keeps refusing correctly and looks
446
+ // healthy. The one-in-flight-invocation-per-job invariant this class
447
+ // advertises holds by two independent guards in two files; a change to
448
+ // either has to be checked against the other.
449
+ const dueJobs = await locked(() => {
176
450
  const nowMs = Date.now();
177
- const dueJobs = this.findDueJobs(nowMs);
178
- for (const job of dueJobs) {
451
+ const due = this.findDueJobs(nowMs);
452
+ for (const job of due) {
179
453
  markRunning(job);
454
+ inFlight.add(job);
180
455
  }
181
- for (const job of dueJobs) {
182
- await this.executeJob(job);
183
- }
456
+ return due;
184
457
  });
458
+ // Phases 2 and 3 run OUTSIDE the claim lock. The consumer callback is
459
+ // awaited here holding no lock at all, so a callback that never settles
460
+ // cannot poison the lock chain and wedge add/update/remove.
461
+ for (const job of dueJobs) {
462
+ try {
463
+ await this.#executeClaimed(job);
464
+ }
465
+ catch (err) {
466
+ // One job's unexpected throw must not abort the batch. Every job in
467
+ // `dueJobs` is already claimed — marked running and detached from
468
+ // the heap — and only its own settle releases it, so aborting here
469
+ // would strand every sibling permanently un-due.
470
+ //
471
+ // Reported on an UNGATED channel. `this.log()` returns early when
472
+ // `config.cron.log` is false, which is a supported production
473
+ // setting, and a failure here permanently unschedules a job while
474
+ // `status()` keeps reporting the service healthy. Silent-and-healthy
475
+ // is exactly the failure class this split exists to remove.
476
+ //
477
+ // This is the outermost handler on the timer path, so it is the one
478
+ // that must not be able to throw: `log` is a shared singleton whose
479
+ // transports can reach the filesystem, so its own failure is
480
+ // swallowed rather than allowed to take the batch down.
481
+ //
482
+ // BOTH halves of that failure have to be caught, and they are caught
483
+ // by different constructs. `log.error` is a chronicle convenience
484
+ // method that returns `logAction(...)` -> `async log(...)`, so its
485
+ // console write, colour lookup, `mkdirSync` and `appendFile` all
486
+ // surface as REJECTIONS, never as synchronous throws. A bare call
487
+ // here escapes this `catch` entirely and terminates the process under
488
+ // Node's default `--unhandled-rejections=throw` — the handler written
489
+ // so it "must not be able to throw" would be the one taking the
490
+ // daemon down. The `try` covers the synchronous half (evaluating the
491
+ // template literal); `Promise.resolve(...).catch()` covers the async
492
+ // half. Deliberately not awaited: the batch must not block on a log
493
+ // transport, and `void` marks the floated promise as intentional.
494
+ try {
495
+ void Promise.resolve(log.error(`Cron — Job "${forLog(job.name, MAX_LOGGED_NAME_LENGTH)}" (${job.id}) execution failed unexpectedly: ${forLog(describeError(err), MAX_LOGGED_ERROR_LENGTH)}`)).catch(() => {
496
+ // Nothing left to report to.
497
+ });
498
+ }
499
+ catch {
500
+ // Nothing left to report to.
501
+ }
502
+ }
503
+ }
185
504
  }
186
505
  finally {
187
506
  this.running = false;
@@ -202,50 +521,198 @@ export default class CronService {
202
521
  }
203
522
  return due;
204
523
  }
524
+ /**
525
+ * Execute a job in three phases:
526
+ *
527
+ * 1. claim (locked) — take ownership of the job, detach it from the heap
528
+ * 2. invoke (UNLOCKED) — await the consumer callback
529
+ * 3. settle (locked) — apply the result, log it, re-insert into the heap
530
+ *
531
+ * The critical section deliberately excludes phase 2. `onJobDue` is
532
+ * arbitrary, unbounded consumer code; awaiting it under the module-global
533
+ * lock is what wedged every subsequent `locked()` call (add/update/remove)
534
+ * when a callback never settled.
535
+ *
536
+ * `onTimer` performs the batch claim (`findDueJobs` + `markRunning`) for all
537
+ * due jobs under a single lock and then enters at phase 2 via
538
+ * `#executeClaimed`. That entry point is `#private` rather than a parameter
539
+ * on this method: as a published `alreadyClaimed` flag it would be a
540
+ * supported way to skip phase 1 entirely, defeating the claim guard and
541
+ * allowing concurrent `onJobDue` invocations for the same job.
542
+ */
205
543
  async executeJob(job) {
544
+ // -- Phase 1: claim (locked) --
545
+ const refusal = await locked(() => this.#claimJob(job));
546
+ if (refusal)
547
+ return { status: 'skipped', reason: refusal };
548
+ return this.#executeClaimed(job);
549
+ }
550
+ /**
551
+ * Phases 2 and 3 for a job that has already been claimed — either by
552
+ * `executeJob` above or by `onTimer`'s batch claim.
553
+ *
554
+ * Private: reaching this without a claim would run the consumer callback for
555
+ * a job nobody owns, and would leave nothing to release the claim.
556
+ */
557
+ async #executeClaimed(job) {
558
+ // Membership re-check. The claim and the invoke are no longer in the same
559
+ // critical section, and sibling callbacks run unlocked, so a `remove()` can
560
+ // now land in between AND RESOLVE — it used to deadlock. A resolved
561
+ // `remove()` must keep meaning "this callback will not fire"; the identity
562
+ // guard in `#settleJob` only cleans up afterwards, by which point the side
563
+ // effect has already happened. Identity, not id, so a removed-then-replaced
564
+ // key is caught too. Deliberately synchronous with the `onJobDue` call
565
+ // below — nothing can interleave between this check and the invocation.
566
+ //
567
+ // This is the one early return after a claim, so it is the one that has to
568
+ // release the claim by hand. Skipping settle is right — re-inserting or
569
+ // run-logging a removed job is the resurrection `#settleJob` refuses, and
570
+ // the heap entry is already gone. But the claim must still come off,
571
+ // because the detached object is NOT unreachable: it is the object `add()`
572
+ // returned and `get()`/`list()` hand out, and `start(initialJobs)`
573
+ // re-registers those objects verbatim, `state` included. A leftover
574
+ // `runningAtMs` rehydrates a permanently dead job — `isDue` false forever,
575
+ // `run()` refused forever, `status()` reporting it healthy.
576
+ //
577
+ // Assigned directly rather than via `applyResult`: this releases the claim
578
+ // and nothing else. No run-log row, no heap entry, no `lastStatus`, no
579
+ // recomputed `nextRunAtMs` — the job did not run.
580
+ if (this.jobs.get(job.id) !== job) {
581
+ job.state.runningAtMs = undefined;
582
+ inFlight.delete(job);
583
+ return { status: 'skipped', reason: 'removed' };
584
+ }
206
585
  const startMs = Date.now();
207
586
  let status = 'ok';
208
587
  let error;
209
588
  let summary;
589
+ let settled;
590
+ // The claim marked the job running and detached it from the heap. Phase 3
591
+ // is the ONLY thing that undoes either, so it must survive every non-local
592
+ // exit from phase 2 — including a throw from the catch handler itself
593
+ // (`this.log` is public and overridable and reaches a transport). A claim
594
+ // with no matching settle is not a degraded state, it is a permanently
595
+ // dead job.
210
596
  try {
211
- if (this.onJobDue) {
212
- const result = await this.onJobDue(job);
213
- if (result) {
214
- status = result.status || 'ok';
215
- error = result.error;
216
- summary = result.summary;
597
+ // -- Phase 2: invoke (NOT locked) --
598
+ try {
599
+ if (this.onJobDue) {
600
+ const result = await this.onJobDue(job);
601
+ if (result) {
602
+ status = result.status || 'ok';
603
+ error = result.error;
604
+ summary = result.summary;
605
+ }
217
606
  }
218
607
  }
608
+ catch (err) {
609
+ status = 'error';
610
+ error = describeError(err);
611
+ this.log(`Job "${forLog(job.name, MAX_LOGGED_NAME_LENGTH)}" (${job.id}) failed: ${forLog(error, MAX_LOGGED_ERROR_LENGTH)}`);
612
+ }
219
613
  }
220
- catch (err) {
221
- status = 'error';
222
- error = err instanceof Error ? err.message : String(err);
223
- this.log(`Job "${job.name}" (${job.id}) failed: ${error}`);
614
+ finally {
615
+ // -- Phase 3: settle (locked) --
616
+ settled = await locked(() => this.#settleJob(job, status, error, summary, startMs, Date.now() - startMs));
224
617
  }
225
- const durationMs = Date.now() - startMs;
226
- const validStatus = (status === 'ok' || status === 'error' || status === 'skipped') ? status : 'error';
227
- applyResult(job, validStatus, error, durationMs);
228
- // Log the run
229
- this.runLog.record({
230
- jobId: job.id,
231
- status,
232
- error,
233
- summary,
234
- runAtMs: startMs,
235
- durationMs,
236
- nextRunAtMs: job.state.nextRunAtMs,
237
- });
238
- // Handle one-shot auto-delete
239
- if (job.deleteAfterRun && status === 'ok' && !job.enabled) {
240
- this.jobs.delete(job.id);
241
- this.runLog.removeJob(job.id);
242
- return { status, summary, deleted: true };
618
+ return settled;
619
+ }
620
+ /**
621
+ * Phase 1 — claim. Must be called while holding the lock (`locked()`, whose
622
+ * chain is module-global and therefore shared across CronService instances).
623
+ *
624
+ * Returns `null` on a successful claim, or the reason the claim was refused.
625
+ * `'already running'` is what makes a second `run()` report a skip instead of
626
+ * launching a concurrent invocation. `'removed'` covers the job being deleted
627
+ * between `run()`'s unlocked lookup and this lock turn — claiming then would
628
+ * `markRunning` an orphan and, worse, `removeFromHeap` an id that may now
629
+ * belong to a replacement.
630
+ *
631
+ * Detaching from the heap here — rather than relying on phase 3 to push a
632
+ * fresh entry — is what stops manual runs permanently duplicating entries.
633
+ *
634
+ * `#private`: published, this would be a supported call performing
635
+ * `markRunning` + `removeFromHeap` with no guaranteed settle and no lease on
636
+ * `runningAtMs`, so a single such call would strand the job forever. The
637
+ * lock-held precondition cannot be expressed in the type system, so the
638
+ * method must not be reachable from outside the class body.
639
+ */
640
+ #claimJob(job) {
641
+ if (this.jobs.get(job.id) !== job)
642
+ return 'removed';
643
+ if (job.state.runningAtMs)
644
+ return 'already running';
645
+ markRunning(job);
646
+ inFlight.add(job);
647
+ this.removeFromHeap(job.id);
648
+ return null;
649
+ }
650
+ /**
651
+ * Phase 3 — settle. Must be called while holding the lock.
652
+ *
653
+ * `#private` for the same reason as `#claimJob`: unlocked it would run
654
+ * `applyResult`, a `runLog.record`, a full `removeFromHeap` rebuild, a
655
+ * `heap.push` and an `armTimer` with no mutual exclusion — exactly the
656
+ * corruption `locked()` exists to prevent.
657
+ */
658
+ #settleJob(job, status, error, summary, startMs, durationMs) {
659
+ try {
660
+ const validStatus = (status === 'ok' || status === 'error' || status === 'skipped') ? status : 'error';
661
+ applyResult(job, validStatus, error, durationMs);
662
+ // The callback ran unlocked, so this job may have been removed — or
663
+ // removed and re-registered under the same id, the shape
664
+ // `start(initialJobs)` uses — while it was in flight. Identity, not id.
665
+ //
666
+ // Deliberately touch NOTHING here. The claim already detached this job's
667
+ // heap entry and nothing re-added it, so there is nothing to clean up;
668
+ // any entry now filed under this id belongs to the replacement, and
669
+ // removing it by id would silently unschedule a live job. Do not
670
+ // resurrect a removed job's heap entry or run log either.
671
+ if (this.jobs.get(job.id) !== job) {
672
+ return { status, error, summary, durationMs };
673
+ }
674
+ // Log the run
675
+ this.runLog.record({
676
+ jobId: job.id,
677
+ status,
678
+ error,
679
+ summary,
680
+ runAtMs: startMs,
681
+ durationMs,
682
+ nextRunAtMs: job.state.nextRunAtMs,
683
+ });
684
+ // Handle one-shot auto-delete. The callback ran unlocked and may have
685
+ // pushed a heap entry for this job via add()/update(), so drop it — the
686
+ // job is about to stop existing.
687
+ if (job.deleteAfterRun && status === 'ok' && !job.enabled) {
688
+ this.jobs.delete(job.id);
689
+ this.removeFromHeap(job.id);
690
+ this.runLog.removeJob(job.id);
691
+ return { status, summary, deleted: true };
692
+ }
693
+ // Re-insert into the heap if still active. Same reason as above: drop any
694
+ // entry the unlocked callback added for this job first, to preserve
695
+ // one-entry-per-key.
696
+ this.removeFromHeap(job.id);
697
+ if (job.enabled && job.state.nextRunAtMs) {
698
+ this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
699
+ }
700
+ return { status, error, summary, durationMs };
243
701
  }
244
- // Re-insert into heap if still active
245
- if (job.enabled && job.state.nextRunAtMs) {
246
- this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
702
+ finally {
703
+ // The invocation is over, so this job is no longer in flight in this
704
+ // process — whatever `applyResult` did or did not manage to write. In the
705
+ // `finally` so it covers a throw out of `applyResult` or `runLog.record`
706
+ // too: past this point no settle is coming, which is precisely the state
707
+ // `start()`'s release is for.
708
+ inFlight.delete(job);
709
+ // One re-arm covering every exit, rather than one per branch. The claim
710
+ // detached this job from the heap, so a timer that fired during the
711
+ // unlocked invoke would have found nothing to arm — and `run()` has no
712
+ // `finally { armTimer() }` of its own the way `onTimer` does. Without
713
+ // this, a manual run() can leave the scheduler with no pending wake.
714
+ this.armTimer();
247
715
  }
248
- return { status, error, summary, durationMs };
249
716
  }
250
717
  // -- Helpers ---------------------------------------------------------
251
718
  removeFromHeap(id) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "keywords": [
4
4
  "stonyx-module"
5
5
  ],
6
- "version": "0.2.1-alpha.6",
6
+ "version": "0.2.1-alpha.61",
7
7
  "description": "Cron/job scheduler for Stonyx framework",
8
8
  "main": "dist/main.js",
9
9
  "types": "dist/main.d.ts",
@@ -69,18 +69,21 @@
69
69
  },
70
70
  "homepage": "https://github.com/abofs/stonyx-cron#readme",
71
71
  "devDependencies": {
72
- "@stonyx/utils": "0.2.3-beta.7",
72
+ "@stonyx/utils": "0.2.3-beta.27",
73
73
  "@types/node": "^25.5.2",
74
+ "@types/qunit": "^2.19.13",
75
+ "@types/sinon": "^21.0.1",
74
76
  "qunit": "^2.24.1",
75
77
  "sinon": "^21.0.0",
78
+ "tsx": "^4.21.0",
76
79
  "typescript": "^5.8.3"
77
80
  },
78
81
  "dependencies": {
79
- "stonyx": "0.2.3-beta.12"
82
+ "stonyx": "0.2.3-beta.93"
80
83
  },
81
84
  "scripts": {
82
85
  "build": "tsc",
83
86
  "build:test": "tsc -p tsconfig.test.json",
84
- "test": "pnpm build && pnpm build:test && stonyx test 'dist-test/test/**/*-test.js'"
87
+ "test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
85
88
  }
86
89
  }