@cloudhal/schedules 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +103 -2
- package/dist/src/automation.d.ts +82 -0
- package/dist/src/automation.js +199 -0
- package/dist/src/budget-meter.d.ts +58 -0
- package/dist/src/budget-meter.js +130 -0
- package/dist/src/budget.d.ts +41 -0
- package/dist/src/budget.js +76 -0
- package/dist/src/builtins.d.ts +132 -0
- package/dist/src/builtins.js +473 -0
- package/dist/src/engine.d.ts +16 -0
- package/dist/src/engine.js +30 -0
- package/dist/src/errors.d.ts +22 -0
- package/dist/src/errors.js +56 -0
- package/dist/src/feed-check.d.ts +56 -0
- package/dist/src/feed-check.js +365 -0
- package/dist/src/findings.d.ts +185 -0
- package/dist/src/findings.js +287 -0
- package/dist/src/ids.d.ts +24 -0
- package/dist/src/ids.js +96 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/instant.d.ts +19 -0
- package/dist/src/instant.js +50 -0
- package/dist/src/notify.d.ts +50 -0
- package/dist/src/notify.js +60 -0
- package/dist/src/recurrence.d.ts +155 -0
- package/dist/src/recurrence.js +939 -0
- package/dist/src/release-when.d.ts +23 -0
- package/dist/src/release-when.js +55 -0
- package/dist/src/release.d.ts +198 -0
- package/dist/src/release.js +552 -0
- package/dist/src/scheduler.d.ts +490 -0
- package/dist/src/scheduler.js +1343 -0
- package/dist/src/sql/finish.d.ts +32 -0
- package/dist/src/sql/finish.js +103 -0
- package/dist/src/sql/index.d.ts +6 -0
- package/dist/src/sql/index.js +15 -0
- package/dist/src/sql/rows.d.ts +125 -0
- package/dist/src/sql/rows.js +297 -0
- package/dist/src/sql/store.d.ts +467 -0
- package/dist/src/sql/store.js +2166 -0
- package/dist/src/taint.d.ts +65 -0
- package/dist/src/taint.js +138 -0
- package/dist/src/text.d.ts +6 -0
- package/dist/src/text.js +25 -0
- package/dist/src/time-entry.d.ts +6 -0
- package/dist/src/time-entry.js +6 -0
- package/dist/src/time.d.ts +61 -0
- package/dist/src/time.js +140 -0
- package/package.json +69 -3
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The scheduler (RFC-0008 §8): one `runDue(now)` that every ticker calls — the every-minute cron,
|
|
3
|
+
* the `SchedulerClock` Durable Object alarm, the Node timer, the request backstop and a person's
|
|
4
|
+
* manual tick. D1 (SQLite on Node) is the only state; tickers only wake `runDue`.
|
|
5
|
+
*
|
|
6
|
+
* A tick does, in order (§8.1): (1) one overview read, which is also the heartbeat check;
|
|
7
|
+
* (2) due releases, one at a time per site in time order (D15), each claimed with a lease
|
|
8
|
+
* (§8.5) and executed by the injected `ReleaseExecutor` (`publishRelease` in core-ts, rollout
|
|
9
|
+
* step 5); expired leases are recovered first; (3) automation occurrences — built-in steps through
|
|
10
|
+
* the injected `BuiltinRunner`, agent-task windows recorded and closed; (4) due system jobs behind
|
|
11
|
+
* their watermarks; (5) the heartbeat write. Every unit first checks that its statements fit the
|
|
12
|
+
* `TickBudget` (S17); a tick that runs out reports `more`.
|
|
13
|
+
*
|
|
14
|
+
* At most one effect per occurrence (S3): run ids are derived from the occurrence, every claim is
|
|
15
|
+
* a compare-and-swap that also stores a fresh lease token, and every later write is guarded on that
|
|
16
|
+
* token, so two ticks racing on two stores over one database produce one effect.
|
|
17
|
+
*/
|
|
18
|
+
import type { Automation, BuiltinAutomationJob, Release, ReleaseConflict, ReleaseFailedOutcome, ReleaseOutcome, ReleasePublishedOutcome, ScheduleRun } from "@cloudhal/core-contracts";
|
|
19
|
+
import type { TickBudget, TickMeter, TickTrigger } from "./budget-meter.js";
|
|
20
|
+
import type { ScheduleNotification, ScheduleNotifier } from "./notify.js";
|
|
21
|
+
import type { Occurrence } from "./recurrence.js";
|
|
22
|
+
import { type ApprovalVerdict, type ExpiredReason, type PinnedItem } from "./release.js";
|
|
23
|
+
export type { TickBudget, TickTrigger } from "./budget-meter.js";
|
|
24
|
+
export interface TickReport {
|
|
25
|
+
/** Releases published plus automation runs and system jobs completed. */
|
|
26
|
+
ran: number;
|
|
27
|
+
/** Releases that ran more than a minute late, or became `missed`. */
|
|
28
|
+
late: number;
|
|
29
|
+
/** Releases and runs that ended `failed`, plus failed system jobs. */
|
|
30
|
+
failed: number;
|
|
31
|
+
/** Work is left that did not fit this tick's budget (the alarm re-arms at +1 s). */
|
|
32
|
+
more: boolean;
|
|
33
|
+
/** The next instant anything is due (canonical), when known. */
|
|
34
|
+
nextWakeAt?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The next instant something is really due (a release, an occurrence, a window close, a system
|
|
37
|
+
* job), before `wake: "minute"` caps `nextWakeAt` at the next minute boundary. A cron tick whose
|
|
38
|
+
* trigger fires late in the minute uses it to run that work on time (`followUpDelay`).
|
|
39
|
+
*/
|
|
40
|
+
nextDueAt?: string;
|
|
41
|
+
/** Statements this tick executed, when it was metered. */
|
|
42
|
+
statements?: number;
|
|
43
|
+
}
|
|
44
|
+
/** §8.1. */
|
|
45
|
+
export interface Scheduler {
|
|
46
|
+
/** Runs what is due within a budget. Idempotent: two concurrent calls never double an effect. */
|
|
47
|
+
runDue(now: Date, budget: TickBudget, trigger: TickTrigger): Promise<TickReport>;
|
|
48
|
+
/** The next instant anything is due (releases, occurrences, window closes, system jobs). */
|
|
49
|
+
nextWakeAt(now: Date): Promise<Date | undefined>;
|
|
50
|
+
}
|
|
51
|
+
export interface Heartbeat {
|
|
52
|
+
at: string;
|
|
53
|
+
trigger: TickTrigger;
|
|
54
|
+
/** The latest time the next heartbeat is expected (see `heartbeatNextWake`). */
|
|
55
|
+
nextWakeAt: string;
|
|
56
|
+
ran: number;
|
|
57
|
+
more: boolean;
|
|
58
|
+
lagSeconds?: number;
|
|
59
|
+
}
|
|
60
|
+
/** One read that tells an idle tick there is nothing to do (§8.2: an idle tick is cheap). */
|
|
61
|
+
export interface TickOverview {
|
|
62
|
+
/** Earliest `execute_at` of a `scheduled` release. */
|
|
63
|
+
nextReleaseAt?: string;
|
|
64
|
+
/** Earliest `next_run_at` of an `active` automation. */
|
|
65
|
+
nextAutomationAt?: string;
|
|
66
|
+
/** Earliest `next_run_at` of a system job. */
|
|
67
|
+
nextSystemJobAt?: string;
|
|
68
|
+
/** Earliest deadline of an open run: a `ready` window's close or a running run's lease. */
|
|
69
|
+
nextRunDeadline?: string;
|
|
70
|
+
/** Rows in `system_jobs` (to register new definitions without a write every tick). */
|
|
71
|
+
systemJobs: number;
|
|
72
|
+
heartbeat?: Heartbeat;
|
|
73
|
+
}
|
|
74
|
+
export interface StoredReleaseItem extends PinnedItem {
|
|
75
|
+
held: boolean;
|
|
76
|
+
}
|
|
77
|
+
export interface DueRelease {
|
|
78
|
+
release: Release;
|
|
79
|
+
items: StoredReleaseItem[];
|
|
80
|
+
executeAt: string;
|
|
81
|
+
sitePaused: boolean;
|
|
82
|
+
}
|
|
83
|
+
export interface RecoverableRelease extends DueRelease {
|
|
84
|
+
run: {
|
|
85
|
+
id: string;
|
|
86
|
+
attempt: number;
|
|
87
|
+
leaseToken: string;
|
|
88
|
+
occurrenceAt: string;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export interface DueAutomation {
|
|
92
|
+
automation: Automation;
|
|
93
|
+
sitePaused: boolean;
|
|
94
|
+
/** A built-in job's continuation (bounded steps span ticks, §11.1). */
|
|
95
|
+
cursor?: BuiltinCursor;
|
|
96
|
+
leaseUntil?: string;
|
|
97
|
+
}
|
|
98
|
+
/** Where a built-in job continues: the run it belongs to and the job's own position. */
|
|
99
|
+
export interface BuiltinCursor {
|
|
100
|
+
runId: string;
|
|
101
|
+
occurrenceAt: string;
|
|
102
|
+
attempt: number;
|
|
103
|
+
position?: unknown;
|
|
104
|
+
/** The run read untrusted text in an earlier step (§11.4); kept to its end. */
|
|
105
|
+
taint?: BuiltinTaint;
|
|
106
|
+
/** Run accounting so far: steps taken and the statements they executed (§11.5, S17). */
|
|
107
|
+
usage?: BuiltinUsage;
|
|
108
|
+
/** A person's "Run now" (§13): the scheduled `nextRunAt` the automation returns to afterwards. */
|
|
109
|
+
manual?: {
|
|
110
|
+
nextRunAt?: string;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** `ScheduleRun.taint` of a built-in run: when and through what it read untrusted text. */
|
|
114
|
+
export interface BuiltinTaint {
|
|
115
|
+
at: string;
|
|
116
|
+
tool: string;
|
|
117
|
+
}
|
|
118
|
+
/** A built-in run's budget use across its steps, recorded as `counts.steps` / `counts.statements`. */
|
|
119
|
+
export interface BuiltinUsage {
|
|
120
|
+
steps: number;
|
|
121
|
+
statements: number;
|
|
122
|
+
}
|
|
123
|
+
export type ReleaseFinish = {
|
|
124
|
+
state: "published";
|
|
125
|
+
outcome: ReleasePublishedOutcome;
|
|
126
|
+
} | {
|
|
127
|
+
state: "conflict" | "failed" | "expired" | "canceled" | "missed";
|
|
128
|
+
outcome: ReleaseOutcome;
|
|
129
|
+
};
|
|
130
|
+
export interface ClaimReleaseInput {
|
|
131
|
+
siteId: string;
|
|
132
|
+
releaseId: string;
|
|
133
|
+
expectedRev: number;
|
|
134
|
+
executeAt: string;
|
|
135
|
+
runId: string;
|
|
136
|
+
token: string;
|
|
137
|
+
now: string;
|
|
138
|
+
leaseUntil: string;
|
|
139
|
+
trigger: TickTrigger;
|
|
140
|
+
latenessSeconds: number;
|
|
141
|
+
adjusted?: "tzdata";
|
|
142
|
+
}
|
|
143
|
+
export interface SystemJobState {
|
|
144
|
+
name: string;
|
|
145
|
+
nextRunAt: string;
|
|
146
|
+
lastRunAt?: string;
|
|
147
|
+
lastOkAt?: string;
|
|
148
|
+
lastError?: string;
|
|
149
|
+
leaseUntil?: string;
|
|
150
|
+
}
|
|
151
|
+
/** What `runDue` needs from storage. `SqlScheduleStore` implements it over `SqlDriver`. */
|
|
152
|
+
export interface SchedulerStore {
|
|
153
|
+
overview(now: string): Promise<TickOverview>;
|
|
154
|
+
dueReleases(now: string, limit: number): Promise<DueRelease[]>;
|
|
155
|
+
expiredReleaseLeases(now: string, limit: number): Promise<RecoverableRelease[]>;
|
|
156
|
+
/** Claims a `scheduled` release (state `running`, a lease, the run row); false if another tick has it. */
|
|
157
|
+
claimRelease(input: ClaimReleaseInput): Promise<boolean>;
|
|
158
|
+
/** Takes over a `running` release whose lease expired; false if another tick did. */
|
|
159
|
+
reclaimRelease(input: {
|
|
160
|
+
releaseId: string;
|
|
161
|
+
runId: string;
|
|
162
|
+
oldToken: string;
|
|
163
|
+
token: string;
|
|
164
|
+
now: string;
|
|
165
|
+
leaseUntil: string;
|
|
166
|
+
trigger: TickTrigger;
|
|
167
|
+
latenessSeconds: number;
|
|
168
|
+
}): Promise<boolean>;
|
|
169
|
+
/** Ends a claimed release (guarded on the lease token). */
|
|
170
|
+
finishRelease(input: {
|
|
171
|
+
releaseId: string;
|
|
172
|
+
runId: string;
|
|
173
|
+
token: string;
|
|
174
|
+
now: string;
|
|
175
|
+
latenessSeconds: number;
|
|
176
|
+
finish: ReleaseFinish;
|
|
177
|
+
}): Promise<boolean>;
|
|
178
|
+
/** Keeps a claimed release `running` until `retryAt` (a transient error, §8.8). */
|
|
179
|
+
deferRelease(input: {
|
|
180
|
+
releaseId: string;
|
|
181
|
+
runId: string;
|
|
182
|
+
token: string;
|
|
183
|
+
now: string;
|
|
184
|
+
retryAt: string;
|
|
185
|
+
error: string;
|
|
186
|
+
}): Promise<boolean>;
|
|
187
|
+
/** Ends a not-yet-claimed `scheduled` release as `missed` or `expired` (CAS on `rev`). */
|
|
188
|
+
settleRelease(input: {
|
|
189
|
+
siteId: string;
|
|
190
|
+
releaseId: string;
|
|
191
|
+
expectedRev: number;
|
|
192
|
+
executeAt: string;
|
|
193
|
+
now: string;
|
|
194
|
+
outcome: ReleaseOutcome & {
|
|
195
|
+
kind: "missed" | "expired";
|
|
196
|
+
};
|
|
197
|
+
trigger: TickTrigger;
|
|
198
|
+
}): Promise<boolean>;
|
|
199
|
+
/** Moves a `scheduled` release later (never-early recompute, `adjusted: "tzdata"`). */
|
|
200
|
+
moveRelease(input: {
|
|
201
|
+
siteId: string;
|
|
202
|
+
releaseId: string;
|
|
203
|
+
expectedRev: number;
|
|
204
|
+
at: string;
|
|
205
|
+
now: string;
|
|
206
|
+
}): Promise<boolean>;
|
|
207
|
+
dueAutomations(now: string, limit: number): Promise<DueAutomation[]>;
|
|
208
|
+
/** Claims a built-in occurrence: the automation's lease, the run row (and a `skipped` row). */
|
|
209
|
+
claimBuiltinRun(input: {
|
|
210
|
+
automation: Automation;
|
|
211
|
+
occurrence: Occurrence;
|
|
212
|
+
runId: string;
|
|
213
|
+
attempt: number;
|
|
214
|
+
continuing: boolean;
|
|
215
|
+
coalesced?: {
|
|
216
|
+
runId: string;
|
|
217
|
+
occurrenceAt: string;
|
|
218
|
+
count: number;
|
|
219
|
+
};
|
|
220
|
+
token: string;
|
|
221
|
+
now: string;
|
|
222
|
+
leaseUntil: string;
|
|
223
|
+
trigger: TickTrigger;
|
|
224
|
+
latenessSeconds: number;
|
|
225
|
+
}): Promise<boolean>;
|
|
226
|
+
/** Ends (or pauses between steps) a built-in run; guarded on the automation's lease token. */
|
|
227
|
+
finishBuiltinRun(input: {
|
|
228
|
+
automation: Automation;
|
|
229
|
+
runId: string;
|
|
230
|
+
token: string;
|
|
231
|
+
now: string;
|
|
232
|
+
step: {
|
|
233
|
+
done: false;
|
|
234
|
+
cursor: BuiltinCursor;
|
|
235
|
+
} | {
|
|
236
|
+
done: true;
|
|
237
|
+
run: Pick<ScheduleRun, "state"> & Partial<Pick<ScheduleRun, "outcome" | "summary" | "counts" | "error" | "taint">> & {
|
|
238
|
+
findings?: unknown[];
|
|
239
|
+
};
|
|
240
|
+
nextRunAt?: string;
|
|
241
|
+
consecutiveFailures: number;
|
|
242
|
+
pause?: {
|
|
243
|
+
state: "auto-paused";
|
|
244
|
+
pausedReason: "failures" | "budget";
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
}): Promise<boolean>;
|
|
248
|
+
/** Records an agent task's latest occurrence (`ready`, or `missed` when its window already closed) and moves `next_run_at`. */
|
|
249
|
+
recordAgentOccurrence(input: {
|
|
250
|
+
automation: Automation;
|
|
251
|
+
occurrence: Occurrence;
|
|
252
|
+
runId: string;
|
|
253
|
+
window: {
|
|
254
|
+
opensAt: string;
|
|
255
|
+
closesAt: string;
|
|
256
|
+
};
|
|
257
|
+
state: "ready" | "missed";
|
|
258
|
+
nextRunAt?: string;
|
|
259
|
+
consecutiveFailures: number;
|
|
260
|
+
pause?: {
|
|
261
|
+
state: "auto-paused";
|
|
262
|
+
pausedReason: "failures" | "budget";
|
|
263
|
+
};
|
|
264
|
+
now: string;
|
|
265
|
+
trigger: TickTrigger;
|
|
266
|
+
latenessSeconds: number;
|
|
267
|
+
}): Promise<boolean>;
|
|
268
|
+
/** Closes windows that passed without `begin_run` (`missed`) and agent runs past their end (`failed`). */
|
|
269
|
+
closeExpiredRuns(now: string): Promise<{
|
|
270
|
+
missed: number;
|
|
271
|
+
failed: number;
|
|
272
|
+
}>;
|
|
273
|
+
ensureSystemJobs(jobs: readonly {
|
|
274
|
+
name: string;
|
|
275
|
+
nextRunAt: string;
|
|
276
|
+
}[]): Promise<void>;
|
|
277
|
+
dueSystemJobs(now: string): Promise<SystemJobState[]>;
|
|
278
|
+
claimSystemJob(input: {
|
|
279
|
+
name: string;
|
|
280
|
+
token: string;
|
|
281
|
+
now: string;
|
|
282
|
+
leaseUntil: string;
|
|
283
|
+
}): Promise<boolean>;
|
|
284
|
+
finishSystemJob(input: {
|
|
285
|
+
name: string;
|
|
286
|
+
token: string;
|
|
287
|
+
now: string;
|
|
288
|
+
ok: boolean;
|
|
289
|
+
nextRunAt: string;
|
|
290
|
+
error?: string;
|
|
291
|
+
}): Promise<boolean>;
|
|
292
|
+
writeHeartbeat(heartbeat: Heartbeat): Promise<void>;
|
|
293
|
+
}
|
|
294
|
+
export interface ReleaseExecutionInput {
|
|
295
|
+
release: Release;
|
|
296
|
+
items: StoredReleaseItem[];
|
|
297
|
+
executeAt: string;
|
|
298
|
+
run: {
|
|
299
|
+
id: string;
|
|
300
|
+
attempt: number;
|
|
301
|
+
leaseToken: string;
|
|
302
|
+
recovering: boolean;
|
|
303
|
+
};
|
|
304
|
+
now: Date;
|
|
305
|
+
latenessSeconds: number;
|
|
306
|
+
trigger: TickTrigger;
|
|
307
|
+
}
|
|
308
|
+
export type ReleaseExecution =
|
|
309
|
+
/** Applied. `committed`: the executor's own batch already moved the release and run (§6.3 statements 5–7). */
|
|
310
|
+
{
|
|
311
|
+
kind: "published";
|
|
312
|
+
snapshotId: string;
|
|
313
|
+
baseSnapshotId: string;
|
|
314
|
+
purge: ReleasePublishedOutcome["purge"];
|
|
315
|
+
committed: boolean;
|
|
316
|
+
} | {
|
|
317
|
+
kind: "conflict";
|
|
318
|
+
conflicts: ReleaseConflict[];
|
|
319
|
+
} | {
|
|
320
|
+
kind: "failed";
|
|
321
|
+
code: ReleaseFailedOutcome["code"];
|
|
322
|
+
message?: string;
|
|
323
|
+
} | {
|
|
324
|
+
kind: "expired";
|
|
325
|
+
reason: ExpiredReason;
|
|
326
|
+
}
|
|
327
|
+
/** The site or a node no longer exists (§10.6). */
|
|
328
|
+
| {
|
|
329
|
+
kind: "canceled";
|
|
330
|
+
}
|
|
331
|
+
/** Transient (a D1 error, CPU, a lost site CAS twice): retry with back-off inside the hour. */
|
|
332
|
+
| {
|
|
333
|
+
kind: "retry";
|
|
334
|
+
error: string;
|
|
335
|
+
};
|
|
336
|
+
/** Applies a claimed release — `publishRelease` in core-ts (rollout step 5). */
|
|
337
|
+
export interface ReleaseExecutor {
|
|
338
|
+
/** Statements one execution may take (reads + its batch), for the budget check. */
|
|
339
|
+
readonly cost: number;
|
|
340
|
+
execute(input: ReleaseExecutionInput): Promise<ReleaseExecution>;
|
|
341
|
+
}
|
|
342
|
+
export interface BuiltinStepInput {
|
|
343
|
+
automation: Automation;
|
|
344
|
+
job: BuiltinAutomationJob;
|
|
345
|
+
run: {
|
|
346
|
+
id: string;
|
|
347
|
+
occurrenceAt: string;
|
|
348
|
+
attempt: number;
|
|
349
|
+
};
|
|
350
|
+
position?: unknown;
|
|
351
|
+
now: Date;
|
|
352
|
+
trigger: TickTrigger;
|
|
353
|
+
}
|
|
354
|
+
export interface BuiltinStepResult {
|
|
355
|
+
/** False: more steps follow on later ticks from `position`. */
|
|
356
|
+
done: boolean;
|
|
357
|
+
position?: unknown;
|
|
358
|
+
outcome?: ScheduleRun["outcome"];
|
|
359
|
+
counts?: Record<string, number>;
|
|
360
|
+
summary?: string;
|
|
361
|
+
/** Studio-only findings (≤ 200 per run). */
|
|
362
|
+
findings?: unknown[];
|
|
363
|
+
/** The step read untrusted text (404 paths, a fetched document): the run is tainted (§11.4). */
|
|
364
|
+
taint?: BuiltinTaint;
|
|
365
|
+
/** Delivered through `RunDueDeps.notify` once the run's end is committed (done steps only). */
|
|
366
|
+
notify?: ScheduleNotification[];
|
|
367
|
+
}
|
|
368
|
+
/** Runs one bounded step of a built-in job (rollout step 9). */
|
|
369
|
+
export interface BuiltinRunner {
|
|
370
|
+
/** Statements one step may take (the largest job's, when `costOf` is absent). */
|
|
371
|
+
readonly cost: number;
|
|
372
|
+
/** Statements one step of this job may take. */
|
|
373
|
+
costOf?(job: BuiltinAutomationJob): number;
|
|
374
|
+
step(input: BuiltinStepInput): Promise<BuiltinStepResult>;
|
|
375
|
+
}
|
|
376
|
+
/** A deployment-wide periodic job behind a watermark (§8.2): `analytics.rollup`, `comments.hourly`, … */
|
|
377
|
+
export interface SystemJobDefinition {
|
|
378
|
+
name: string;
|
|
379
|
+
/** Statements one run may take. */
|
|
380
|
+
cost: number;
|
|
381
|
+
/** The next watermark strictly after `after`. */
|
|
382
|
+
next(after: Date): Date;
|
|
383
|
+
run(context: {
|
|
384
|
+
now: Date;
|
|
385
|
+
trigger: TickTrigger;
|
|
386
|
+
}): Promise<void>;
|
|
387
|
+
}
|
|
388
|
+
/** Every hour at `:minute` (UTC). */
|
|
389
|
+
export declare function hourlyAt(minute: number): (after: Date) => Date;
|
|
390
|
+
/** Every day at `hour:minute` (UTC). */
|
|
391
|
+
export declare function dailyAt(hour: number, minute: number): (after: Date) => Date;
|
|
392
|
+
/** Idle ticks write the heartbeat every fifth minute (§8.7). */
|
|
393
|
+
export declare const HEARTBEAT_EVERY_MS: number;
|
|
394
|
+
/** Stalled when this far past the expected wake while something is due within 24 h (§8.7). */
|
|
395
|
+
export declare const STALL_GRACE_DUE_MS: number;
|
|
396
|
+
/** Stalled at all when this far past it. */
|
|
397
|
+
export declare const STALL_GRACE_MS: number;
|
|
398
|
+
export declare const SYSTEM_JOB_LEASE_MS: number;
|
|
399
|
+
export declare function shouldWriteHeartbeat(prev: Heartbeat | undefined, now: string, didWork: boolean): boolean;
|
|
400
|
+
/**
|
|
401
|
+
* The heartbeat's `next_wake_at`: the later of the next wake and the next heartbeat write an idle
|
|
402
|
+
* cron tick owes (`at + 5 min`), so a quiet scheduler is not reported stalled between writes.
|
|
403
|
+
*/
|
|
404
|
+
export declare function heartbeatNextWake(at: string, nextWake: string | undefined): string;
|
|
405
|
+
export type SchedulerHealthState = "running" | "late" | "stopped" | "unknown";
|
|
406
|
+
/**
|
|
407
|
+
* §8.7: stalled = `now > next_wake_at + 2 min` while something is due within 24 h, or
|
|
408
|
+
* `now > next_wake_at + 10 min` at all. `late` is the first, `stopped` the second.
|
|
409
|
+
*/
|
|
410
|
+
export declare function schedulerHealth(heartbeat: Heartbeat | undefined, now: string | Date | number, options: {
|
|
411
|
+
dueWithin24h: boolean;
|
|
412
|
+
}): {
|
|
413
|
+
state: SchedulerHealthState;
|
|
414
|
+
healthy: boolean;
|
|
415
|
+
lastTickAt?: string;
|
|
416
|
+
nextWakeAt?: string;
|
|
417
|
+
};
|
|
418
|
+
/**
|
|
419
|
+
* The request backstop (§8.6): an admin request runs `runDue(…, "request")` in `waitUntil` when the
|
|
420
|
+
* heartbeat is missing or more than 2 minutes behind its `next_wake_at`.
|
|
421
|
+
*/
|
|
422
|
+
export declare function backstopDue(heartbeat: Heartbeat | undefined, now: string | Date | number): boolean;
|
|
423
|
+
/** The next instant anything in the overview is due. */
|
|
424
|
+
export declare function overviewNextWake(o: TickOverview): string | undefined;
|
|
425
|
+
/** Statement estimates per path, checked before each unit (measured by the conformance suite). */
|
|
426
|
+
export declare const PATH_COST: {
|
|
427
|
+
readonly overview: 1;
|
|
428
|
+
readonly dueReleases: 1;
|
|
429
|
+
readonly recover: 1;
|
|
430
|
+
readonly releaseClaim: 3;
|
|
431
|
+
readonly releaseFinish: 4;
|
|
432
|
+
readonly releaseSettle: 3;
|
|
433
|
+
readonly releaseMove: 1;
|
|
434
|
+
readonly dueAutomations: 1;
|
|
435
|
+
readonly builtinClaim: 4;
|
|
436
|
+
readonly builtinFinish: 3;
|
|
437
|
+
readonly agentOccurrence: 3;
|
|
438
|
+
readonly closeRuns: 5;
|
|
439
|
+
readonly ensureJobs: 1;
|
|
440
|
+
readonly dueJobs: 1;
|
|
441
|
+
readonly jobClaim: 1;
|
|
442
|
+
readonly jobFinish: 1;
|
|
443
|
+
readonly heartbeat: 1;
|
|
444
|
+
};
|
|
445
|
+
export interface RunDueDeps {
|
|
446
|
+
store: SchedulerStore;
|
|
447
|
+
trigger: TickTrigger;
|
|
448
|
+
budget: TickBudget;
|
|
449
|
+
/** The meter wrapping the store's driver; without it only `maxReleases` and the deadline limit the tick. */
|
|
450
|
+
meter?: TickMeter;
|
|
451
|
+
/** Applies releases; without it due releases are left for a tick that has one. */
|
|
452
|
+
releases?: ReleaseExecutor;
|
|
453
|
+
/** Policy re-checks at run time (§10.6); `baseApprovalVerdict` is applied first either way. */
|
|
454
|
+
checkApproval?: (release: Release, now: Date) => ApprovalVerdict | Promise<ApprovalVerdict>;
|
|
455
|
+
builtins?: BuiltinRunner;
|
|
456
|
+
/** Notifications of finished, failed and paused automation runs (§14); default: none. */
|
|
457
|
+
notify?: ScheduleNotifier;
|
|
458
|
+
systemJobs?: readonly SystemJobDefinition[];
|
|
459
|
+
/** `CLOUDHAL_AUTOMATIONS=off` → false: automations stop, releases continue (§11.6). */
|
|
460
|
+
automationsEnabled?: boolean;
|
|
461
|
+
/** `minute` (cron: wakes every minute anyway) or `computed` (alarm, Node timer). */
|
|
462
|
+
wake?: "minute" | "computed";
|
|
463
|
+
log?: (line: string) => void;
|
|
464
|
+
/** Lease tokens (tests may pin them). */
|
|
465
|
+
newToken?: () => string;
|
|
466
|
+
}
|
|
467
|
+
/** Runs what is due (§8.1). See the module comment for the order and guarantees. */
|
|
468
|
+
export declare function runDue(nowDate: Date, deps: RunDueDeps): Promise<TickReport>;
|
|
469
|
+
/**
|
|
470
|
+
* Executes a release the caller already claimed — a person's "Publish now"
|
|
471
|
+
* (`SqlScheduleStore.claimManualRun`) — and records its end exactly as a tick does: published,
|
|
472
|
+
* conflict, failed, expired or canceled, or a transient error kept `running` for a retry.
|
|
473
|
+
*/
|
|
474
|
+
export declare function runClaimedRelease(deps: Pick<RunDueDeps, "store" | "trigger" | "log"> & {
|
|
475
|
+
releases: ReleaseExecutor;
|
|
476
|
+
}, claimed: {
|
|
477
|
+
release: Release;
|
|
478
|
+
items: StoredReleaseItem[];
|
|
479
|
+
executeAt: string;
|
|
480
|
+
runId: string;
|
|
481
|
+
token: string;
|
|
482
|
+
now: Date;
|
|
483
|
+
attempt?: number;
|
|
484
|
+
}): Promise<TickReport>;
|
|
485
|
+
/** Builds a `Scheduler` over a store factory: each tick gets a fresh meter (and so a fresh budget). */
|
|
486
|
+
export declare function createScheduler(options: {
|
|
487
|
+
/** The store for one tick, over a driver wrapped by `meter.wrap(...)`. */
|
|
488
|
+
openStore: (meter: TickMeter) => SchedulerStore;
|
|
489
|
+
newMeter: (budget: TickBudget) => TickMeter;
|
|
490
|
+
} & Omit<RunDueDeps, "store" | "trigger" | "budget" | "meter">): Scheduler;
|