@stonyx/cron 0.2.1-alpha.23 → 0.2.1-alpha.25

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,14 +35,6 @@ 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. One warning is logged per stuck run (not per tick), including how long the invocation has been running. `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. Other jobs are unaffected.
41
- >
42
- > 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.
43
- >
44
- > `interval` is **whole seconds, as a string**. `register` throws a `TypeError` on a value it cannot parse — in particular, this class does not accept cron expressions; use `CronService` (`@stonyx/cron/service`) for those. Values below `1` are clamped to `1` second with a warning.
45
-
46
38
  > `MinHeap` is also exported as a public subpath (`@stonyx/cron/min-heap`) and can be imported directly for advanced usage.
47
39
 
48
40
  ## Configuration
package/dist/main.d.ts CHANGED
@@ -3,17 +3,6 @@ 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. Mirrors `job.state.runningAtMs` in the service tier
9
- * (`src/job.ts` `markRunning`/`applyResult`/`isDue`).
10
- */
11
- runningAtMs?: number;
12
- /**
13
- * True once a skip has been reported for the *current* invocation. Bounds the
14
- * still-running warning to one line per stuck run instead of one per tick.
15
- */
16
- skipReported?: boolean;
17
6
  }
18
7
  export default class Cron {
19
8
  static instance: Cron | null;
@@ -24,37 +13,8 @@ export default class Cron {
24
13
  init(): Promise<void>;
25
14
  scheduleNextRun(): void;
26
15
  runDueJobs(): Promise<void>;
27
- /**
28
- * The one safe way this class invokes a consumer callback.
29
- *
30
- * Never blocks the caller, catches synchronous throws and asynchronous
31
- * rejections alike, and skips the invocation entirely when the job's previous
32
- * invocation has not settled yet (fire-and-forget would otherwise let a slow
33
- * job stack invocations on itself).
34
- */
35
- safeInvoke(job: CronJob, runOnInit?: boolean): void;
36
- /**
37
- * Report a scheduler-level message without ever letting the logger's own
38
- * failure reach the caller.
39
- *
40
- * `@stonyx/logs` convenience methods return a promise and write to disk
41
- * through an unguarded `mkdirSync` + `fsp.appendFile`. On a read-only or full
42
- * log volume that promise rejects; an unobserved rejection raised from inside
43
- * the handler that exists to prevent unhandled rejections would re-create
44
- * exactly the defect this class was fixed for (measured: exit code 1).
45
- */
46
- report(level: 'error' | 'warn', message: string): void;
47
- /** Release a job's in-flight guard. Only ever called for the job it belongs to. */
48
- release(job: CronJob): void;
49
16
  register(key: string, callback: () => void | Promise<void>, interval: string, runOnInit?: boolean): void;
50
17
  unregister(key: string): void;
51
- /**
52
- * Parse a job interval (whole seconds, as a string) into a positive integer.
53
- *
54
- * Returns `null` when the value cannot be parsed at all, so callers can choose
55
- * between failing fast (`register`) and falling back (`setNextTrigger`).
56
- */
57
- parseInterval(interval: string): number | null;
58
18
  setNextTrigger(job: CronJob): void;
59
19
  log(text: string, key?: string | null): void;
60
20
  }
package/dist/main.js CHANGED
@@ -17,28 +17,6 @@ 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
- /**
21
- * Floor for a job interval, in whole seconds.
22
- *
23
- * `runDueJobs` no longer awaits the callback, so `next.nextTrigger > now` is the
24
- * drain loop's only exit condition *and* the loop has no suspension point left.
25
- * An interval that fails to advance `nextTrigger` therefore spins the loop
26
- * forever and blocks the event loop, rather than merely scheduling too often.
27
- */
28
- const MIN_INTERVAL_SECONDS = 1;
29
- /**
30
- * Render an unknown thrown value as log text.
31
- *
32
- * `@stonyx/logs` reads a second argument as `logToFile`, not as a format
33
- * argument, so `log.error(message, err)` discards the error entirely *and*
34
- * forces a disk write on every failure. The error has to be interpolated into
35
- * the message instead — the shape `CronService.executeJob` already uses.
36
- */
37
- function describeError(err) {
38
- if (err instanceof Error)
39
- return err.stack ?? `${err.name}: ${err.message}`;
40
- return String(err);
41
- }
42
20
  export default class Cron {
43
21
  static instance;
44
22
  jobs = {};
@@ -77,117 +55,18 @@ export default class Cron {
77
55
  const job = heap.pop();
78
56
  if (config.debug)
79
57
  this.log('job has been triggered', job.key);
80
- // Reschedule *before* invoking. The callback's result is not used by this
81
- // class (`runDueJobs` returns void), so awaiting it bought nothing and
82
- // cost the scheduler: a callback that never settled left the job absent
83
- // from the heap and stopped the timer from ever re-arming.
58
+ try {
59
+ await job.callback();
60
+ }
61
+ catch (err) {
62
+ log.error(`Cron job "${job.key}" failed:`, err);
63
+ }
84
64
  this.setNextTrigger(job);
85
65
  heap.push(job);
86
- this.safeInvoke(job);
87
66
  }
88
67
  this.scheduleNextRun();
89
68
  }
90
- /**
91
- * The one safe way this class invokes a consumer callback.
92
- *
93
- * Never blocks the caller, catches synchronous throws and asynchronous
94
- * rejections alike, and skips the invocation entirely when the job's previous
95
- * invocation has not settled yet (fire-and-forget would otherwise let a slow
96
- * job stack invocations on itself).
97
- */
98
- safeInvoke(job, runOnInit = false) {
99
- const { key } = job;
100
- const context = runOnInit ? 'failed on init:' : 'failed:';
101
- // The in-flight guard lives on the job object, not in a module-level set
102
- // keyed by string. That is what gives each invocation an identity: the only
103
- // thing that ever clears the flag is the settle handler of the invocation
104
- // that set it, and that handler closes over this exact job object. A stale
105
- // handler therefore cannot release a *later* invocation's guard. It also
106
- // matches the in-repo idiom one tier up (`job.state.runningAtMs`).
107
- //
108
- // `unregister` needs no explicit clear as a result: the flag is dropped with
109
- // the job object, so a re-registered key gets a fresh object and runs
110
- // immediately, while the abandoned invocation can only ever release itself.
111
- if (job.runningAtMs !== undefined) {
112
- // Bounded: one line per stuck run, not one per tick. A permanently hung
113
- // job is re-pushed and re-skipped every interval forever, which at the
114
- // 1s interval this class's own tests use is ~86k log lines a day, per job
115
- // — a disk-fill and ingest-cost vector on any deployment capturing stdout.
116
- if (!job.skipReported) {
117
- job.skipReported = true;
118
- const runningForSeconds = Math.max(0, Math.round((Date.now() - job.runningAtMs) / 1000));
119
- this.report('warn', `Cron job ${JSON.stringify(key)} is still running after ${runningForSeconds}s; skipping this `
120
- + 'tick and any further ticks until it settles (this warning is not repeated for this run)');
121
- }
122
- return;
123
- }
124
- job.runningAtMs = Date.now();
125
- job.skipReported = false;
126
- try {
127
- const result = job.callback();
128
- if (result && typeof result.then === 'function') {
129
- Promise.resolve(result)
130
- .catch((err) => {
131
- // Braces matter: returning `report`'s value would put it back into
132
- // the chain, and `.finally` passes a rejection straight through.
133
- this.report('error', `Cron job ${JSON.stringify(key)} ${context} ${describeError(err)}`);
134
- })
135
- .finally(() => { this.release(job); })
136
- // Backstop: a throw inside the error handler or the release must not
137
- // re-create the unhandled rejection this helper exists to prevent.
138
- .catch(() => { });
139
- return;
140
- }
141
- this.release(job);
142
- }
143
- catch (err) {
144
- this.release(job);
145
- this.report('error', `Cron job ${JSON.stringify(key)} ${context} ${describeError(err)}`);
146
- }
147
- }
148
- /**
149
- * Report a scheduler-level message without ever letting the logger's own
150
- * failure reach the caller.
151
- *
152
- * `@stonyx/logs` convenience methods return a promise and write to disk
153
- * through an unguarded `mkdirSync` + `fsp.appendFile`. On a read-only or full
154
- * log volume that promise rejects; an unobserved rejection raised from inside
155
- * the handler that exists to prevent unhandled rejections would re-create
156
- * exactly the defect this class was fixed for (measured: exit code 1).
157
- */
158
- report(level, message) {
159
- try {
160
- const result = level === 'error' ? log.error(message) : log.warn(message);
161
- void Promise.resolve(result).catch(() => { });
162
- }
163
- catch {
164
- // Nowhere left to report to; the logger must never stop the scheduler.
165
- }
166
- }
167
- /** Release a job's in-flight guard. Only ever called for the job it belongs to. */
168
- release(job) {
169
- job.runningAtMs = undefined;
170
- job.skipReported = false;
171
- }
172
69
  register(key, callback, interval, runOnInit = false) {
173
- const seconds = this.parseInterval(interval);
174
- // Fail fast rather than clamp. An unparseable interval is a programming
175
- // error with exactly one likely cause — a cron expression handed to the
176
- // legacy class, which takes whole seconds — and clamping it would silently
177
- // run a job intended for every 5 minutes once per second, hammering whatever
178
- // the callback talks to. Throwing surfaces it at the call site, at boot,
179
- // before anything is scheduled. A degenerate-but-parseable interval (`'0'`,
180
- // `'-5'`) is a different case: it is interpretable as "as often as possible"
181
- // and is clamped to the floor with one warning.
182
- if (seconds === null) {
183
- throw new TypeError(`Cron job ${JSON.stringify(key)} has an invalid interval ${JSON.stringify(interval)}: `
184
- + 'expected whole seconds (e.g. \'30\'). The legacy Cron class does not accept cron '
185
- + 'expressions — use CronService for those.');
186
- }
187
- if (parseInt(interval, 10) < MIN_INTERVAL_SECONDS) {
188
- this.report('warn', `Cron job ${JSON.stringify(key)} interval ${JSON.stringify(interval)} is below the `
189
- + `${MIN_INTERVAL_SECONDS}s floor; clamping to ${MIN_INTERVAL_SECONDS}s`);
190
- }
191
70
  const job = { callback, interval, key, nextTrigger: 0 };
192
71
  this.jobs[key] = job;
193
72
  this.setNextTrigger(job);
@@ -195,8 +74,14 @@ export default class Cron {
195
74
  if (config.debug) {
196
75
  this.log(`job has been registered with interval: ${interval}`, key);
197
76
  }
198
- if (runOnInit)
199
- this.safeInvoke(job, true);
77
+ if (runOnInit) {
78
+ try {
79
+ callback();
80
+ }
81
+ catch (err) {
82
+ log.error(`Cron job "${key}" failed on init:`, err);
83
+ }
84
+ }
200
85
  this.scheduleNextRun();
201
86
  }
202
87
  unregister(key) {
@@ -210,24 +95,8 @@ export default class Cron {
210
95
  this.log('job has been unregistered', key);
211
96
  this.scheduleNextRun();
212
97
  }
213
- /**
214
- * Parse a job interval (whole seconds, as a string) into a positive integer.
215
- *
216
- * Returns `null` when the value cannot be parsed at all, so callers can choose
217
- * between failing fast (`register`) and falling back (`setNextTrigger`).
218
- */
219
- parseInterval(interval) {
220
- const seconds = parseInt(interval, 10);
221
- if (!Number.isFinite(seconds))
222
- return null;
223
- return Math.max(MIN_INTERVAL_SECONDS, seconds);
224
- }
225
98
  setNextTrigger(job) {
226
- // `register` rejects an unparseable interval up front; this floor is the
227
- // backstop for a job object mutated after registration (`cron.jobs` is
228
- // public, mutable state) and is what actually guarantees the drain loop
229
- // terminates. Never let `nextTrigger` land on `NaN` or on `now`.
230
- job.nextTrigger = getTimestamp() + (this.parseInterval(job.interval) ?? MIN_INTERVAL_SECONDS);
99
+ job.nextTrigger = getTimestamp() + parseInt(job.interval, 10);
231
100
  }
232
101
  log(text, key = null) {
233
102
  if (!config.cron?.log)
package/dist/service.d.ts CHANGED
@@ -15,7 +15,8 @@ interface ExecuteResult {
15
15
  summary?: string;
16
16
  durationMs?: number;
17
17
  deleted?: boolean;
18
- reason?: string;
18
+ /** Only set when `status` is `'skipped'`. */
19
+ reason?: 'not due' | 'already running' | 'removed';
19
20
  }
20
21
  interface ServiceStatus {
21
22
  started: boolean;
@@ -27,6 +28,7 @@ interface ListOptions {
27
28
  }
28
29
  type OnJobDueCallback = (job: Job) => Promise<JobDueResult | void> | JobDueResult | void;
29
30
  export default class CronService {
31
+ #private;
30
32
  jobs: Map<string, Job>;
31
33
  heap: MinHeap<HeapEntry>;
32
34
  timer: ReturnType<typeof setTimeout> | null;
@@ -69,6 +71,25 @@ export default class CronService {
69
71
  remove(id: string): Promise<void>;
70
72
  /**
71
73
  * Manually trigger a job.
74
+ *
75
+ * Returns `{ status: 'skipped', reason }` without invoking the callback when
76
+ * the job is not due (`mode: 'due'`), is already in flight
77
+ * (`'already running'`), or was removed before the claim landed
78
+ * (`'removed'`). Before the phase split a forced run against an in-flight job
79
+ * launched a second concurrent invocation; refusing it is AC4 of #34.
80
+ *
81
+ * CONCURRENCY: the same job is bounded to one in-flight invocation on every
82
+ * path, and the timer path invokes due jobs one at a time. `run()` fan-out
83
+ * across DIFFERENT jobs is deliberately unbounded - N concurrent `run()`
84
+ * calls produce N concurrent consumer callbacks. Before the phase split
85
+ * these serialized behind the module-global lock; that serialization was the
86
+ * bug, not the feature (one hung callback wedged every other caller), so it
87
+ * is not being restored here. The fan-out is caller-driven: it is bounded by
88
+ * how many times the consumer chooses to call `run()`, exactly like any other
89
+ * async API, and the scheduler never produces it on its own. A consumer that
90
+ * exposes `run()` over HTTP or a CLI owns that bound the same way it owns
91
+ * request concurrency for every other handler. A per-invoke bound inside the
92
+ * service is tracked separately (#35).
72
93
  */
73
94
  run(id: string, mode?: 'due' | 'force'): Promise<ExecuteResult>;
74
95
  /**
@@ -78,7 +99,46 @@ export default class CronService {
78
99
  armTimer(): void;
79
100
  onTimer(): Promise<void>;
80
101
  findDueJobs(nowMs: number): Job[];
102
+ /**
103
+ * Execute a job in three phases:
104
+ *
105
+ * 1. claim (locked) - take ownership of the job, detach it from the heap
106
+ * 2. invoke (UNLOCKED) - await the consumer callback
107
+ * 3. settle (locked) - apply the result, log it, re-insert into the heap
108
+ *
109
+ * The critical section deliberately excludes phase 2. `onJobDue` is
110
+ * arbitrary, unbounded consumer code; awaiting it under the module-global
111
+ * lock is what wedged every subsequent `locked()` call (add/update/remove)
112
+ * when a callback never settled.
113
+ *
114
+ * `onTimer` performs the batch claim (findDueJobs + markRunning) for all due
115
+ * jobs under a single lock, then enters at phase 2 via `#executeClaimed`.
116
+ * That entry point is a `#private` method rather than a parameter on this
117
+ * one: as a published `alreadyClaimed` boolean it was a supported way for a
118
+ * consumer to skip phase 1 entirely, which defeats the claim guard AC4 asks
119
+ * for and allows concurrent `onJobDue` invocations for the same job.
120
+ */
81
121
  executeJob(job: Job): Promise<ExecuteResult>;
