@stonyx/cron 0.2.1-alpha.17 → 0.2.1-alpha.18

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,6 +35,8 @@ 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
+ > Callbacks are invoked fire-and-forget: `Cron` never waits for one to settle. Two *different* jobs that fall due on the same tick may therefore overlap, and a job that is still running when it next falls due is skipped for that tick (a warning is logged). Both synchronous throws and asynchronous rejections are caught and logged; neither can stop the scheduler.
39
+
38
40
  > `MinHeap` is also exported as a public subpath (`@stonyx/cron/min-heap`) and can be imported directly for advanced usage.
39
41
 
40
42
  ## Configuration
package/dist/main.d.ts CHANGED
@@ -3,6 +3,17 @@ 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;
6
17
  }
7
18
  export default class Cron {
8
19
  static instance: Cron | null;
@@ -13,8 +24,37 @@ export default class Cron {
13
24
  init(): Promise<void>;
14
25
  scheduleNextRun(): void;
15
26
  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;
16
49
  register(key: string, callback: () => void | Promise<void>, interval: string, runOnInit?: boolean): void;
17
50
  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;
18
58
  setNextTrigger(job: CronJob): void;
19
59
  log(text: string, key?: string | null): void;
20
60
  }
package/dist/main.js CHANGED
@@ -17,6 +17,28 @@ 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
+ }
20
42
  export default class Cron {
21
43
  static instance;
22
44
  jobs = {};
@@ -55,18 +77,117 @@ export default class Cron {
55
77
  const job = heap.pop();
56
78
  if (config.debug)
57
79
  this.log('job has been triggered', job.key);
58
- try {
59
- await job.callback();
60
- }
61
- catch (err) {
62
- log.error(`Cron job "${job.key}" failed:`, err);
63
- }
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.
64
84
  this.setNextTrigger(job);
65
85
  heap.push(job);
86
+ this.safeInvoke(job);
66
87
  }
67
88
  this.scheduleNextRun();
68
89
  }
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
+ }
69
172
  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
+ }
70
191
  const job = { callback, interval, key, nextTrigger: 0 };
71
192
  this.jobs[key] = job;
72
193
  this.setNextTrigger(job);
@@ -74,14 +195,8 @@ export default class Cron {
74
195
  if (config.debug) {
75
196
  this.log(`job has been registered with interval: ${interval}`, key);
76
197
  }
77
- if (runOnInit) {
78
- try {
79
- callback();
80
- }
81
- catch (err) {
82
- log.error(`Cron job "${key}" failed on init:`, err);
83
- }
84
- }
198
+ if (runOnInit)
199
+ this.safeInvoke(job, true);
85
200
  this.scheduleNextRun();
86
201
  }
87
202
  unregister(key) {
@@ -95,8 +210,24 @@ export default class Cron {
95
210
  this.log('job has been unregistered', key);
96
211
  this.scheduleNextRun();
97
212
  }
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
+ }
98
225
  setNextTrigger(job) {
99
- job.nextTrigger = getTimestamp() + parseInt(job.interval, 10);
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);
100
231
  }
