@mark1russell7/workflow-runner 0.1.0 → 0.2.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/build/{engine-B_0OOo1N.d.ts → engine-bAZZ3ZT9.d.ts} +78 -4
- package/build/engine-bAZZ3ZT9.d.ts.map +1 -0
- package/build/index.d.ts +2 -2
- package/build/index.js +204 -63
- package/build/index.js.map +1 -1
- package/build/service.d.ts +1 -1
- package/package.json +2 -2
- package/src/engine.ts +153 -20
- package/src/index.ts +3 -1
- package/src/record.ts +39 -2
- package/build/engine-B_0OOo1N.d.ts.map +0 -1
package/src/engine.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { canonicalHash } from "@mark1russell7/rigor-generators";
|
|
|
6
6
|
import { backoffDelay } from "@mark1russell7/service/backoff";
|
|
7
7
|
import { type ProcedureInfo, checkWorkflow, workflowMachine } from "@mark1russell7/workflow";
|
|
8
8
|
import { RUN_MACHINE, STEP_MACHINE } from "./machines.ts";
|
|
9
|
-
import type
|
|
9
|
+
import { type Outcome, type RunEntry, type RunStatus, type RunStore, type StepStatus, type TriggerEvent, RecordConflict, RunHeld } from "./record.ts";
|
|
10
10
|
|
|
11
11
|
/** The facts of one procedure for the runner: the facts of the check, and the removal of a second call. */
|
|
12
12
|
export interface RunnerProcedure extends ProcedureInfo {
|
|
@@ -29,6 +29,11 @@ export interface ProcedureCall {
|
|
|
29
29
|
readonly try: number;
|
|
30
30
|
/** The workspace that runs the workflow. */
|
|
31
31
|
readonly workspace: string;
|
|
32
|
+
/**
|
|
33
|
+
* The depth of the run in a chain of calls. A host that starts a workflow from this call gives it this depth plus 1.
|
|
34
|
+
* The host takes the depth from this field, not from the inputs of the call.
|
|
35
|
+
*/
|
|
36
|
+
readonly depth: number;
|
|
32
37
|
/** The signal of the time limit of the step. */
|
|
33
38
|
readonly signal: AbortSignal;
|
|
34
39
|
}
|
|
@@ -56,6 +61,10 @@ export interface RunnerLimits {
|
|
|
56
61
|
readonly outcomeBytes?: number;
|
|
57
62
|
/** The most job instances that run at the same time. The default is 8. */
|
|
58
63
|
readonly jobs?: number;
|
|
64
|
+
/** The time of a lease on a run, in milliseconds. The runner renews it after each third of this time. The default is 60000. */
|
|
65
|
+
readonly lease?: number;
|
|
66
|
+
/** The most depth of a call trigger. A deeper chain of calls is refused. The default is 8. */
|
|
67
|
+
readonly callDepth?: number;
|
|
59
68
|
}
|
|
60
69
|
|
|
61
70
|
/** The ports of the runner. The runner names no host: the host gives each port. */
|
|
@@ -83,6 +92,11 @@ export interface RunnerOptions {
|
|
|
83
92
|
readonly callNamespaces?: readonly string[];
|
|
84
93
|
/** The limits. */
|
|
85
94
|
readonly limits?: RunnerLimits;
|
|
95
|
+
/**
|
|
96
|
+
* The id of this runner in the leases of the runs. A runner with the same id resumes its runs at once after a restart.
|
|
97
|
+
* Another runner waits for the end of the lease. The default is a new random id.
|
|
98
|
+
*/
|
|
99
|
+
readonly holder?: string;
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
/** A request to start a run. */
|
|
@@ -111,8 +125,26 @@ export interface WorkflowRunner {
|
|
|
111
125
|
readonly runIdOf: (request: StartRequest) => string;
|
|
112
126
|
/** This function starts a run. A request with a run id that has a record resumes that run. */
|
|
113
127
|
readonly start: (request: StartRequest) => Promise<RunResult>;
|
|
114
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* This function resumes a run from its record. The record holds the document, thus a changed document makes no change.
|
|
130
|
+
* A live lease of another runner gives the error `RunHeld`.
|
|
131
|
+
*/
|
|
115
132
|
readonly resume: (run: string) => Promise<RunResult>;
|
|
133
|
+
/** This function resumes each open run of the store with no live lease of another runner. The store must give `open`. */
|
|
134
|
+
readonly recover: (options?: RecoverOptions) => Promise<RecoverReport>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The options of a recovery. */
|
|
138
|
+
export interface RecoverOptions {
|
|
139
|
+
/** The most runs that resume at the same time. The default is 4. */
|
|
140
|
+
readonly concurrency?: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The report of a recovery: the runs that ended, the runs that another runner holds, and the runs that failed to resume. */
|
|
144
|
+
export interface RecoverReport {
|
|
145
|
+
readonly resumed: readonly { readonly run: string; readonly status: RunStatus; readonly why: string }[];
|
|
146
|
+
readonly held: readonly { readonly run: string; readonly holder: string; readonly until: number }[];
|
|
147
|
+
readonly failed: readonly { readonly run: string; readonly why: string }[];
|
|
116
148
|
}
|
|
117
149
|
|
|
118
150
|
type Rec = Readonly<Record<string, unknown>>;
|
|
@@ -189,8 +221,11 @@ function jobOrder(jobs: Rec): readonly string[] | undefined {
|
|
|
189
221
|
return order;
|
|
190
222
|
}
|
|
191
223
|
|
|
192
|
-
/**
|
|
193
|
-
|
|
224
|
+
/**
|
|
225
|
+
* The problems that make a document `invalid` for the runner, besides the errors of `checkWorkflow`. The check holds the
|
|
226
|
+
* retry rule, with the limit of tries of the runner.
|
|
227
|
+
*/
|
|
228
|
+
function runnerProblems(doc: unknown): readonly string[] {
|
|
194
229
|
const problems: string[] = [];
|
|
195
230
|
const jobs = fieldsOrEmpty(valueOf(doc)["jobs"]);
|
|
196
231
|
if (jobOrder(jobs) === undefined) problems.push("The needs of the jobs have a cycle or name no job.");
|
|
@@ -198,18 +233,35 @@ function runnerProblems(doc: unknown, limits: Required<RunnerLimits>): readonly
|
|
|
198
233
|
listOf(valueOf(job)["steps"]).forEach((step, index) => {
|
|
199
234
|
const s = valueOf(step);
|
|
200
235
|
if (typeof s["id"] !== "string") problems.push(`The step ${String(index + 1)} of the job ${id} has no id. The runner addresses each effect by the step id.`);
|
|
201
|
-
const retry = s["retry"];
|
|
202
|
-
if (retry !== undefined) {
|
|
203
|
-
const r = fieldsOrEmpty(retry);
|
|
204
|
-
const tries = r["tries"];
|
|
205
|
-
if (typeof tries !== "number" || !Number.isInteger(tries) || tries < 1 || tries > limits.tries) problems.push(`The retry of the step ${String(s["id"])} of the job ${id} needs tries: a whole number from 1 to ${String(limits.tries)}.`);
|
|
206
|
-
for (const key of ["delay", "factor", "cap"]) if (r[key] !== undefined && (typeof r[key] !== "number" || (r[key] as number) < 0)) problems.push(`The retry ${key} of the step ${String(s["id"])} of the job ${id} must be a number of 0 or more.`);
|
|
207
|
-
}
|
|
208
236
|
});
|
|
209
237
|
}
|
|
210
238
|
return problems;
|
|
211
239
|
}
|
|
212
240
|
|
|
241
|
+
/** This function gives the fields of a trigger that are present among `keys`, for a record with no undefined value. */
|
|
242
|
+
function present(trigger: TriggerEvent, keys: readonly ("event" | "type" | "from" | "collection")[]): Record<string, string> {
|
|
243
|
+
const out: Record<string, string> = {};
|
|
244
|
+
for (const key of keys) {
|
|
245
|
+
const value = trigger[key];
|
|
246
|
+
if (value !== undefined) out[key] = value;
|
|
247
|
+
}
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* This function gives the source of a trigger for the run id: the kind and the id of the trigger. It adds the name of an
|
|
253
|
+
* event or the collection of a feed when the trigger has one. Two events with one id and two names thus get two runs. A trigger without
|
|
254
|
+
* these fields keeps the run id of before.
|
|
255
|
+
*/
|
|
256
|
+
function sourceOf(trigger: TriggerEvent): Record<string, string> {
|
|
257
|
+
return { kind: trigger.kind, id: trigger.id, ...present(trigger, trigger.collection === undefined ? ["event"] : ["event", "from", "collection"]) };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** This function gives the value that an expression reads as `event`: the kind as `name`, the id, the data and the source. */
|
|
261
|
+
function eventValueOf(trigger: TriggerEvent): Readonly<Record<string, unknown>> {
|
|
262
|
+
return { name: trigger.kind, id: trigger.id, data: trigger.data ?? null, ...present(trigger, ["event", "type", "from", "collection"]) };
|
|
263
|
+
}
|
|
264
|
+
|
|
213
265
|
/** The default value of each input of a document that has one. */
|
|
214
266
|
function defaultsOf(doc: unknown): Record<string, unknown> {
|
|
215
267
|
const out: Record<string, unknown> = {};
|
|
@@ -217,7 +269,7 @@ function defaultsOf(doc: unknown): Record<string, unknown> {
|
|
|
217
269
|
return out;
|
|
218
270
|
}
|
|
219
271
|
|
|
220
|
-
const DEFAULT_LIMITS: Required<RunnerLimits> = { matrix: 256, tries: 10, outcomeBytes: 1_000_000, jobs: 8 };
|
|
272
|
+
const DEFAULT_LIMITS: Required<RunnerLimits> = { matrix: 256, tries: 10, outcomeBytes: 1_000_000, jobs: 8, lease: 60_000, callDepth: 8 };
|
|
221
273
|
|
|
222
274
|
/**
|
|
223
275
|
* This function makes a runner of workflows. The runner records each run in the store, and each record is the log of
|
|
@@ -232,9 +284,10 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
232
284
|
const wall = options.wall ?? Date.now;
|
|
233
285
|
const namespaces = ["procedure", ...(options.callNamespaces ?? [])];
|
|
234
286
|
const callCaps: readonly Cap[] = namespaces.map((namespace) => [namespace]);
|
|
287
|
+
const holder = options.holder ?? `runner-${globalThis.crypto.randomUUID()}`;
|
|
235
288
|
|
|
236
289
|
const runIdOf = (request: StartRequest): string =>
|
|
237
|
-
canonicalHash({ doc: canonicalHash(request.doc), owner: request.owner, trigger:
|
|
290
|
+
canonicalHash({ doc: canonicalHash(request.doc), owner: request.owner, trigger: sourceOf(request.trigger) });
|
|
238
291
|
|
|
239
292
|
async function start(request: StartRequest): Promise<RunResult> {
|
|
240
293
|
const id = runIdOf(request);
|
|
@@ -261,15 +314,47 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
261
314
|
if (first?.type !== "start") throw new Error(`The run ${id} has no record.`);
|
|
262
315
|
const prior = priorOf(entries);
|
|
263
316
|
if (prior.end !== undefined) return { run: id, status: prior.end.status, why: prior.end.why, entries };
|
|
317
|
+
const lease = entries.findLast((entry) => entry.type === "lease");
|
|
318
|
+
if (lease !== undefined && lease.holder !== holder && lease.until > wall()) throw new RunHeld(id, lease.holder, lease.until);
|
|
264
319
|
return new Run(id, first, entries, prior).go();
|
|
265
320
|
}
|
|
266
321
|
|
|
322
|
+
async function recover(recoverOptions: RecoverOptions = {}): Promise<RecoverReport> {
|
|
323
|
+
if (options.store.open === undefined) throw new Error("The store gives no list of open runs: it has no function open.");
|
|
324
|
+
const waiting = [...(await options.store.open())];
|
|
325
|
+
const resumed: { run: string; status: RunStatus; why: string }[] = [];
|
|
326
|
+
const held: { run: string; holder: string; until: number }[] = [];
|
|
327
|
+
const failed: { run: string; why: string }[] = [];
|
|
328
|
+
const worker = async (): Promise<void> => {
|
|
329
|
+
for (let id = waiting.shift(); id !== undefined; id = waiting.shift()) {
|
|
330
|
+
try {
|
|
331
|
+
const result = await resume(id);
|
|
332
|
+
resumed.push({ run: id, status: result.status, why: result.why });
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (error instanceof RunHeld) held.push({ run: id, holder: error.holder, until: error.until });
|
|
335
|
+
// Another runner wrote the record first: it holds the run now.
|
|
336
|
+
else if (error instanceof RecordConflict) held.push({ run: id, holder: "another runner", until: wall() });
|
|
337
|
+
else failed.push({ run: id, why: error instanceof Error ? error.message : String(error) });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
const width = Math.max(1, Math.floor(recoverOptions.concurrency ?? 4));
|
|
342
|
+
await Promise.all(Array.from({ length: width }, worker));
|
|
343
|
+
return { resumed, held, failed };
|
|
344
|
+
}
|
|
345
|
+
|
|
267
346
|
/** One run: its writer, its state and its jobs. */
|
|
268
347
|
class Run {
|
|
269
348
|
private tail: Promise<void> = Promise.resolve();
|
|
270
349
|
private runState: string;
|
|
271
350
|
private halt: { readonly status: "refused" | "in-doubt"; readonly why: string } | undefined;
|
|
272
351
|
private failed = false;
|
|
352
|
+
private ended = false;
|
|
353
|
+
private renewal: (() => void) | undefined;
|
|
354
|
+
/** The end of the last lease that this runner wrote. */
|
|
355
|
+
private leaseUntil = 0;
|
|
356
|
+
/** This signal aborts when the runner loses the lease: each call in flight then stops. */
|
|
357
|
+
private readonly lost = new AbortController();
|
|
273
358
|
private readonly event: unknown;
|
|
274
359
|
private readonly doc: unknown;
|
|
275
360
|
|
|
@@ -287,14 +372,16 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
287
372
|
let state = "queued";
|
|
288
373
|
for (const entry of entries) if (entry.type === "transition" && entry.machine === "workflow.run") state = entry.to;
|
|
289
374
|
this.runState = state;
|
|
290
|
-
this.event =
|
|
375
|
+
this.event = eventValueOf(first.trigger);
|
|
291
376
|
}
|
|
292
377
|
|
|
293
378
|
/** This function appends one entry. The appends of one run go in one order, and a conflict stops the run. */
|
|
294
379
|
private write(entry: RunEntry): Promise<void> {
|
|
295
380
|
const next = this.tail.then(async () => {
|
|
381
|
+
if (this.ended) throw new Error(`The record of the run ${this.id} has its end.`);
|
|
296
382
|
await options.store.append(this.id, this.entries.length, entry);
|
|
297
383
|
this.entries.push(entry);
|
|
384
|
+
if (entry.type === "end") this.ended = true;
|
|
298
385
|
});
|
|
299
386
|
this.tail = next.catch(() => undefined);
|
|
300
387
|
return next;
|
|
@@ -328,7 +415,44 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
328
415
|
return answer;
|
|
329
416
|
}
|
|
330
417
|
|
|
418
|
+
/** This function takes the lease of the run, and renews it after each third of its time until the end. */
|
|
419
|
+
// The fence of the store keeps the record safe: a second writer gets RecordConflict at its next append. The lease only
|
|
420
|
+
// keeps two runners from the same run, and the lost signal stops the calls in flight of a runner that lost it.
|
|
421
|
+
private async lease(): Promise<void> {
|
|
422
|
+
const take = async (): Promise<void> => {
|
|
423
|
+
const until = wall() + limits.lease;
|
|
424
|
+
await this.write({ type: "lease", holder, until, time: wall() });
|
|
425
|
+
this.leaseUntil = until;
|
|
426
|
+
};
|
|
427
|
+
await take();
|
|
428
|
+
const lose = (why: string): void => {
|
|
429
|
+
this.renewal = undefined;
|
|
430
|
+
this.lost.abort(new Error(why));
|
|
431
|
+
};
|
|
432
|
+
const renew = (): void => {
|
|
433
|
+
if (this.ended || this.lost.signal.aborted) return;
|
|
434
|
+
this.renewal = options.clock.after(Math.max(1, Math.floor(limits.lease / 3)), renew);
|
|
435
|
+
take().catch((error: unknown) => {
|
|
436
|
+
if (this.ended) return;
|
|
437
|
+
// A conflict tells that another runner wrote the record: it holds the run. Do not read the record again and retry.
|
|
438
|
+
if (error instanceof RecordConflict) lose(`Another runner took the run ${this.id}.`);
|
|
439
|
+
// A failure of the store keeps the run until its last lease ends.
|
|
440
|
+
else if (wall() >= this.leaseUntil) lose(`The lease of the run ${this.id} ended, because the store refused each renewal.`);
|
|
441
|
+
});
|
|
442
|
+
};
|
|
443
|
+
this.renewal = options.clock.after(Math.max(1, Math.floor(limits.lease / 3)), renew);
|
|
444
|
+
}
|
|
445
|
+
|
|
331
446
|
async go(): Promise<RunResult> {
|
|
447
|
+
await this.lease();
|
|
448
|
+
try {
|
|
449
|
+
return await this.body();
|
|
450
|
+
} finally {
|
|
451
|
+
this.renewal?.();
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private async body(): Promise<RunResult> {
|
|
332
456
|
if (this.runState !== "queued" && this.runState !== "running") {
|
|
333
457
|
// A crash after the last transition of the run and before its end: the end follows the transition.
|
|
334
458
|
const status = this.runState as RunStatus;
|
|
@@ -367,16 +491,20 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
367
491
|
/** The checks before the first effect: the document, the owner and the trigger. */
|
|
368
492
|
private async precheck(): Promise<RunResult | undefined> {
|
|
369
493
|
const problems = [
|
|
370
|
-
...checkWorkflow(this.doc, { machine, procedures: options.procedures, callNamespaces: options.callNamespaces ?? [] })
|
|
494
|
+
...checkWorkflow(this.doc, { machine, procedures: options.procedures, callNamespaces: options.callNamespaces ?? [], maxTries: limits.tries })
|
|
371
495
|
.filter((issue) => issue.level === "error")
|
|
372
496
|
.map((issue) => `${issue.code}: ${issue.message}`),
|
|
373
|
-
...runnerProblems(this.doc
|
|
497
|
+
...runnerProblems(this.doc),
|
|
374
498
|
];
|
|
375
499
|
if (problems.length > 0) return this.finish("reject", "invalid", problems.join(" "));
|
|
376
500
|
if (this.first.workspace !== this.first.owner) {
|
|
377
501
|
return this.finish("refuse", "refused", `The document names the workspace ${JSON.stringify(this.first.workspace)}, and the workspace ${this.first.owner} holds it.`);
|
|
378
502
|
}
|
|
379
503
|
const trigger = this.first.trigger;
|
|
504
|
+
const depth = trigger.depth ?? 0;
|
|
505
|
+
if (!Number.isInteger(depth) || depth < 0 || depth > limits.callDepth) {
|
|
506
|
+
return this.finish("refuse", "refused", `The depth of the call trigger is ${String(depth)}. It must be a whole number from 0 to ${String(limits.callDepth)}.`);
|
|
507
|
+
}
|
|
380
508
|
if (trigger.kind === "feed") {
|
|
381
509
|
const answer = await this.ask("trigger", "read", trigger.from);
|
|
382
510
|
if (!answer.ok) return this.finish("refuse", "refused", `The feed cannot read ${String(trigger.from)}: ${answer.why}`);
|
|
@@ -565,6 +693,9 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
565
693
|
/** One try of a step. */
|
|
566
694
|
private async attempt(instance: string, s: Rec, scope: Rec, attempt: number, deadline?: number): Promise<{ kind: "ok"; value: unknown } | { kind: "fail" | "expired"; why: string } | { kind: "halt"; status: "refused" | "in-doubt"; why: string }> {
|
|
567
695
|
const controller = new AbortController();
|
|
696
|
+
const stop = (): void => controller.abort(this.lost.signal.reason);
|
|
697
|
+
if (this.lost.signal.aborted) stop();
|
|
698
|
+
else this.lost.signal.addEventListener("abort", stop, { once: true });
|
|
568
699
|
const own = typeof s["timeout"] === "number" ? s["timeout"] * 1000 : Infinity;
|
|
569
700
|
const left = deadline === undefined ? Infinity : Math.max(0, deadline - options.clock.now());
|
|
570
701
|
const timeout = Math.min(own, left) === Infinity ? undefined : Math.min(own, left);
|
|
@@ -595,6 +726,7 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
595
726
|
return { kind: "ok", value: ran.result.value };
|
|
596
727
|
} finally {
|
|
597
728
|
cancel?.();
|
|
729
|
+
this.lost.signal.removeEventListener("abort", stop);
|
|
598
730
|
}
|
|
599
731
|
}
|
|
600
732
|
|
|
@@ -602,7 +734,7 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
602
734
|
|
|
603
735
|
private idempotent(at: string): boolean {
|
|
604
736
|
const name = this.procedureAt.get(at);
|
|
605
|
-
return name !== undefined && options.procedures[name]?.idempotent === true;
|
|
737
|
+
return name !== undefined && Object.hasOwn(options.procedures, name) && options.procedures[name]?.idempotent === true;
|
|
606
738
|
}
|
|
607
739
|
|
|
608
740
|
/** One effect: the replay of its record, or the grant check and the live call. */
|
|
@@ -623,7 +755,8 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
623
755
|
if (!namespaces.includes(namespace)) return refuse(`The effect ${effect.kind} is not a call.`);
|
|
624
756
|
const name = (namespace === "procedure" ? effect.path.slice(1) : effect.path).map(String).join(".");
|
|
625
757
|
this.procedureAt.set(at, name);
|
|
626
|
-
|
|
758
|
+
// An own key only: a path such as `constructor` names no procedure.
|
|
759
|
+
const info = Object.hasOwn(options.procedures, name) ? options.procedures[name] : undefined;
|
|
627
760
|
if (info === undefined) return refuse(`No procedure ${name} is known, thus no grant covers it.`);
|
|
628
761
|
if (!isRecord(effect.payload)) return refuse(`The inputs of ${name} are not a record.`);
|
|
629
762
|
const payload = effect.payload;
|
|
@@ -642,7 +775,7 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
642
775
|
open.add(at);
|
|
643
776
|
let outcome: Outcome;
|
|
644
777
|
try {
|
|
645
|
-
const value = options.redact(await options.call({ procedure: name.split("."), inputs: payload, key, try: attempt, workspace: this.first.owner, signal }));
|
|
778
|
+
const value = options.redact(await options.call({ procedure: name.split("."), inputs: payload, key, try: attempt, workspace: this.first.owner, depth: this.first.trigger.depth ?? 0, signal }));
|
|
646
779
|
const bytes = JSON.stringify(value ?? null).length;
|
|
647
780
|
outcome = bytes > limits.outcomeBytes ? { ok: false, code: "#SIZE!", why: `The outcome has ${String(bytes)} bytes. The limit is ${String(limits.outcomeBytes)}.` } : { ok: true, value: value ?? null };
|
|
648
781
|
} catch (error) {
|
|
@@ -658,5 +791,5 @@ export function workflowRunner(options: RunnerOptions): WorkflowRunner {
|
|
|
658
791
|
}
|
|
659
792
|
}
|
|
660
793
|
|
|
661
|
-
return { runIdOf, start, resume };
|
|
794
|
+
return { runIdOf, start, resume, recover };
|
|
662
795
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,8 @@ export {
|
|
|
2
2
|
type AccessAnswer,
|
|
3
3
|
type AccessRequest,
|
|
4
4
|
type ProcedureCall,
|
|
5
|
+
type RecoverOptions,
|
|
6
|
+
type RecoverReport,
|
|
5
7
|
type RunResult,
|
|
6
8
|
type RunnerLimits,
|
|
7
9
|
type RunnerOptions,
|
|
@@ -11,4 +13,4 @@ export {
|
|
|
11
13
|
workflowRunner,
|
|
12
14
|
} from "./engine.ts";
|
|
13
15
|
export { RUN_MACHINE, STEP_MACHINE } from "./machines.ts";
|
|
14
|
-
export { type Outcome, type RunEntry, type RunStatus, type RunStore, type StepStatus, type TriggerEvent, RecordConflict, memoryStore } from "./record.ts";
|
|
16
|
+
export { type Outcome, type RunEntry, type RunStatus, type RunStore, type StepStatus, type TriggerEvent, RecordConflict, RunHeld, memoryStore } from "./record.ts";
|
package/src/record.ts
CHANGED
|
@@ -10,12 +10,23 @@ export interface TriggerEvent {
|
|
|
10
10
|
readonly kind: string;
|
|
11
11
|
/** The id of the event, for example the id of a delivery or the time of a schedule boundary. */
|
|
12
12
|
readonly id: string;
|
|
13
|
-
/** The data of the event. A reference reads it as `event`. */
|
|
13
|
+
/** The data of the event. A reference reads it as `event.data`. */
|
|
14
14
|
readonly data?: unknown;
|
|
15
|
+
/** For an event: the name of the event, for example `push`. A reference reads it as `event.event`. */
|
|
16
|
+
readonly event?: string;
|
|
17
|
+
/** For an event: its type, for example `opened`. A reference reads it as `event.type`. */
|
|
18
|
+
readonly type?: string;
|
|
15
19
|
/** For a feed: the workspace that the feed reads. */
|
|
16
20
|
readonly from?: string;
|
|
21
|
+
/** For a feed: the collection that the feed reads. */
|
|
22
|
+
readonly collection?: string;
|
|
17
23
|
/** For a call: the workspace of the caller. */
|
|
18
24
|
readonly caller?: string;
|
|
25
|
+
/**
|
|
26
|
+
* For a call: the count of the runs above this run in the chain of calls. A start by a person, a schedule or a source has
|
|
27
|
+
* the depth 0. The host gives the depth of the calling run plus 1.
|
|
28
|
+
*/
|
|
29
|
+
readonly depth?: number;
|
|
19
30
|
}
|
|
20
31
|
|
|
21
32
|
/** The outcome of one effect, after the redaction of the host. */
|
|
@@ -41,7 +52,8 @@ export type RunEntry =
|
|
|
41
52
|
| { readonly type: "effect-end"; readonly at: string; readonly outcome: Outcome; readonly time: number }
|
|
42
53
|
| { readonly type: "step-end"; readonly instance: string; readonly status: StepStatus; readonly value?: unknown; readonly time: number }
|
|
43
54
|
| { readonly type: "job-end"; readonly instance: string; readonly status: StepStatus; readonly outputs?: unknown; readonly time: number }
|
|
44
|
-
| { readonly type: "end"; readonly status: RunStatus; readonly why: string; readonly time: number }
|
|
55
|
+
| { readonly type: "end"; readonly status: RunStatus; readonly why: string; readonly time: number }
|
|
56
|
+
| { readonly type: "lease"; readonly holder: string; readonly until: number; readonly time: number };
|
|
45
57
|
|
|
46
58
|
/** The answer of a store to an append that another writer took first. */
|
|
47
59
|
export class RecordConflict extends Error {
|
|
@@ -71,6 +83,30 @@ export interface RunStore {
|
|
|
71
83
|
readonly append: (run: string, seq: number, entry: RunEntry) => Promise<void> | void;
|
|
72
84
|
/** This function gives the entries of a run in their order. A run with no record gives an empty list. */
|
|
73
85
|
readonly read: (run: string) => Promise<readonly RunEntry[]> | readonly RunEntry[];
|
|
86
|
+
/** This function gives the ids of the runs with no `end` entry. The recovery of a runner reads it. */
|
|
87
|
+
readonly open?: () => Promise<readonly string[]> | readonly string[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The answer of a resume of a run that another runner holds: its lease is live. */
|
|
91
|
+
export class RunHeld extends Error {
|
|
92
|
+
/** The holder of the lease. */
|
|
93
|
+
readonly holder: string;
|
|
94
|
+
/** The wall time at the end of the lease, in milliseconds. */
|
|
95
|
+
readonly until: number;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* This constructor makes the error.
|
|
99
|
+
*
|
|
100
|
+
* @param run - The run id.
|
|
101
|
+
* @param holder - The holder of the lease.
|
|
102
|
+
* @param until - The wall time at the end of the lease.
|
|
103
|
+
*/
|
|
104
|
+
constructor(run: string, holder: string, until: number) {
|
|
105
|
+
super(`The runner ${holder} holds the run ${run} until ${new Date(until).toISOString()}.`);
|
|
106
|
+
this.name = "RunHeld";
|
|
107
|
+
this.holder = holder;
|
|
108
|
+
this.until = until;
|
|
109
|
+
}
|
|
74
110
|
}
|
|
75
111
|
|
|
76
112
|
/** A store in memory, for tests and for a host with no durable store. Each entry is a copy as JSON. */
|
|
@@ -84,6 +120,7 @@ export function memoryStore(): RunStore & { readonly runs: () => readonly string
|
|
|
84
120
|
records.set(run, list);
|
|
85
121
|
},
|
|
86
122
|
read: (run) => [...(records.get(run) ?? [])],
|
|
123
|
+
open: () => [...records].filter(([, list]) => !list.some((entry) => entry.type === "end")).map(([run]) => run),
|
|
87
124
|
runs: () => [...records.keys()],
|
|
88
125
|
};
|
|
89
126
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"engine-B_0OOo1N.d.ts","names":[],"sources":["../src/record.ts","../src/engine.ts"],"mappings":";;;;KACY;;KAGA;;UAGK;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;;KAIC;WAAqB;WAAmB;;WAA8B;WAAoB;WAAuB;;;KAGjH;WAEG;WACA;WACA;WACA;WACA;WACA;WACA,SAAS;WACT;WACA;;WAEA;WAAwB;WAAqB;WAAuB;WAAmC;WAAyB;WAAsB;WAAsB;;WAC5K;WAA6B;WAAoD;WAA2B;WAAuB;WAAqB;WAAqB;WAAuB;;WACpM;WAAuB;WAAsB;WAAmC;;WAChF;WAA+B;WAAqB;WAAsB;WAAsB;WAA4B;WAA2B;;WACvJ;WAA6B;WAAqB,SAAS;WAAkB;;WAC7E;WAA2B;WAA2B,QAAQ;WAAqB;WAA0B;;WAC7G;WAA0B;WAA2B,QAAQ;WAAqB;WAA4B;;WAC9G;WAAsB,QAAQ;WAAoB;WAAsB;;;cAG1E,uBAAuB;;WAEzB;;;;;;;;EAST,YAAY,aAAa,aAAa;;;;;;UAWvB;;WAEN,SAAS,aAAa,aAAa,OAAO,aAAa;;WAEvD,OAAO,gBAAgB,iBAAiB,uBAAuB;;;iBAI1D,eAAe;WAAsB;;;UCjEpC,wBAAwB;;;;;WAK9B;;;UAIM;;WAEN;;WAEA,QAAQ,SAAS;;WAEjB;;WAEA;;WAEA;;WAEA,QAAQ;;;UAIF;WACN;WACA;WACA;;;UAIM;WACN;WACA;;;UAIM;;WAEN;;WAEA;;WAEA;;WAEA;;;UAIM;;WAEN,YAAY,SAAS,eAAe;;WAEpC,OAAO,MAAM,kBAAkB;;WAE/B,SAAS,SAAS,kBAAkB,eAAe,QAAQ;;;;;WAK3D,SAAS;;WAET,OAAO;;WAEP,OAAO;;WAEP;;WAEA,UAAU;;WAEV;;WAEA,SAAS;;;UAIH;;WAEN;;WAEA;;WAEA,SAAS;;WAET,SAAS,SAAS;;;UAIZ;WACN;WACA,QAAQ;WACR;WACA,kBAAkB;;;UAIZ;;WAEN,UAAU,SAAS;;WAEnB,QAAQ,SAAS,iBAAiB,QAAQ;;WAE1C,SAAS,gBAAgB,QAAQ;;;;;;;;;iBAkH5B,eAAe,SAAS,gBAAgB"}
|