122
+ /**
123
+ * Phase 1 - claim. Must be called while holding the lock (`locked()`, whose
124
+ * chain is module-global and therefore shared across CronService instances).
125
+ *
126
+ * Returns `null` on a successful claim, or the reason the claim was refused.
127
+ * "already running" is what makes a second `run()` report a skip instead of
128
+ * launching a concurrent invocation. "removed" covers the job being deleted
129
+ * between `run()`'s unlocked lookup and this lock turn - claiming then would
130
+ * `markRunning` an orphan and, worse, `removeFromHeap` an id that may now
131
+ * belong to a replacement.
132
+ *
133
+ * Detaching from the heap here (rather than relying on phase 3 to push a
134
+ * fresh entry) is what keeps manual runs from permanently duplicating heap
135
+ * entries.
136
+ */
137
+ claimJob(job: Job): 'already running' | 'removed' | null;
138
+ /**
139
+ * Phase 3 - settle. Must be called while holding the lock.
140
+ */
141
+ settleJob(job: Job, status: string, error: string | undefined, summary: string | undefined, startMs: number, durationMs: number): ExecuteResult;
82
142
  removeFromHeap(id: string): void;
83
143
  log(message: string): void;
84
144
  }
package/dist/service.js CHANGED
@@ -12,6 +12,24 @@ import { locked } from './locked.js';
12
12
  import { normalizeJobInput, recoverFlatParams } from './normalize.js';