101
232
  log(text, key = null) {
102
233
  if (!config.cron?.log)
package/dist/service.d.ts CHANGED
@@ -15,8 +15,7 @@ interface ExecuteResult {
15
15
  summary?: string;
16
16
  durationMs?: number;
17
17
  deleted?: boolean;
18
- /** Only set when `status` is `'skipped'`. */
19
- reason?: 'not due' | 'already running' | 'removed';
18
+ reason?: string;
20
19
  }
21
20
  interface ServiceStatus {
22
21
  started: boolean;
@@ -79,41 +78,7 @@ export default class CronService {
79
78
  armTimer(): void;
80
79
  onTimer(): Promise<void>;
81
80
  findDueJobs(nowMs: number): Job[];
82
- /**
83
- * Execute a job in three phases:
84
- *
85
- * 1. claim (locked) - take ownership of the job, detach it from the heap
86
- * 2. invoke (UNLOCKED) - await the consumer callback
87
- * 3. settle (locked) - apply the result, log it, re-insert into the heap
88
- *
89
- * The critical section deliberately excludes phase 2. `onJobDue` is
90
- * arbitrary, unbounded consumer code; awaiting it under the module-global
91
- * lock is what wedged every subsequent `locked()` call (add/update/remove)
92
- * when a callback never settled.
93
- *
94
- * `alreadyClaimed` is passed by `onTimer`, which performs the batch claim
95
- * (findDueJobs + markRunning) for all due jobs under a single lock.
96
- */
97
- executeJob(job: Job, alreadyClaimed?: boolean): Promise<ExecuteResult>;
98
- /**
99
- * Phase 1 - claim. Must be called while holding the lock.
100
- *
101
- * Returns `null` on a successful claim, or the reason the claim was refused.
102
- * "already running" is what makes a second `run()` report a skip instead of
103
- * launching a concurrent invocation. "removed" covers the job being deleted
104
- * between `run()`'s unlocked lookup and this lock turn - claiming then would
105
- * `markRunning` an orphan and, worse, `removeFromHeap` an id that may now
106
- * belong to a replacement.
107
- *
108
- * Detaching from the heap here (rather than relying on phase 3 to push a
109
- * fresh entry) is what keeps manual runs from permanently duplicating heap
110
- * entries.
111
- */
112
- claimJob(job: Job): 'already running' | 'removed' | null;
113
- /**
114
- * Phase 3 - settle. Must be called while holding the lock.
115
- */
116
- settleJob(job: Job, status: string, error: string | undefined, summary: string | undefined, startMs: number, durationMs: number): ExecuteResult;
81
+ executeJob(job: Job): Promise<ExecuteResult>;
117
82
  removeFromHeap(id: string): void;
118
83
  log(message: string): void;
119
84
  }
package/dist/service.js CHANGED
@@ -12,24 +12,6 @@ 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
- }
33
15
  export default class CronService {
34
16
  jobs;
35
17
  heap;
@@ -162,10 +144,6 @@ export default class CronService {
162
144
  if (mode === 'due' && !isDue(job, Date.now())) {
163
145
  return { status: 'skipped', reason: 'not due' };
164
146
  }
165
- // Deliberately NOT wrapped in locked(): executeJob takes the lock itself
166
- // for its claim and settle phases only. Wrapping here would re-create the
167
- // wedge through a second door, since the callback would again be awaited
168
- // while a lock is held.
169
147
  return this.executeJob(job);
170
148
  }
171
149
  /**
@@ -194,42 +172,16 @@ export default class CronService {
194
172
  }
195
173
  this.running = true;
196
174
  try {
197
- // Phase 1 - claim (locked). Collecting due jobs pops them off the heap
198
- // and marking them running makes them un-collectable by anyone else, so
199
- // both must happen under the same lock.
200
- const dueJobs = await locked(() => {
175
+ await locked(async () => {
201
176
  const nowMs = Date.now();
202
- const due = this.findDueJobs(nowMs);
203
- for (const job of due) {
177
+ const dueJobs = this.findDueJobs(nowMs);
178
+ for (const job of dueJobs) {
204
179
  markRunning(job);
205
180
  }
206
- return due;
207
- });
208
- // Phases 2 and 3 run outside the claim lock. The consumer callback is
209
- // awaited here holding no lock at all, so a callback that never settles
210
- // cannot poison the lock chain.
211
- for (const job of dueJobs) {
212
- try {
213
- await this.executeJob(job, true);
214
- }
215
- catch (err) {
216
- // One job's unexpected throw must not abort the batch. Every job in
217
- // `dueJobs` is already claimed - marked running and detached from the
218
- // heap - and only its own settle releases it, so aborting here would
219
- // strand every sibling permanently un-due.
220
- //
221
- // This is the outermost handler on the timer path, so it is the one
222
- // that must not be able to throw. `log()` is public, overridable and
223
- // can reach a file transport, so its own failure is swallowed here
224
- // rather than being allowed to take the batch down.
225
- try {
226
- this.log(`Job "${job.name}" (${job.id}) execution failed unexpectedly: ${describeError(err)}`);
227
- }
228
- catch {
229
- // Nothing left to report to.
230
- }
181
+ for (const job of dueJobs) {
182
+ await this.executeJob(job);
231
183
  }
232
- }
184
+ });
233
185
  }
234
186
  finally {
235
187
  this.running = false;
@@ -250,152 +202,50 @@ export default class CronService {
250
202
  }
251
203
  return due;
252
204
  }
253
- /**
254
- * Execute a job in three phases:
255
- *
256
- * 1. claim (locked) - take ownership of the job, detach it from the heap
257
- * 2. invoke (UNLOCKED) - await the consumer callback
258
- * 3. settle (locked) - apply the result, log it, re-insert into the heap
259
- *
260
- * The critical section deliberately excludes phase 2. `onJobDue` is
261
- * arbitrary, unbounded consumer code; awaiting it under the module-global
262
- * lock is what wedged every subsequent `locked()` call (add/update/remove)
263
- * when a callback never settled.
264
- *
265
- * `alreadyClaimed` is passed by `onTimer`, which performs the batch claim
266
- * (findDueJobs + markRunning) for all due jobs under a single lock.
267
- */
268
- async executeJob(job, alreadyClaimed = false) {
269
- // -- Phase 1: claim (locked) --
270
- if (!alreadyClaimed) {
271
- const refusal = await locked(() => this.claimJob(job));
272
- if (refusal)
273
- return { status: 'skipped', reason: refusal };
274
- }
275
- // Membership re-check. The claim and the invoke are no longer in the same
276
- // critical section, and sibling callbacks run unlocked, so a `remove()` can
277
- // land in between AND RESOLVE - it used to deadlock. A resolved `remove()`
278
- // must keep meaning "this callback will not fire": settleJob's identity
279
- // guard only cleans up afterwards, by which point the side effect has
280
- // already happened. Identity, not id, so a removed-then-replaced key is
281
- // caught too. Deliberately synchronous with the `onJobDue` call below -
282
- // nothing can interleave between this check and the invocation.
283
- if (this.jobs.get(job.id) !== job)
284
- return { status: 'skipped', reason: 'removed' };
205
+ async executeJob(job) {
285
206
  const startMs = Date.now();
286
207
  let status = 'ok';
287
208
  let error;
288
209
  let summary;
289
- let settled;
290
- // The claim above marked the job running and detached it from the heap.
291
- // Phase 3 is the ONLY thing that undoes either, so it must survive every
292
- // non-local exit from phase 2 - including a throw from the catch handler
293
- // itself. A claim with no matching settle is not a degraded state, it is a
294
- // permanently dead job: `runningAtMs` set, no heap entry, `isDue` false
295
- // forever and `run()` refused forever.
296
210
  try {
297
- // -- Phase 2: invoke (NOT locked) --
298
- try {
299
- if (this.onJobDue) {
300
- const result = await this.onJobDue(job);
301
- if (result) {
302
- status = result.status || 'ok';
303
- error = result.error;
304
- summary = result.summary;
305
- }
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;
306
217
  }
307
218
  }
308
- catch (err) {
309
- status = 'error';
310
- error = describeError(err);
311
- this.log(`Job "${job.name}" (${job.id}) failed: ${error}`);
312
- }
313
219
  }
314
- finally {
315
- // -- Phase 3: settle (locked) --
316
- settled = await locked(() => this.settleJob(job, status, error, summary, startMs, Date.now() - startMs));
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}`);
317
224
  }
318
- return settled;
319
- }
320
- /**
321
- * Phase 1 - claim. Must be called while holding the lock.
322
- *
323
- * Returns `null` on a successful claim, or the reason the claim was refused.
324
- * "already running" is what makes a second `run()` report a skip instead of
325
- * launching a concurrent invocation. "removed" covers the job being deleted
326
- * between `run()`'s unlocked lookup and this lock turn - claiming then would
327
- * `markRunning` an orphan and, worse, `removeFromHeap` an id that may now
328
- * belong to a replacement.
329
- *
330
- * Detaching from the heap here (rather than relying on phase 3 to push a
331
- * fresh entry) is what keeps manual runs from permanently duplicating heap
332
- * entries.
333
- */
334
- claimJob(job) {
335
- if (this.jobs.get(job.id) !== job)
336
- return 'removed';
337
- if (job.state.runningAtMs)
338
- return 'already running';
339
- markRunning(job);
340
- this.removeFromHeap(job.id);
341
- return null;
342
- }
343
- /**
344
- * Phase 3 - settle. Must be called while holding the lock.
345
- */
346
- settleJob(job, status, error, summary, startMs, durationMs) {
347
- try {
348
- const validStatus = (status === 'ok' || status === 'error' || status === 'skipped') ? status : 'error';
349
- applyResult(job, validStatus, error, durationMs);
350
- // The callback ran unlocked, so this job may have been removed - or
351
- // removed and re-registered under the same id (the shape
352
- // `start(initialJobs)` uses) - while it was in flight. Identity, not id.
353
- //
354
- // Deliberately touch NOTHING here. The claim phase already detached this
355
- // job's own heap entry and nothing re-added it, so there is nothing to
356
- // clean up; any entry now filed under this id belongs to the
357
- // replacement, and removing it by id would silently unschedule a live
358
- // job. Do not resurrect a removed job's heap entry or run log either.
359
- if (this.jobs.get(job.id) !== job) {
360
- return { status, error, summary, durationMs };
361
- }
362
- // Log the run
363
- this.runLog.record({
364
- jobId: job.id,
365
- status,
366
- error,
367
- summary,
368
- runAtMs: startMs,
369
- durationMs,
370
- nextRunAtMs: job.state.nextRunAtMs,
371
- });
372
- // Handle one-shot auto-delete. The callback ran unlocked and may have
373
- // pushed a heap entry for this job via update(), so drop it - the job is
374
- // about to stop existing.
375
- if (job.deleteAfterRun && status === 'ok' && !job.enabled) {
376
- this.jobs.delete(job.id);
377
- this.removeFromHeap(job.id);
378
- this.runLog.removeJob(job.id);
379
- return { status, summary, deleted: true };
380
- }
381
- // Re-insert into heap if still active. The callback ran unlocked, so it
382
- // may itself have added a heap entry for this job (via add/update); drop
383
- // any such entry first to preserve one-entry-per-key.
384
- this.removeFromHeap(job.id);
385
- if (job.enabled && job.state.nextRunAtMs) {
386
- this.heap.push({ key: job.id, nextTrigger: job.state.nextRunAtMs });
387
- }
388
- return { status, error, summary, durationMs };
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 };
389
243
  }
390
- finally {
391
- // One re-arm covering every exit, rather than one per branch. The claim
392
- // phase detached this job from the heap, so a timer that fired during the
393
- // unlocked invoke would have found an empty heap and armed nothing -
394
- // and `run()` has no `finally { armTimer() }` of its own the way
395
- // `onTimer` does. Without this a manual run() can leave the scheduler
396
- // with no pending wake at all.
397
- this.armTimer();
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 });
398
247
  }
248
+ return { status, error, summary, durationMs };
399
249
  }
400
250
  // -- Helpers ---------------------------------------------------------
401
251
  removeFromHeap(id) {
@@ -416,19 +266,6 @@ export default class CronService {
416
266
  log(message) {
417
267
  if (!config.cron?.log)
418
268
  return;
419
- // `log.cron` is created by `log.defineType`, which runs in `Cron.init()`
420
- // (src/main.ts) - a DIFFERENT class. A consumer wiring CronService directly
421
- // never runs it, while `config/environment.js` defaults `cron.log` to true,
422
- // so an unguarded call throws `log.cron is not a function`. That throw
423
- // escapes executeJob's catch, and the error-reporting path must never be
424
- // the thing that kills the scheduler. `src/types/stonyx.d.ts:19` declares
425
- // `cron()` unconditionally, so the type system will not catch this.
426
- const { logColor = '#888', logMethod = 'cron' } = config.cron ?? {};
427
- if (typeof log[logMethod] !== 'function')
428
- log.defineType(logMethod, logColor);
429
- const method = log[logMethod];
430
- if (typeof method !== 'function')
431
- return;
432
- method.call(log, `Cron — ${message}`);
269
+ log.cron(`Cron ${message}`);
433
270
  }
434
271
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "keywords": [
4
4
  "stonyx-module"
5
5
  ],
6
- "version": "0.2.1-alpha.17",
6
+ "version": "0.2.1-alpha.18",
7
7
  "description": "Cron/job scheduler for Stonyx framework",
8
8
  "main": "dist/main.js",
9
9
  "types": "dist/main.d.ts",