@stonyx/cron 0.2.1-beta.9 → 0.2.1-beta.91

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/dist/main.js ADDED
@@ -0,0 +1,284 @@
1
+ /*
2
+ * Copyright 2025 Stone Costa
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the 'License');
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import config from 'stonyx/config';
17
+ import log from 'stonyx/log';
18
+ import { getTimestamp } from '@stonyx/utils/date';
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
+ * Ceiling for a single `setTimeout` delay, in milliseconds (2^31 - 1, ~24.9
31
+ * days).
32
+ *
33
+ * Node stores a timer's delay in a 32-bit signed int. A larger value overflows,
34
+ * is truncated to 1 ms and emits a `TimeoutOverflowWarning`, so `scheduleNextRun`
35
+ * would re-arm every millisecond while the job never came due — measured at
36
+ * `'86400000'` (a day expressed in *milliseconds*, the plausible typo): ~790
37
+ * wakeups a second and ~8 GB of stderr a day from a job that never runs.
38
+ *
39
+ * Long intervals are clamped and re-armed rather than rejected, so they work
40
+ * instead of merely failing loudly.
41
+ */
42
+ const TIMEOUT_MAX_MS = 2_147_483_647;
43
+ /**
44
+ * Render an unknown thrown value as log text.
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
+ function describeError(err) {
52
+ if (err instanceof Error)
53
+ return err.stack ?? `${err.name}: ${err.message}`;
54
+ return String(err);
55
+ }
56
+ export default class Cron {
57
+ static instance;
58
+ jobs = {};
59
+ heap = new MinHeap();
60
+ timer = null;
61
+ constructor() {
62
+ if (Cron.instance)
63
+ return Cron.instance;
64
+ Cron.instance = this;
65
+ }
66
+ async init() {
67
+ // Self-register so log.cron works even when @stonyx/cron is in the
68
+ // consumer's `dependencies` (stonyx loader only merges devDependencies).
69
+ const { logColor = '#888', logMethod = 'cron' } = config.cron ?? {};
70
+ log.defineType(logMethod, logColor);
71
+ }
72
+ scheduleNextRun() {
73
+ if (this.timer)
74
+ clearTimeout(this.timer);
75
+ const { heap } = this;
76
+ if (heap.isEmpty())
77
+ return;
78
+ const nextJob = heap.peek();
79
+ if (!nextJob)
80
+ return;
81
+ // Clamped to `setTimeout`'s range. `runDueJobs` finds nothing due when the
82
+ // clamp fires, breaks out of the drain loop, and re-arms the remainder — so
83
+ // an interval past the ceiling costs one extra wakeup every ~24.9 days
84
+ // instead of one every millisecond, forever.
85
+ const delay = Math.min(Math.max(0, nextJob.nextTrigger - getTimestamp()) * 1000, TIMEOUT_MAX_MS);
86
+ this.timer = setTimeout(() => this.runDueJobs(), delay);
87
+ }
88
+ async runDueJobs() {
89
+ const now = getTimestamp();
90
+ const { heap } = this;
91
+ while (!heap.isEmpty()) {
92
+ const next = heap.peek();
93
+ if (!next || next.nextTrigger > now)
94
+ break;
95
+ const job = heap.pop();
96
+ if (config.debug)
97
+ this.log('job has been triggered', job.key);
98
+ // Reschedule *before* invoking. The callback's result is not used by this
99
+ // class (`runDueJobs` returns void), so awaiting it bought nothing and
100
+ // cost the scheduler: a callback that never settled left the job absent
101
+ // from the heap and stopped the timer from ever re-arming.
102
+ this.setNextTrigger(job);
103
+ heap.push(job);
104
+ this.safeInvoke(job);
105
+ }
106
+ this.scheduleNextRun();
107
+ }
108
+ /**
109
+ * The one safe way this class invokes a consumer callback.
110
+ *
111
+ * Never blocks the caller, catches synchronous throws and asynchronous
112
+ * rejections alike, and skips the invocation entirely when the job's previous
113
+ * invocation has not settled yet (fire-and-forget would otherwise let a slow
114
+ * job stack invocations on itself).
115
+ */
116
+ safeInvoke(job, runOnInit = false) {
117
+ const { key } = job;
118
+ const context = runOnInit ? 'failed on init:' : 'failed:';
119
+ // The in-flight guard lives on the job object, not in a module-level set
120
+ // keyed by string. That is what gives each invocation an identity: the only
121
+ // thing that ever clears the flag is the settle handler of the invocation
122
+ // that set it, and that handler closes over this exact job object. A stale
123
+ // handler therefore cannot release a *later* invocation's guard. It also
124
+ // matches the in-repo idiom one tier up (`job.state.runningAtMs`).
125
+ //
126
+ // `unregister` needs no explicit clear as a result: the flag is dropped with
127
+ // the job object, so a re-registered key gets a fresh object and runs
128
+ // immediately, while the abandoned invocation can only ever release itself.
129
+ if (job.runningAtMs !== undefined) {
130
+ // Bounded: one line per stuck run, not one per tick. A permanently hung
131
+ // job is re-pushed and re-skipped every interval forever, which at the
132
+ // 1s interval this class's own tests use is ~86k log lines a day, per job
133
+ // — a disk-fill and ingest-cost vector on any deployment capturing stdout.
134
+ if (!job.skipReported) {
135
+ job.skipReported = true;
136
+ const runningForSeconds = Math.max(0, Math.round((Date.now() - job.runningAtMs) / 1000));
137
+ this.report('warn', `Cron job ${JSON.stringify(key)} is still running after ${runningForSeconds}s; skipping this `
138
+ + 'tick and any further ticks until it settles (this warning is not repeated for this run)');
139
+ }
140
+ return;
141
+ }
142
+ job.runningAtMs = Date.now();
143
+ job.skipReported = false;
144
+ try {
145
+ const result = job.callback();
146
+ if (result && typeof result.then === 'function') {
147
+ Promise.resolve(result)
148
+ .catch((err) => {
149
+ // Braces matter: returning `report`'s value would put it back into
150
+ // the chain, and `.finally` passes a rejection straight through.
151
+ this.report('error', `Cron job ${JSON.stringify(key)} ${context} ${describeError(err)}`);
152
+ })
153
+ .finally(() => { this.release(job); })
154
+ // Backstop: a throw inside the error handler or the release must not
155
+ // re-create the unhandled rejection this helper exists to prevent.
156
+ .catch(() => { });
157
+ return;
158
+ }
159
+ this.release(job);
160
+ }
161
+ catch (err) {
162
+ this.release(job);
163
+ this.report('error', `Cron job ${JSON.stringify(key)} ${context} ${describeError(err)}`);
164
+ }
165
+ }
166
+ /**
167
+ * Report a scheduler-level message without ever letting the logger's own
168
+ * failure reach the caller.
169
+ *
170
+ * `@stonyx/logs` convenience methods return a promise and write to disk
171
+ * through an unguarded `mkdirSync` + `fsp.appendFile`. On a read-only or full
172
+ * log volume that promise rejects; an unobserved rejection raised from inside
173
+ * the handler that exists to prevent unhandled rejections would re-create
174
+ * exactly the defect this class was fixed for (measured: exit code 1).
175
+ */
176
+ report(level, message) {
177
+ try {
178
+ const result = level === 'error' ? log.error(message) : log.warn(message);
179
+ void Promise.resolve(result).catch(() => { });
180
+ }
181
+ catch {
182
+ // Nowhere left to report to; the logger must never stop the scheduler.
183
+ }
184
+ }
185
+ /** Release a job's in-flight guard. Only ever called for the job it belongs to. */
186
+ release(job) {
187
+ job.runningAtMs = undefined;
188
+ job.skipReported = false;
189
+ }
190
+ register(key, callback, interval, runOnInit = false) {
191
+ const seconds = this.toSeconds(interval);
192
+ // Fail fast rather than clamp. An interval that is not wholly a number is a
193
+ // programming error — a cron expression handed to the legacy class, or a
194
+ // duration with a unit on it (`'1h'`, `'30s'`) — and clamping or truncating
195
+ // it would silently run a job intended for every hour once per second,
196
+ // hammering whatever the callback talks to. Throwing surfaces it at the call
197
+ // site, at boot, before anything is scheduled. A degenerate-but-numeric
198
+ // interval (`'0'`, `'-5'`) is a different case: it is interpretable as "as
199
+ // often as possible" and is clamped to the floor with one warning.
200
+ if (seconds === null) {
201
+ throw new TypeError(`Cron job ${JSON.stringify(key)} has an invalid interval ${JSON.stringify(interval)}: `
202
+ + 'expected a value that is wholly a whole-second count (e.g. \'30\'). Units are not '
203
+ + 'accepted — \'1h\' is rejected, not read as 1. The legacy Cron class does not accept '
204
+ + 'cron expressions — use CronService for those.');
205
+ }
206
+ if (seconds < MIN_INTERVAL_SECONDS) {
207
+ this.report('warn', `Cron job ${JSON.stringify(key)} interval ${JSON.stringify(interval)} is below the `
208
+ + `${MIN_INTERVAL_SECONDS}s floor; clamping to ${MIN_INTERVAL_SECONDS}s`);
209
+ }
210
+ const job = { callback, interval, key, nextTrigger: 0 };
211
+ this.jobs[key] = job;
212
+ this.setNextTrigger(job);
213
+ this.heap.push(job);
214
+ if (config.debug) {
215
+ this.log(`job has been registered with interval: ${interval}`, key);
216
+ }
217
+ if (runOnInit)
218
+ this.safeInvoke(job, true);
219
+ this.scheduleNextRun();
220
+ }
221
+ unregister(key) {
222
+ const { heap, jobs } = this;
223
+ const job = jobs[key];
224
+ if (!job)
225
+ return;
226
+ delete jobs[key];
227
+ heap.remove(job);
228
+ if (config.debug)
229
+ this.log('job has been unregistered', key);
230
+ this.scheduleNextRun();
231
+ }
232
+ /**
233
+ * Read a job interval (whole seconds, as a string) as a number, WITHOUT
234
+ * applying the floor. Returns `null` when the value is not wholly numeric.
235
+ *
236
+ * `Number()` rather than `parseInt`, deliberately. `parseInt` stops at the
237
+ * first non-numeric character and so fails in the dangerous direction: it
238
+ * reads `'1h'` as 1, `'30s'` as 30 and `'5m'` as 5 — intervals 3600x, 120x and
239
+ * 60x faster than written, scheduled with no error attached to them. A `NaN`
240
+ * check catches a cron expression but not those, and those are the likelier
241
+ * typo: `stonyx-orm` hands `DB_SAVE_INTERVAL` straight through from the
242
+ * environment as a string. `Number()` reads the whole value or none of it, and
243
+ * also gets `'1e3'` (1000, not 1) and `' 60 '` (60) right.
244
+ *
245
+ * An empty or whitespace-only string is rejected rather than read as
246
+ * `Number('')` === 0, so a missing value is a loud error and not a job silently
247
+ * clamped to the floor.
248
+ */
249
+ toSeconds(interval) {
250
+ const trimmed = String(interval ?? '').trim();
251
+ if (!trimmed)
252
+ return null;
253
+ const seconds = Number(trimmed);
254
+ if (!Number.isFinite(seconds))
255
+ return null;
256
+ // Whole seconds; a fractional value truncates as it always has.
257
+ return Math.trunc(seconds);
258
+ }
259
+ /**
260
+ * Parse a job interval into a positive integer at or above the floor.
261
+ *
262
+ * Returns `null` when the value cannot be parsed at all, so callers can choose
263
+ * between failing fast (`register`) and falling back (`setNextTrigger`).
264
+ */
265
+ parseInterval(interval) {
266
+ const seconds = this.toSeconds(interval);
267
+ if (seconds === null)
268
+ return null;
269
+ return Math.max(MIN_INTERVAL_SECONDS, seconds);
270
+ }
271
+ setNextTrigger(job) {
272
+ // `register` rejects an unparseable interval up front; this floor is the
273
+ // backstop for a job object mutated after registration (`cron.jobs` is
274
+ // public, mutable state) and is what actually guarantees the drain loop
275
+ // terminates. Never let `nextTrigger` land on `NaN` or on `now`.
276
+ job.nextTrigger = getTimestamp() + (this.parseInterval(job.interval) ?? MIN_INTERVAL_SECONDS);
277
+ }
278
+ log(text, key = null) {
279
+ if (!config.cron?.log)
280
+ return;
281
+ const tag = key ? `Cron::${key}` : `Cron`;
282
+ log.cron(`${tag} - ${text}:`);
283
+ }
284
+ }
@@ -0,0 +1,13 @@
1
+ export interface HeapItem {
2
+ nextTrigger: number;
3
+ }
4
+ export default class MinHeap<T extends HeapItem> {
5
+ items: T[];
6
+ push(job: T): void;
7
+ pop(): T | undefined;
8
+ peek(): T | undefined;
9
+ bubbleUp(): void;
10
+ bubbleDown(): void;
11
+ remove(job: T): void;
12
+ isEmpty(): boolean;
13
+ }
@@ -0,0 +1,67 @@
1
+ export default class MinHeap {
2
+ items = [];
3
+ push(job) {
4
+ this.items.push(job);
5
+ this.bubbleUp();
6
+ }
7
+ pop() {
8
+ if (this.items.length <= 1)
9
+ return this.items.pop();
10
+ const top = this.items[0];
11
+ const last = this.items.pop();
12
+ if (last === undefined)
13
+ return top;
14
+ this.items[0] = last;
15
+ this.bubbleDown();
16
+ return top;
17
+ }
18
+ peek() {
19
+ return this.items[0];
20
+ }
21
+ bubbleUp() {
22
+ let idx = this.items.length - 1;
23
+ while (idx > 0) {
24
+ const parentIdx = Math.floor((idx - 1) / 2);
25
+ if (this.items[idx].nextTrigger >= this.items[parentIdx].nextTrigger)
26
+ break;
27
+ [this.items[idx], this.items[parentIdx]] = [this.items[parentIdx], this.items[idx]];
28
+ idx = parentIdx;
29
+ }
30
+ }
31
+ bubbleDown() {
32
+ let idx = 0;
33
+ const length = this.items.length;
34
+ while (true) {
35
+ const leftIdx = 2 * idx + 1;
36
+ const rightIdx = 2 * idx + 2;
37
+ let swapIdx = null;
38
+ if (leftIdx < length && this.items[leftIdx].nextTrigger < this.items[idx].nextTrigger) {
39
+ swapIdx = leftIdx;
40
+ }
41
+ if (rightIdx < length &&
42
+ this.items[rightIdx].nextTrigger < (swapIdx === null ? this.items[idx].nextTrigger : this.items[leftIdx].nextTrigger)) {
43
+ swapIdx = rightIdx;
44
+ }
45
+ if (swapIdx === null)
46
+ break;
47
+ [this.items[idx], this.items[swapIdx]] = [this.items[swapIdx], this.items[idx]];
48
+ idx = swapIdx;
49
+ }
50
+ }
51
+ remove(job) {
52
+ const idx = this.items.indexOf(job);
53
+ if (idx === -1)
54
+ return;
55
+ const end = this.items.pop();
56
+ if (end === undefined)
57
+ return;
58
+ if (idx < this.items.length) {
59
+ this.items[idx] = end;
60
+ this.bubbleUp();
61
+ this.bubbleDown();
62
+ }
63
+ }
64
+ isEmpty() {
65
+ return this.items.length === 0;
66
+ }
67
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Input normalization for AI-generated job definitions.
3
+ * Handles imperfect JSON from AI models: wrong casing, missing fields,
4
+ * flat-param recovery, type coercion.
5
+ */
6
+ interface RawSchedule {
7
+ kind?: string;
8
+ at?: string | number;
9
+ atMs?: number;
10
+ everyMs?: string | number;
11
+ expr?: string;
12
+ tz?: string;
13
+ }
14
+ interface RawPayload {
15
+ kind?: string;
16
+ message?: string;
17
+ text?: string;
18
+ [key: string]: unknown;
19
+ }
20
+ interface RawJobInput {
21
+ name?: string;
22
+ description?: string;
23
+ schedule?: RawSchedule;
24
+ payload?: RawPayload;
25
+ delivery?: Record<string, unknown>;
26
+ sessionTarget?: string;
27
+ wakeMode?: string;
28
+ enabled?: boolean;
29
+ deleteAfterRun?: boolean;
30
+ job?: RawJobInput;
31
+ message?: string;
32
+ text?: string;
33
+ [key: string]: unknown;
34
+ }
35
+ /**
36
+ * Normalize a schedule object. Infers kind from fields if missing.
37
+ */
38
+ export declare function normalizeSchedule(raw: unknown): RawSchedule;
39
+ /**
40
+ * Normalize a payload object. Infers kind from fields if missing.
41
+ */
42
+ export declare function normalizePayload(raw: unknown): RawPayload;
43
+ export declare function recoverFlatParams(params: RawJobInput): RawJobInput;
44
+ /**
45
+ * Normalize a complete job input for creation.
46
+ * Applies all normalization: schedule, payload, defaults.
47
+ */
48
+ export declare function normalizeJobInput(raw: RawJobInput): RawJobInput;
49
+ export {};
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Input normalization for AI-generated job definitions.
3
+ * Handles imperfect JSON from AI models: wrong casing, missing fields,
4
+ * flat-param recovery, type coercion.
5
+ */
6
+ /**
7
+ * Normalize a schedule object. Infers kind from fields if missing.
8
+ */
9
+ export function normalizeSchedule(raw) {
10
+ if (!raw || typeof raw !== 'object')
11
+ return raw;
12
+ const schedule = { ...raw };
13
+ // Infer kind from fields if missing
14
+ if (!schedule.kind) {
15
+ if (schedule.at || schedule.atMs)
16
+ schedule.kind = 'at';
17
+ else if (schedule.everyMs)
18
+ schedule.kind = 'every';
19
+ else if (schedule.expr)
20
+ schedule.kind = 'cron';
21
+ }
22
+ // Case normalization
23
+ if (typeof schedule.kind === 'string') {
24
+ schedule.kind = schedule.kind.toLowerCase();
25
+ }
26
+ // Legacy: atMs (number) -> at (ISO string)
27
+ if (schedule.atMs && !schedule.at) {
28
+ schedule.at = new Date(schedule.atMs).toISOString();
29
+ delete schedule.atMs;
30
+ }
31
+ // Coerce string everyMs to number
32
+ if (typeof schedule.everyMs === 'string') {
33
+ schedule.everyMs = Number(schedule.everyMs);
34
+ }
35
+ return schedule;
36
+ }
37
+ /**
38
+ * Normalize a payload object. Infers kind from fields if missing.
39
+ */
40
+ export function normalizePayload(raw) {
41
+ if (!raw || typeof raw !== 'object')
42
+ return raw;
43
+ const payload = { ...raw };
44
+ // Infer kind from fields
45
+ if (!payload.kind) {
46
+ if (payload.message)
47
+ payload.kind = 'agentTurn';
48
+ else if (payload.text)
49
+ payload.kind = 'systemEvent';
50
+ }
51
+ // Case normalization
52
+ if (typeof payload.kind === 'string') {
53
+ const lower = payload.kind.toLowerCase();
54
+ if (lower === 'agentturn')
55
+ payload.kind = 'agentTurn';
56
+ else if (lower === 'systemevent')
57
+ payload.kind = 'systemEvent';
58
+ }
59
+ return payload;
60
+ }
61
+ /**
62
+ * Recover a job object from flat parameters.
63
+ * AI models sometimes flatten nested fields to the top level.
64
+ */
65
+ const JOB_KEYS = new Set([
66
+ 'name', 'description', 'schedule', 'sessionTarget', 'payload',
67
+ 'delivery', 'enabled', 'deleteAfterRun', 'wakeMode',
68
+ ]);
69
+ export function recoverFlatParams(params) {
70
+ if (params.job && typeof params.job === 'object' && Object.keys(params.job).length > 0) {
71
+ return params.job;
72
+ }
73
+ const synthetic = {};
74
+ for (const key of Object.keys(params)) {
75
+ if (JOB_KEYS.has(key)) {
76
+ synthetic[key] = params[key];
77
+ }
78
+ }
79
+ // message/text are not JOB_KEYS but need to be recovered for payload wrapping
80
+ const message = params.message;
81
+ const text = params.text;
82
+ if (synthetic.schedule || synthetic.payload || message || text) {
83
+ // If message/text are at top level, wrap into payload
84
+ if (!synthetic.payload) {
85
+ if (message) {
86
+ synthetic.payload = { kind: 'agentTurn', message };
87
+ }
88
+ else if (text) {
89
+ synthetic.payload = { kind: 'systemEvent', text };
90
+ }
91
+ }
92
+ return synthetic;
93
+ }
94
+ return params;
95
+ }
96
+ /**
97
+ * Normalize a complete job input for creation.
98
+ * Applies all normalization: schedule, payload, defaults.
99
+ */
100
+ export function normalizeJobInput(raw) {
101
+ const job = { ...raw };
102
+ if (job.schedule) {
103
+ job.schedule = normalizeSchedule(job.schedule);
104
+ }
105
+ if (job.payload) {
106
+ job.payload = normalizePayload(job.payload);
107
+ }
108
+ // Default: enabled
109
+ if (job.enabled === undefined)
110
+ job.enabled = true;
111
+ // Default: wakeMode
112
+ if (!job.wakeMode)
113
+ job.wakeMode = 'now';
114
+ // Default: sessionTarget inferred from payload
115
+ if (!job.sessionTarget && job.payload) {
116
+ job.sessionTarget = job.payload.kind === 'systemEvent' ? 'main' : 'isolated';
117
+ }
118
+ // Default: deleteAfterRun for one-shot
119
+ if (job.deleteAfterRun === undefined && job.schedule?.kind === 'at') {
120
+ job.deleteAfterRun = true;
121
+ }
122
+ // Default: delivery for isolated agentTurn
123
+ if (!job.delivery && job.sessionTarget === 'isolated' && job.payload?.kind === 'agentTurn') {
124
+ job.delivery = { mode: 'announce' };
125
+ }
126
+ // Auto-generate name if missing
127
+ if (!job.name) {
128
+ job.name = inferName(job);
129
+ }
130
+ return job;
131
+ }
132
+ /**
133
+ * Infer a job name from schedule and payload.
134
+ */
135
+ function inferName(job) {
136
+ const parts = [];
137
+ if (job.schedule?.kind === 'at')
138
+ parts.push('One-shot');
139
+ else if (job.schedule?.kind === 'every')
140
+ parts.push('Recurring');
141
+ else if (job.schedule?.kind === 'cron')
142
+ parts.push('Scheduled');
143
+ if (job.payload?.kind === 'agentTurn')
144
+ parts.push('agent task');
145
+ else if (job.payload?.kind === 'systemEvent')
146
+ parts.push('system event');
147
+ return parts.join(' ') || 'Unnamed job';
148
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * In-memory run log for job execution history.
3
+ * Stores recent execution results per job with auto-pruning.
4
+ */
5
+ export interface RunLogEntry {
6
+ ts: number;
7
+ jobId: string;
8
+ status: string;
9
+ error?: string;
10
+ summary?: string;
11
+ runAtMs?: number;
12
+ durationMs?: number;
13
+ nextRunAtMs?: number;
14
+ }
15
+ export interface RunLogInput {
16
+ jobId: string;
17
+ status: string;
18
+ error?: string;
19
+ summary?: string;
20
+ runAtMs?: number;
21
+ durationMs?: number;
22
+ nextRunAtMs?: number;
23
+ }
24
+ export default class RunLog {
25
+ maxEntries: number;
26
+ entries: Map<string, RunLogEntry[]>;
27
+ constructor(maxEntriesPerJob?: number);
28
+ /**
29
+ * Record a job execution result.
30
+ */
31
+ record(entry: RunLogInput): void;
32
+ /**
33
+ * Get run history for a job.
34
+ */
35
+ get(jobId: string, limit?: number): RunLogEntry[];
36
+ /**
37
+ * Remove all entries for a job.
38
+ */
39
+ removeJob(jobId: string): void;
40
+ /**
41
+ * Clear all entries.
42
+ */
43
+ clear(): void;
44
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * In-memory run log for job execution history.
3
+ * Stores recent execution results per job with auto-pruning.
4
+ */
5
+ const DEFAULT_MAX_ENTRIES_PER_JOB = 100;
6
+ export default class RunLog {
7
+ maxEntries;
8
+ entries;
9
+ constructor(maxEntriesPerJob = DEFAULT_MAX_ENTRIES_PER_JOB) {
10
+ this.maxEntries = maxEntriesPerJob;
11
+ this.entries = new Map();
12
+ }
13
+ /**
14
+ * Record a job execution result.
15
+ */
16
+ record(entry) {
17
+ const log = {
18
+ ts: Date.now(),
19
+ jobId: entry.jobId,
20
+ status: entry.status,
21
+ error: entry.error,
22
+ summary: entry.summary,
23
+ runAtMs: entry.runAtMs,
24
+ durationMs: entry.durationMs,
25
+ nextRunAtMs: entry.nextRunAtMs,
26
+ };
27
+ if (!this.entries.has(entry.jobId)) {
28
+ this.entries.set(entry.jobId, []);
29
+ }
30
+ const logs = this.entries.get(entry.jobId);
31
+ if (!logs)
32
+ return;
33
+ logs.push(log);
34
+ // Auto-prune
35
+ if (logs.length > this.maxEntries) {
36
+ logs.splice(0, logs.length - this.maxEntries);
37
+ }
38
+ }
39
+ /**
40
+ * Get run history for a job.
41
+ */
42
+ get(jobId, limit = 20) {
43
+ const logs = this.entries.get(jobId);
44
+ if (!logs)
45
+ return [];
46
+ return logs.slice(-limit).reverse();
47
+ }
48
+ /**
49
+ * Remove all entries for a job.
50
+ */
51
+ removeJob(jobId) {
52
+ this.entries.delete(jobId);
53
+ }
54
+ /**
55
+ * Clear all entries.
56
+ */
57
+ clear() {
58
+ this.entries.clear();
59
+ }
60
+ }
@@ -0,0 +1,23 @@
1
+ export interface AtSchedule {
2
+ kind: 'at';
3
+ at: string | number;
4
+ }
5
+ export interface EverySchedule {
6
+ kind: 'every';
7
+ everyMs: number;
8
+ anchorMs?: number;
9
+ }
10
+ export interface CronSchedule {
11
+ kind: 'cron';
12
+ expr: string;
13
+ tz?: string;
14
+ }
15
+ export type Schedule = AtSchedule | EverySchedule | CronSchedule;
16
+ /**
17
+ * Compute the next run time for a schedule.
18
+ */
19
+ export declare function computeNextRunAtMs(schedule: Schedule, nowMs: number): number | undefined;
20
+ /**
21
+ * Validate a schedule definition.
22
+ */
23
+ export declare function validateSchedule(schedule: Schedule): void;