13
13
  import RunLog from './run-log.js';
14
14
  const MAX_TIMER_DELAY_MS = 60_000;
15
+ /**
16
+ * Describe a thrown value without ever throwing.
17
+ *
18
+ * `String(err)` is not total: a null-prototype object, or any object whose
19
+ * `toString`/`Symbol.toPrimitive` throws, raises "Cannot convert object to
20
+ * primitive value". Consumer callbacks throw arbitrary values, so the error
21
+ * handler itself must not be a second failure source.
22
+ */
23
+ function describeError(err) {
24
+ if (err instanceof Error)
25
+ return err.message;
26
+ try {
27
+ return String(err);
28
+ }
29
+ catch {
30
+ return 'unknown error';
31
+ }
32
+ }
15
33
  export default class CronService {
16
34
  jobs;
17
35
  heap;
@@ -136,6 +154,25 @@ export default class CronService {
136
154
  }
137
155
  /**
138
156
  * Manually trigger a job.
157
+ *
158
+ * Returns `{ status: 'skipped', reason }` without invoking the callback when
159
+ * the job is not due (`mode: 'due'`), is already in flight
160
+ * (`'already running'`), or was removed before the claim landed
161
+ * (`'removed'`). Before the phase split a forced run against an in-flight job
162
+ * launched a second concurrent invocation; refusing it is AC4 of #34.
163
+ *
164
+ * CONCURRENCY: the same job is bounded to one in-flight invocation on every
165
+ * path, and the timer path invokes due jobs one at a time. `run()` fan-out
166
+ * across DIFFERENT jobs is deliberately unbounded - N concurrent `run()`
167
+ * calls produce N concurrent consumer callbacks. Before the phase split
168
+ * these serialized behind the module-global lock; that serialization was the
169
+ * bug, not the feature (one hung callback wedged every other caller), so it
170
+ * is not being restored here. The fan-out is caller-driven: it is bounded by
171
+ * how many times the consumer chooses to call `run()`, exactly like any other
172
+ * async API, and the scheduler never produces it on its own. A consumer that
173
+ * exposes `run()` over HTTP or a CLI owns that bound the same way it owns
174
+ * request concurrency for every other handler. A per-invoke bound inside the
175
+ * service is tracked separately (#35).
139
176
  */
140
177
  async run(id, mode = 'force') {
141
178
  const job = this.jobs.get(id);
@@ -144,6 +181,10 @@ export default class CronService {
144
181
  if (mode === 'due' && !isDue(job, Date.now())) {
145
182
  return { status: 'skipped', reason: 'not due' };
146
183
  }
184
+ // Deliberately NOT wrapped in locked(): executeJob takes the lock itself
185
+ // for its claim and settle phases only. Wrapping here would re-create the
186
+ // wedge through a second door, since the callback would again be awaited
187
+ // while a lock is held.
147
188
  return this.executeJob(job);
148
189
  }
149
190
  /**
@@ -172,16 +213,42 @@ export default class CronService {
172
213
  }
173
214
  this.running = true;
174
215
  try {
175
- await locked(async () => {
216
+ // Phase 1 - claim (locked). Collecting due jobs pops them off the heap
217
+ // and marking them running makes them un-collectable by anyone else, so
218
+ // both must happen under the same lock.
219
+ const dueJobs = await locked(() => {
176
220
  const nowMs = Date.now();
177
- const dueJobs = this.findDueJobs(nowMs);
178
- for (const job of dueJobs) {
221
+ const due = this.findDueJobs(nowMs);
222
+ for (const job of due) {
179
223
  markRunning(job);
180
224
  }
181
- for (const job of dueJobs) {
182
- await this.executeJob(job);
183
- }
225
+ return due;
184
226
  });
227
+ // Phases 2 and 3 run outside the claim lock. The consumer callback is
228
+ // awaited here holding no lock at all, so a callback that never settles
229
+ // cannot poison the lock chain.
230
+ for (const job of dueJobs) {
231
+ try {
232
+ await this.#executeClaimed(job);
233
+ }
234
+ catch (err) {
235
+ // One job's unexpected throw must not abort the batch. Every job in
236
+ // `dueJobs` is already claimed - marked running and detached from the
237
+ // heap - and only its own settle releases it, so aborting here would
238
+ // strand every sibling permanently un-due.
239
+ //
240
+ // This is the outermost handler on the timer path, so it is the one
241
+ // that must not be able to throw. `log()` is public, overridable and
242
+ // can reach a file transport, so its own failure is swallowed here
243
+ // rather than being allowed to take the batch down.
244
+ try {
245
+ this.log(`Job "${job.name}" (${job.id}) execution failed unexpectedly: ${describeError(err)}`);
246
+ }
247
+ catch {
248
+ // Nothing left to report to.
249
+ }
250
+ }
251
+ }
185
252
  }
186
253
  finally {
187
254
  this.running = false;
@@ -202,50 +269,165 @@ export default class CronService {
202
269
  }
203
270
  return due;
204
271
  }
272
+ /**
273
+ * Execute a job in three phases:
274
+ *
275
+ * 1. claim (locked) - take ownership of the job, detach it from the heap
276
+ * 2. invoke (UNLOCKED) - await the consumer callback
277
+ * 3. settle (locked) - apply the result, log it, re-insert into the heap
278
+ *
279
+ * The critical section deliberately excludes phase 2. `onJobDue` is
280
+ * arbitrary, unbounded consumer code; awaiting it under the module-global
281
+ * lock is what wedged every subsequent `locked()` call (add/update/remove)
282
+ * when a callback never settled.
283
+ *
284
+ * `onTimer` performs the batch claim (findDueJobs + markRunning) for all due
285
+ * jobs under a single lock, then enters at phase 2 via `#executeClaimed`.
286
+ * That entry point is a `#private` method rather than a parameter on this
287
+ * one: as a published `alreadyClaimed` boolean it was a supported way for a
288
+ * consumer to skip phase 1 entirely, which defeats the claim guard AC4 asks
289
+ * for and allows concurrent `onJobDue` invocations for the same job.
290
+ */
205
291
  async executeJob(job) {
292
+ // -- Phase 1: claim (locked) --
293
+ const refusal = await locked(() => this.claimJob(job));
294
+ if (refusal)
295
+ return { status: 'skipped', reason: refusal };
296
+ return this.#executeClaimed(job);
297
+ }
298
+ /**
299
+ * Phases 2 and 3 for a job that has already been claimed - either by
300
+ * `executeJob` above or by `onTimer`'s batch claim.
301
+ *
302
+ * Private: reaching this without a claim would run the consumer callback for
303
+ * a job nobody owns.
304
+ */
305
+ async #executeClaimed(job) {
306
+ // Membership re-check. The claim and the invoke are no longer in the same
307
+ // critical section, and sibling callbacks run unlocked, so a `remove()` can
308
+ // land in between AND RESOLVE - it used to deadlock. A resolved `remove()`
309
+ // must keep meaning "this callback will not fire": settleJob's identity
310
+ // guard only cleans up afterwards, by which point the side effect has
311
+ // already happened. Identity, not id, so a removed-then-replaced key is
312
+ // caught too. Deliberately synchronous with the `onJobDue` call below -
313
+ // nothing can interleave between this check and the invocation.
314
+ if (this.jobs.get(job.id) !== job)
315
+ return { status: 'skipped', reason: 'removed' };
206
316
  const startMs = Date.now();
207
317
  let status = 'ok';
208
318
  let error;
209
319
  let summary;
320
+ let settled;
321
+ // The claim above marked the job running and detached it from the heap.
322
+ // Phase 3 is the ONLY thing that undoes either, so it must survive every
323
+ // non-local exit from phase 2 - including a throw from the catch handler
324
+ // itself. A claim with no matching settle is not a degraded state, it is a
325
+ // permanently dead job: `runningAtMs` set, no heap entry, `isDue` false
326
+ // forever and `run()` refused forever.
210
327
  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;
328
+ // -- Phase 2: invoke (NOT locked) --
329
+ try {
330
+ if (this.onJobDue) {
331
+ const result = await this.onJobDue(job);
332
+ if (result) {
333
+ status = result.status || 'ok';
334
+ error = result.error;
335
+ summary = result.summary;
336
+ }
217
337
  }
218
338
  }
339
+ catch (err) {
340
+ status = 'error';
341
+ error = describeError(err);
342
+ this.log(`Job "${job.name}" (${job.id}) failed: ${error}`);
343
+ }
219
344
  }
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}`);
345
+ finally {
346
+ // -- Phase 3: settle (locked) --
347
+ settled = await locked(() => this.settleJob(job, status, error, summary, startMs, Date.now() - startMs));
224
348
  }
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 };
349
+ return settled;
350
+ }
351
+ /**
352
+ * Phase 1 - claim. Must be called while holding the lock (`locked()`, whose
353
+ * chain is module-global and therefore shared across CronService instances).
354
+ *
355
+ * Returns `null` on a successful claim, or the reason the claim was refused.
356
+ * "already running" is what makes a second `run()` report a skip instead of
357
+ * launching a concurrent invocation. "removed" covers the job being deleted
358
+ * between `run()`'s unlocked lookup and this lock turn - claiming then would
359
+ * `markRunning` an orphan and, worse, `removeFromHeap` an id that may now
360
+ * belong to a replacement.
361
+ *
362
+ * Detaching from the heap here (rather than relying on phase 3 to push a
363
+ * fresh entry) is what keeps manual runs from permanently duplicating heap
364
+ * entries.
365
+ */
366
+ claimJob(job) {
367
+ if (this.jobs.get(job.id) !== job)
368
+ return 'removed';
369
+ if (job.state.runningAtMs)
370
+ return 'already running';
371
+ markRunning(job);
372
+ this.removeFromHeap(job.id);
373
+ return null;
374
+ }
375
+ /**
376
+ * Phase 3 - settle. Must be called while holding the lock.
377
+ */
378
+ settleJob(job, status, error, summary, startMs, durationMs) {
379
+ try {
380
+ const validStatus = (status === 'ok' || status === 'error' || status === 'skipped') ? status : 'error';
381
+ applyResult(job, validStatus, error, durationMs);
382
+ // The callback ran unlocked, so this job may have been removed - or
383
+ // removed and re-registered under the same id (the shape
384
+ // `start(initialJobs)` uses) - while it was in flight. Identity, not id.
385
+ //
386
+ // Deliberately touch NOTHING here. The claim phase already detached this
387
+ // job's own heap entry and nothing re-added it, so there is nothing to
388
+ // clean up; any entry now filed under this id belongs to the
389
+ // replacement, and removing it by id would silently unschedule a live
390
+ // job. Do not resurrect a removed job's heap entry or run log either.
391
+ if (this.jobs.get(job.id) !== job) {
392
+ return { status, error, summary, durationMs };
393
+ }
394
+ // Log the run
395
+ this.runLog.record({
396
+ jobId: job.id,
397
+ status,
398
+ error,
399
+ summary,
400
+ runAtMs: startMs,
401
+ durationMs,
402
+ nextRunAtMs: job.state.nextRunAtMs,
403
+ });
404
+ // Handle one-shot auto-delete. The callback ran unlocked and may have
405
+ // pushed a heap entry for this job via update(), so drop it - the job is
406
+ // about to stop existing.
407
+ if (job.deleteAfterRun && status === 'ok' && !job.enabled) {
408
+ this.jobs.delete(job.id);
409
+ this.removeFromHeap(job.id);
410
+ this.runLog.removeJob(job.id);
411
+ return { status, summary, deleted: true };
412
+ }
413
+ // Re-insert into heap if still active. The callback ran unlocked, so it
414
+ // may itself have added a heap entry for this job (via add/update); drop
415
+ // any such entry first to preserve one-entry-per-key.
416
+ this.removeFromHeap(job.id);
417
+ if (job.enabled && job.state.nextRunAtMs) {
418
+ this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
419
+ }
420
+ return { status, error, summary, durationMs };
243
421
  }
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 });
422
+ finally {
423
+ // One re-arm covering every exit, rather than one per branch. The claim
424
+ // phase detached this job from the heap, so a timer that fired during the
425
+ // unlocked invoke would have found an empty heap and armed nothing -
426
+ // and `run()` has no `finally { armTimer() }` of its own the way
427
+ // `onTimer` does. Without this a manual run() can leave the scheduler
428
+ // with no pending wake at all.
429
+ this.armTimer();
247
430
  }
248
- return { status, error, summary, durationMs };
249
431
  }
250
432
  // -- Helpers ---------------------------------------------------------
251
433
  removeFromHeap(id) {
@@ -266,6 +448,19 @@ export default class CronService {
266
448
  log(message) {
267
449
  if (!config.cron?.log)
268
450
  return;
269
- log.cron(`Cron ${message}`);
451
+ // `log.cron` is created by `log.defineType`, which runs in `Cron.init()`
452
+ // (src/main.ts) - a DIFFERENT class. A consumer wiring CronService directly
453
+ // never runs it, while `config/environment.js` defaults `cron.log` to true,
454
+ // so an unguarded call throws `log.cron is not a function`. That throw
455
+ // escapes executeJob's catch, and the error-reporting path must never be
456
+ // the thing that kills the scheduler. `src/types/stonyx.d.ts:19` declares
457
+ // `cron()` unconditionally, so the type system will not catch this.
458
+ const { logColor = '#888', logMethod = 'cron' } = config.cron ?? {};
459
+ if (typeof log[logMethod] !== 'function')
460
+ log.defineType(logMethod, logColor);
461
+ const method = log[logMethod];
462
+ if (typeof method !== 'function')
463
+ return;
464
+ method.call(log, `Cron — ${message}`);
270
465
  }
271
466
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "keywords": [
4
4
  "stonyx-module"
5
5
  ],
6
- "version": "0.2.1-alpha.23",
6
+ "version": "0.2.1-alpha.25",
7
7
  "description": "Cron/job scheduler for Stonyx framework",
8
8
  "main": "dist/main.js",
9
9
  "types": "dist/main.d.ts",