@mark1russell7/workflow-runner 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +37 -2
- package/build/engine-bAZZ3ZT9.d.ts +303 -0
- package/build/engine-bAZZ3ZT9.d.ts.map +1 -0
- package/build/index.d.ts +14 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1125 -0
- package/build/index.js.map +1 -0
- package/build/service.d.ts +31 -0
- package/build/service.d.ts.map +1 -0
- package/build/service.js +56 -0
- package/build/service.js.map +1 -0
- package/package.json +53 -3
- package/src/engine.ts +795 -0
- package/src/index.ts +16 -0
- package/src/machines.ts +98 -0
- package/src/record.ts +126 -0
- package/src/service.ts +54 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type AccessAnswer,
|
|
3
|
+
type AccessRequest,
|
|
4
|
+
type ProcedureCall,
|
|
5
|
+
type RecoverOptions,
|
|
6
|
+
type RecoverReport,
|
|
7
|
+
type RunResult,
|
|
8
|
+
type RunnerLimits,
|
|
9
|
+
type RunnerOptions,
|
|
10
|
+
type RunnerProcedure,
|
|
11
|
+
type StartRequest,
|
|
12
|
+
type WorkflowRunner,
|
|
13
|
+
workflowRunner,
|
|
14
|
+
} from "./engine.ts";
|
|
15
|
+
export { RUN_MACHINE, STEP_MACHINE } from "./machines.ts";
|
|
16
|
+
export { type Outcome, type RunEntry, type RunStatus, type RunStore, type StepStatus, type TriggerEvent, RecordConflict, RunHeld, memoryStore } from "./record.ts";
|
package/src/machines.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Machine } from "@mark1russell7/algebra";
|
|
2
|
+
|
|
3
|
+
const SOURCES: Machine["sources"] = [{ ref: "docs/WORKFLOW.md#the-runner-w4", rung: "implemented", note: "D225" }];
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The state machine of a run. A run is queued, then it runs. It ends with one status. The runner records each
|
|
7
|
+
* transition, and a test checks the record against this machine.
|
|
8
|
+
*/
|
|
9
|
+
export const RUN_MACHINE: Machine = {
|
|
10
|
+
format: "mark1russell7.machine/1",
|
|
11
|
+
id: "workflow.run",
|
|
12
|
+
title: "The run of a workflow",
|
|
13
|
+
layer: "self",
|
|
14
|
+
version: "1.0.0",
|
|
15
|
+
sources: SOURCES,
|
|
16
|
+
states: {
|
|
17
|
+
queued: { initial: true },
|
|
18
|
+
running: {},
|
|
19
|
+
succeeded: { final: true },
|
|
20
|
+
failed: { final: true },
|
|
21
|
+
refused: { final: true, note: "A grant, an owner or a check refused the run. No later effect happened." },
|
|
22
|
+
invalid: { final: true, note: "The document has an error. No effect happened." },
|
|
23
|
+
"in-doubt": { final: true, note: "An effect started and has no outcome, and its procedure does not remove a second call." },
|
|
24
|
+
cancelled: { final: true },
|
|
25
|
+
},
|
|
26
|
+
events: {
|
|
27
|
+
start: { kind: "app" },
|
|
28
|
+
succeed: { kind: "local" },
|
|
29
|
+
fail: { kind: "local" },
|
|
30
|
+
refuse: { kind: "local" },
|
|
31
|
+
reject: { kind: "local" },
|
|
32
|
+
doubt: { kind: "local" },
|
|
33
|
+
cancel: { kind: "app" },
|
|
34
|
+
},
|
|
35
|
+
knobs: {},
|
|
36
|
+
timers: {},
|
|
37
|
+
transitions: [
|
|
38
|
+
{ id: "start", from: "queued", to: "running", on: "start" },
|
|
39
|
+
{ id: "reject", from: "queued", to: "invalid", on: "reject" },
|
|
40
|
+
{ id: "refuse-early", from: "queued", to: "refused", on: "refuse" },
|
|
41
|
+
{ id: "succeed", from: "running", to: "succeeded", on: "succeed" },
|
|
42
|
+
{ id: "fail", from: "running", to: "failed", on: "fail" },
|
|
43
|
+
{ id: "refuse", from: "running", to: "refused", on: "refuse" },
|
|
44
|
+
{ id: "doubt", from: "running", to: "in-doubt", on: "doubt" },
|
|
45
|
+
{ id: "cancel", from: ["queued", "running"], to: "cancelled", on: "cancel" },
|
|
46
|
+
],
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The state machine of one step of one job instance. A failed try waits for its retry. A refusal and a doubt end the
|
|
51
|
+
* step at once: no retry and no `continue` changes them.
|
|
52
|
+
*/
|
|
53
|
+
export const STEP_MACHINE: Machine = {
|
|
54
|
+
format: "mark1russell7.machine/1",
|
|
55
|
+
id: "workflow.step",
|
|
56
|
+
title: "The step of a job",
|
|
57
|
+
layer: "self",
|
|
58
|
+
version: "1.0.0",
|
|
59
|
+
sources: SOURCES,
|
|
60
|
+
states: {
|
|
61
|
+
pending: { initial: true },
|
|
62
|
+
running: {},
|
|
63
|
+
waiting: { note: "A try failed. The next try starts at the due time of the record." },
|
|
64
|
+
succeeded: { final: true },
|
|
65
|
+
skipped: { final: true },
|
|
66
|
+
failed: { final: true },
|
|
67
|
+
"timed-out": { final: true },
|
|
68
|
+
refused: { final: true },
|
|
69
|
+
"in-doubt": { final: true },
|
|
70
|
+
cancelled: { final: true },
|
|
71
|
+
},
|
|
72
|
+
events: {
|
|
73
|
+
run: { kind: "local" },
|
|
74
|
+
skip: { kind: "local" },
|
|
75
|
+
succeed: { kind: "local" },
|
|
76
|
+
fail: { kind: "local" },
|
|
77
|
+
retry: { kind: "local" },
|
|
78
|
+
due: { kind: "timer" },
|
|
79
|
+
expire: { kind: "timer" },
|
|
80
|
+
refuse: { kind: "local" },
|
|
81
|
+
doubt: { kind: "local" },
|
|
82
|
+
cancel: { kind: "app" },
|
|
83
|
+
},
|
|
84
|
+
knobs: {},
|
|
85
|
+
timers: {},
|
|
86
|
+
transitions: [
|
|
87
|
+
{ id: "run", from: "pending", to: "running", on: "run" },
|
|
88
|
+
{ id: "skip", from: "pending", to: "skipped", on: "skip" },
|
|
89
|
+
{ id: "succeed", from: "running", to: "succeeded", on: "succeed" },
|
|
90
|
+
{ id: "fail", from: "running", to: "failed", on: "fail" },
|
|
91
|
+
{ id: "retry", from: "running", to: "waiting", on: "retry" },
|
|
92
|
+
{ id: "due", from: "waiting", to: "running", on: "due" },
|
|
93
|
+
{ id: "expire", from: "running", to: "timed-out", on: "expire" },
|
|
94
|
+
{ id: "refuse", from: ["pending", "running"], to: "refused", on: "refuse" },
|
|
95
|
+
{ id: "doubt", from: ["pending", "running"], to: "in-doubt", on: "doubt" },
|
|
96
|
+
{ id: "cancel", from: ["pending", "running", "waiting"], to: "cancelled", on: "cancel" },
|
|
97
|
+
],
|
|
98
|
+
};
|
package/src/record.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** The final status of a run. */
|
|
2
|
+
export type RunStatus = "succeeded" | "failed" | "refused" | "invalid" | "in-doubt" | "cancelled";
|
|
3
|
+
|
|
4
|
+
/** The final status of a step. */
|
|
5
|
+
export type StepStatus = "succeeded" | "skipped" | "failed" | "timed-out" | "refused" | "in-doubt" | "cancelled";
|
|
6
|
+
|
|
7
|
+
/** The event that started a run. `id` makes the run id: the same event gives the same run. */
|
|
8
|
+
export interface TriggerEvent {
|
|
9
|
+
/** The kind of the trigger: `manual`, `schedule`, `event`, `feed` or `call`. */
|
|
10
|
+
readonly kind: string;
|
|
11
|
+
/** The id of the event, for example the id of a delivery or the time of a schedule boundary. */
|
|
12
|
+
readonly id: string;
|
|
13
|
+
/** The data of the event. A reference reads it as `event.data`. */
|
|
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;
|
|
19
|
+
/** For a feed: the workspace that the feed reads. */
|
|
20
|
+
readonly from?: string;
|
|
21
|
+
/** For a feed: the collection that the feed reads. */
|
|
22
|
+
readonly collection?: string;
|
|
23
|
+
/** For a call: the workspace of the caller. */
|
|
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;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The outcome of one effect, after the redaction of the host. */
|
|
33
|
+
export type Outcome = { readonly ok: true; readonly value: unknown } | { readonly ok: false; readonly code: string; readonly why: string };
|
|
34
|
+
|
|
35
|
+
/** One entry of the record of a run. The record is append-only, and it is also the log of a resume. */
|
|
36
|
+
export type RunEntry =
|
|
37
|
+
| {
|
|
38
|
+
readonly type: "start";
|
|
39
|
+
readonly run: string;
|
|
40
|
+
readonly docHash: string;
|
|
41
|
+
readonly doc: unknown;
|
|
42
|
+
readonly owner: string;
|
|
43
|
+
readonly workspace: unknown;
|
|
44
|
+
readonly trigger: TriggerEvent;
|
|
45
|
+
readonly inputs: unknown;
|
|
46
|
+
readonly time: number;
|
|
47
|
+
}
|
|
48
|
+
| { readonly type: "grant"; readonly at: string; readonly from: string; readonly access: "read" | "write"; readonly target: string; readonly ok: boolean; readonly why: string; readonly time: number }
|
|
49
|
+
| { readonly type: "transition"; readonly machine: "workflow.run" | "workflow.step"; readonly instance: string; readonly from: string; readonly on: string; readonly to: string; readonly time: number; readonly due?: number }
|
|
50
|
+
| { readonly type: "rows"; readonly job: string; readonly rows: readonly unknown[]; readonly time: number }
|
|
51
|
+
| { readonly type: "effect-start"; readonly at: string; readonly key: string; readonly try: number; readonly procedure: string; readonly payload: unknown; readonly time: number }
|
|
52
|
+
| { readonly type: "effect-end"; readonly at: string; readonly outcome: Outcome; readonly time: number }
|
|
53
|
+
| { readonly type: "step-end"; readonly instance: string; readonly status: StepStatus; readonly value?: unknown; readonly time: number }
|
|
54
|
+
| { readonly type: "job-end"; readonly instance: string; readonly status: StepStatus; readonly outputs?: unknown; 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 };
|
|
57
|
+
|
|
58
|
+
/** The answer of a store to an append that another writer took first. */
|
|
59
|
+
export class RecordConflict extends Error {
|
|
60
|
+
/** The count of the entries that the store holds. */
|
|
61
|
+
readonly length: number;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* This constructor makes the error.
|
|
65
|
+
*
|
|
66
|
+
* @param run - The run id.
|
|
67
|
+
* @param seq - The place that the writer asked for.
|
|
68
|
+
* @param length - The count of the entries that the store holds.
|
|
69
|
+
*/
|
|
70
|
+
constructor(run: string, seq: number, length: number) {
|
|
71
|
+
super(`The record of the run ${run} has ${String(length)} entries, not ${String(seq)}. Another runner writes it.`);
|
|
72
|
+
this.name = "RecordConflict";
|
|
73
|
+
this.length = length;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The store of the records of the runs. An append gives the place of the entry. When the record has another length,
|
|
79
|
+
* the store refuses with `RecordConflict`. Thus two runners cannot write one run (a fenced append).
|
|
80
|
+
*/
|
|
81
|
+
export interface RunStore {
|
|
82
|
+
/** This function appends one entry at the place `seq`, or it refuses with `RecordConflict`. */
|
|
83
|
+
readonly append: (run: string, seq: number, entry: RunEntry) => Promise<void> | void;
|
|
84
|
+
/** This function gives the entries of a run in their order. A run with no record gives an empty list. */
|
|
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
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A store in memory, for tests and for a host with no durable store. Each entry is a copy as JSON. */
|
|
113
|
+
export function memoryStore(): RunStore & { readonly runs: () => readonly string[] } {
|
|
114
|
+
const records = new Map<string, RunEntry[]>();
|
|
115
|
+
return {
|
|
116
|
+
append(run, seq, entry) {
|
|
117
|
+
const list = records.get(run) ?? [];
|
|
118
|
+
if (seq !== list.length) throw new RecordConflict(run, seq, list.length);
|
|
119
|
+
list.push(JSON.parse(JSON.stringify(entry)) as RunEntry);
|
|
120
|
+
records.set(run, list);
|
|
121
|
+
},
|
|
122
|
+
read: (run) => [...(records.get(run) ?? [])],
|
|
123
|
+
open: () => [...records].filter(([, list]) => !list.some((entry) => entry.type === "end")).map(([run]) => run),
|
|
124
|
+
runs: () => [...records.keys()],
|
|
125
|
+
};
|
|
126
|
+
}
|
package/src/service.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Channel } from "@mark1russell7/service/channel";
|
|
2
|
+
import type { RunResult, StartRequest, WorkflowRunner } from "./engine.ts";
|
|
3
|
+
|
|
4
|
+
/** The answer of a queue to an offer. */
|
|
5
|
+
export type OfferAnswer = { readonly accepted: true; readonly run: string } | { readonly accepted: false; readonly run: string; readonly why: "duplicate" | "full" | "closed" };
|
|
6
|
+
|
|
7
|
+
/** A queue of runs. The host gives `next` to the `iteration` of a worker loop of `@mark1russell7/service`. */
|
|
8
|
+
export interface RunQueue {
|
|
9
|
+
/** This function offers a request. A full queue refuses it at once: the caller keeps the event and offers it again later. */
|
|
10
|
+
readonly offer: (request: StartRequest) => OfferAnswer;
|
|
11
|
+
/** This function waits for one request, and runs it. It rejects when the queue is closed and empty. */
|
|
12
|
+
readonly next: () => Promise<RunResult>;
|
|
13
|
+
/** This function closes the queue. The requests in it stay, and `next` takes them. */
|
|
14
|
+
readonly close: () => void;
|
|
15
|
+
/** The count of the requests that wait. */
|
|
16
|
+
readonly size: () => number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This function makes a queue of runs with a bound. The same event gives the same run id, thus a second offer of a run
|
|
21
|
+
* that waits or runs is a duplicate. The store removes a duplicate after the end: `start` gives the recorded end.
|
|
22
|
+
*
|
|
23
|
+
* @param runner - The runner.
|
|
24
|
+
* @param capacity - The most requests that wait.
|
|
25
|
+
* @returns The queue.
|
|
26
|
+
*/
|
|
27
|
+
export function runQueue(runner: WorkflowRunner, capacity: number): RunQueue {
|
|
28
|
+
if (!Number.isInteger(capacity) || capacity < 1) throw new RangeError("The capacity of a run queue must be a whole number of 1 or more.");
|
|
29
|
+
const channel = new Channel<StartRequest>(capacity);
|
|
30
|
+
const live = new Set<string>();
|
|
31
|
+
return {
|
|
32
|
+
offer(request) {
|
|
33
|
+
const run = runner.runIdOf(request);
|
|
34
|
+
if (channel.isClosed) return { accepted: false, run, why: "closed" };
|
|
35
|
+
if (live.has(run)) return { accepted: false, run, why: "duplicate" };
|
|
36
|
+
if (!channel.trySend(request)) return { accepted: false, run, why: "full" };
|
|
37
|
+
live.add(run);
|
|
38
|
+
return { accepted: true, run };
|
|
39
|
+
},
|
|
40
|
+
async next() {
|
|
41
|
+
const request = await channel.receive();
|
|
42
|
+
const run = runner.runIdOf(request);
|
|
43
|
+
try {
|
|
44
|
+
return await runner.start(request);
|
|
45
|
+
} finally {
|
|
46
|
+
live.delete(run);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
close: () => {
|
|
50
|
+
channel.close();
|
|
51
|
+
},
|
|
52
|
+
size: () => channel.size,
|
|
53
|
+
};
|
|
54
|
+
}
|