@genroc/eval-node 0.0.0-edge.aff7b37 → 0.0.0-edge.b6172ef
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 +67 -33
- package/dist/eval.js +85 -0
- package/dist/import.js +397 -0
- package/dist/realm.js +113 -0
- package/dist/worker.js +305 -0
- package/eval.ts +31 -5
- package/import.ts +190 -80
- package/package.json +6 -4
- package/realm.ts +61 -77
- package/worker.ts +44 -12
- package/bin/import.mjs +0 -4
- package/bin/worker.mjs +0 -2
package/worker.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
// The queue worker: claims parked `external` script tasks from genroc, evaluates each in its
|
|
2
3
|
// own realm, and answers. This is the whole genroc-facing half — eval.ts and realm.ts know
|
|
3
4
|
// nothing about the queue, which is what keeps the containment strategy swappable.
|
|
4
5
|
//
|
|
5
6
|
// See README.md for the contract, and specs/external-task-queue.md for the queue itself.
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { Cancelled, evaluate, type EvalRequest, type FailureKind } from "./eval.ts";
|
|
8
10
|
|
|
9
11
|
const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
|
|
10
12
|
const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
@@ -16,7 +18,12 @@ const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
|
16
18
|
// Sent as a header rather than in the URL because Node's fetch REFUSES a URL carrying
|
|
17
19
|
// credentials ("Request cannot be constructed from a URL that includes credentials"), so the
|
|
18
20
|
// basic-auth-in-the-URL trick that works for genctl is not available here.
|
|
19
|
-
|
|
21
|
+
// GENROC_TOKEN_FILE is the mounted-secret shape: a credential in a file rather than an
|
|
22
|
+
// environment variable, so it stays out of `docker inspect` and out of the process environment
|
|
23
|
+
// any child inherits. The inline variable wins when both are set.
|
|
24
|
+
const TOKEN =
|
|
25
|
+
process.env.GENROC_TOKEN ??
|
|
26
|
+
(process.env.GENROC_TOKEN_FILE ? readFileSync(process.env.GENROC_TOKEN_FILE, "utf8").trim() : "");
|
|
20
27
|
const authHeaders: Record<string, string> = TOKEN ? { authorization: `Bearer ${TOKEN}` } : {};
|
|
21
28
|
// Concurrency is the worker's to set, and that is the point of pulling: under the old fetch
|
|
22
29
|
// shape genroc decided how many scripts ran at once (--max-concurrent, default 200) and the
|
|
@@ -128,7 +135,12 @@ function asEvalRequest(input: unknown): EvalRequest | string {
|
|
|
128
135
|
};
|
|
129
136
|
}
|
|
130
137
|
|
|
131
|
-
|
|
138
|
+
/** A claim this worker is currently serving. The controller is how the renewal loop reaches
|
|
139
|
+
* the evaluation: genroc cannot call us, so a cancellation arrives as an answer to our own
|
|
140
|
+
* heartbeat and has to be delivered inward from there. */
|
|
141
|
+
type Running = { job: QueueTask; abort: AbortController };
|
|
142
|
+
|
|
143
|
+
const inFlight = new Map<string, Running>();
|
|
132
144
|
let running = true;
|
|
133
145
|
|
|
134
146
|
// Whether the last claim reached genroc. A worker polls several times a second, so an
|
|
@@ -196,7 +208,7 @@ async function answer(token: string, outcome: Record<string, unknown>): Promise<
|
|
|
196
208
|
await release(token);
|
|
197
209
|
}
|
|
198
210
|
|
|
199
|
-
async function run(job: QueueTask): Promise<void> {
|
|
211
|
+
async function run(job: QueueTask, signal: AbortSignal): Promise<void> {
|
|
200
212
|
let resolved: unknown;
|
|
201
213
|
try {
|
|
202
214
|
resolved = await resolveObjects(job);
|
|
@@ -217,8 +229,16 @@ async function run(job: QueueTask): Promise<void> {
|
|
|
217
229
|
|
|
218
230
|
let result;
|
|
219
231
|
try {
|
|
220
|
-
result = await evaluate(req);
|
|
232
|
+
result = await evaluate(req, signal);
|
|
221
233
|
} catch (err) {
|
|
234
|
+
// Cancelled is not a fault: the process was stopped while this ran. The claim still goes
|
|
235
|
+
// back — the row is terminal, so nothing re-claims it, and the release is what stops it
|
|
236
|
+
// waiting out a lease nobody is serving.
|
|
237
|
+
if (err instanceof Cancelled) {
|
|
238
|
+
console.log(`cancelled: ${job.token}`);
|
|
239
|
+
await release(job.token);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
222
242
|
// The RUNNER faulted, not the script — the one class where a retry can help. There is no
|
|
223
243
|
// error code for it on purpose: releasing the claim is how a queue spells "retryable", and
|
|
224
244
|
// it puts the task in front of a different worker instead of burning the definition's
|
|
@@ -256,11 +276,22 @@ async function renewLoop(): Promise<void> {
|
|
|
256
276
|
tokens,
|
|
257
277
|
lease_ms: LEASE_MS,
|
|
258
278
|
});
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
279
|
+
if (!ok) continue;
|
|
280
|
+
|
|
281
|
+
// Cancelled: the process was stopped, so the evaluation is abandoned and the claim handed
|
|
282
|
+
// back. Aborting first is the point of the list — waiting for the script to finish would
|
|
283
|
+
// keep burning a core on work an operator has already stopped. The release rides run()'s
|
|
284
|
+
// own catch, so nothing is released twice.
|
|
285
|
+
for (const token of (data?.cancelled ?? []) as string[]) {
|
|
286
|
+
inFlight.get(token)?.abort.abort();
|
|
287
|
+
}
|
|
288
|
+
// Lost: someone else holds this claim now. The work continues and its answer will be
|
|
289
|
+
// refused — that is not fixable here — but say so, because it is the signal that LEASE_MS
|
|
290
|
+
// is too short for what these scripts actually take. Deliberately NOT released: the claim
|
|
291
|
+
// is the new holder's, and releasing would bump the epoch out from under it.
|
|
292
|
+
const lost = (data?.lost ?? []) as string[];
|
|
293
|
+
if (lost.length) {
|
|
294
|
+
console.error(`lost ${lost.length}/${tokens.length} claims; a lease lapsed under load`);
|
|
264
295
|
}
|
|
265
296
|
}
|
|
266
297
|
}
|
|
@@ -270,8 +301,9 @@ async function pollLoop(): Promise<void> {
|
|
|
270
301
|
const free = CONCURRENCY - inFlight.size;
|
|
271
302
|
const jobs = free > 0 ? await claim(free) : [];
|
|
272
303
|
for (const job of jobs) {
|
|
273
|
-
|
|
274
|
-
|
|
304
|
+
const abort = new AbortController();
|
|
305
|
+
inFlight.set(job.token, { job, abort });
|
|
306
|
+
void run(job, abort.signal).finally(() => inFlight.delete(job.token));
|
|
275
307
|
}
|
|
276
308
|
// Only idle when there was nothing to take: a full queue should be drained at the speed the
|
|
277
309
|
// realms allow, not at the poll interval.
|
package/bin/import.mjs
DELETED
package/bin/worker.mjs
DELETED