@artieeez/cf-worker-runtime 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/README.md +12 -2
- package/dist/agents.d.ts +64 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +324 -0
- package/dist/agents.js.map +1 -0
- package/dist/exec.d.ts +36 -0
- package/dist/exec.d.ts.map +1 -0
- package/dist/exec.js +186 -0
- package/dist/exec.js.map +1 -0
- package/dist/fatal.d.ts +5 -0
- package/dist/fatal.d.ts.map +1 -0
- package/dist/fatal.js +5 -0
- package/dist/fatal.js.map +1 -0
- package/dist/health.d.ts +49 -0
- package/dist/health.d.ts.map +1 -0
- package/dist/health.js +97 -0
- package/dist/health.js.map +1 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1058 -0
- package/dist/index.js.map +1 -0
- package/dist/provenance.d.ts +11 -0
- package/dist/provenance.d.ts.map +1 -0
- package/dist/provenance.js +74 -0
- package/dist/provenance.js.map +1 -0
- package/dist/run.d.ts +17 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +37 -0
- package/dist/run.js.map +1 -0
- package/dist/sessions.d.ts +14 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +98 -0
- package/dist/sessions.js.map +1 -0
- package/dist/steps.d.ts +25 -0
- package/dist/steps.d.ts.map +1 -0
- package/dist/steps.js +62 -0
- package/dist/steps.js.map +1 -0
- package/dist/workspaces.d.ts +37 -0
- package/dist/workspaces.d.ts.map +1 -0
- package/dist/workspaces.js +68 -0
- package/dist/workspaces.js.map +1 -0
- package/package.json +78 -3
- package/src/agents.ts +387 -0
- package/src/exec.ts +184 -0
- package/src/fatal.ts +4 -0
- package/src/health.ts +119 -0
- package/src/index.ts +1142 -0
- package/src/provenance.ts +74 -0
- package/src/run.ts +40 -0
- package/src/sessions.ts +98 -0
- package/src/steps.ts +85 -0
- package/src/workspaces.ts +81 -0
package/src/health.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { createServer, type Server } from 'node:http';
|
|
2
|
+
import type { CheckResult } from '@artieeez/cf-sdk';
|
|
3
|
+
import { WorkerFatalError } from './fatal.ts';
|
|
4
|
+
|
|
5
|
+
/** One workflow version's readiness on this worker, as `POST /w/health` takes it (health-ready door 1). */
|
|
6
|
+
export interface WorkflowReadiness {
|
|
7
|
+
workflow: string;
|
|
8
|
+
version: string;
|
|
9
|
+
ready: boolean;
|
|
10
|
+
problems: string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A round's answer for one workflow: its readiness and whether its check was fatal. */
|
|
14
|
+
export interface CheckOutcome extends WorkflowReadiness {
|
|
15
|
+
fatal: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Runs `check` within `timeoutMs` (AC 14): past it, the signal it was given is aborted and the
|
|
20
|
+
* answer is a timeout. A throw is `{ ok: false, reason }`; a check with nothing to say is ok.
|
|
21
|
+
*/
|
|
22
|
+
export async function bounded(
|
|
23
|
+
check: (signal: AbortSignal) => Promise<CheckResult | void> | CheckResult | void,
|
|
24
|
+
timeoutMs: number,
|
|
25
|
+
outer: AbortSignal,
|
|
26
|
+
): Promise<CheckResult> {
|
|
27
|
+
const abort = new AbortController();
|
|
28
|
+
const signal = AbortSignal.any([abort.signal, outer]);
|
|
29
|
+
let timer: NodeJS.Timeout | undefined;
|
|
30
|
+
try {
|
|
31
|
+
const answer = await Promise.race([
|
|
32
|
+
(async () => (await check(signal)) ?? { ok: true as const })(),
|
|
33
|
+
new Promise<CheckResult>((resolve) => {
|
|
34
|
+
timer = setTimeout(() => {
|
|
35
|
+
abort.abort();
|
|
36
|
+
resolve({ ok: false, reason: `timeout after ${timeoutMs} ms` });
|
|
37
|
+
}, timeoutMs);
|
|
38
|
+
}),
|
|
39
|
+
]);
|
|
40
|
+
return answer;
|
|
41
|
+
} catch (err) {
|
|
42
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) };
|
|
43
|
+
} finally {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Whether two rounds would report the same thing (AC 18): readiness and problems alike. */
|
|
49
|
+
export function sameReadiness(a: WorkflowReadiness[] | undefined, b: WorkflowReadiness[]): boolean {
|
|
50
|
+
if (a === undefined || a.length !== b.length) return false;
|
|
51
|
+
return a.every((x, i) => {
|
|
52
|
+
const y = b[i]!;
|
|
53
|
+
return x.workflow === y.workflow && x.version === y.version && x.ready === y.ready && x.problems.join('\n') === y.problems.join('\n');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** What the probes read: progress of the server calls, and the last round the server holds. */
|
|
58
|
+
export class Vitals {
|
|
59
|
+
/** when a server call or a retry wait last settled; null before the lease loop starts */
|
|
60
|
+
lastProgress: number | null = null;
|
|
61
|
+
looping = false;
|
|
62
|
+
/** the last round the server accepted; undefined before the first report */
|
|
63
|
+
reported: WorkflowReadiness[] | undefined;
|
|
64
|
+
|
|
65
|
+
private readonly staleMs: number;
|
|
66
|
+
private readonly now: () => number;
|
|
67
|
+
|
|
68
|
+
constructor(staleMs: number, now: () => number = Date.now) {
|
|
69
|
+
this.staleMs = staleMs;
|
|
70
|
+
this.now = now;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
progress(): void {
|
|
74
|
+
this.lastProgress = this.now();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
startLoop(): void {
|
|
78
|
+
this.looping = true;
|
|
79
|
+
this.progress();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** `GET /health` (AC 8-10). */
|
|
83
|
+
health(): { status: number; body: Record<string, unknown> } {
|
|
84
|
+
if (!this.looping || this.lastProgress === null) return { status: 200, body: { status: 'starting' } };
|
|
85
|
+
if (this.now() - this.lastProgress > this.staleMs) {
|
|
86
|
+
return { status: 503, body: { status: 'stuck', lastProgressAt: new Date(this.lastProgress).toISOString() } };
|
|
87
|
+
}
|
|
88
|
+
return { status: 200, body: { status: 'ok' } };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** `GET /ready` (AC 11). */
|
|
92
|
+
ready(): { status: number; body: Record<string, unknown> } {
|
|
93
|
+
const workflows = this.reported ?? [];
|
|
94
|
+
if (this.reported === undefined) return { status: 503, body: { status: 'starting', workflows } };
|
|
95
|
+
const any = workflows.some((w) => w.ready);
|
|
96
|
+
return { status: any ? 200 : 503, body: { status: any ? 'ready' : 'unready', workflows } };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The probe listener (AC 6, 7): `node:http`, `GET /health` and `GET /ready`, 404 otherwise. A port
|
|
102
|
+
* already taken stops the worker.
|
|
103
|
+
*/
|
|
104
|
+
export async function listenProbes(vitals: Vitals, port: number, host: string): Promise<Server> {
|
|
105
|
+
const server = createServer((req, res) => {
|
|
106
|
+
const path = new URL(req.url ?? '/', 'http://probe').pathname;
|
|
107
|
+
const answer = req.method !== 'GET' ? undefined : path === '/health' ? vitals.health() : path === '/ready' ? vitals.ready() : undefined;
|
|
108
|
+
const { status, body } = answer ?? { status: 404, body: { status: 'not_found' } };
|
|
109
|
+
res.writeHead(status, { 'content-type': 'application/json' });
|
|
110
|
+
res.end(JSON.stringify(body));
|
|
111
|
+
});
|
|
112
|
+
await new Promise<void>((resolve, reject) => {
|
|
113
|
+
server.once('error', (err: NodeJS.ErrnoException) => {
|
|
114
|
+
reject(err.code === 'EADDRINUSE' ? new WorkerFatalError(`health port ${port} is in use`) : err);
|
|
115
|
+
});
|
|
116
|
+
server.listen(port, host, () => resolve());
|
|
117
|
+
});
|
|
118
|
+
return server;
|
|
119
|
+
}
|