@genroc/eval-node 0.0.0-edge.e5d3af3 → 0.0.0-edge.e968531
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 +0 -17
- package/dist/eval.js +1 -21
- package/dist/worker.js +10 -30
- package/eval.ts +1 -20
- package/package.json +1 -1
- package/worker.ts +11 -36
package/README.md
CHANGED
|
@@ -90,23 +90,6 @@ queue worker rather than the HTTP sidecar it used to be:
|
|
|
90
90
|
that genroc had to be able to reach. A worker only needs outbound access, so it can live
|
|
91
91
|
anywhere — behind NAT, in another trust zone.
|
|
92
92
|
|
|
93
|
-
The cost of that inversion is that genroc cannot reach a running worker, so anything it needs
|
|
94
|
-
to say has to ride the renewal the worker already makes. **Renewing is therefore mandatory,
|
|
95
|
-
not an optimisation** — a worker that stops renewing is indistinguishable from one that died.
|
|
96
|
-
The response answers per token:
|
|
97
|
-
|
|
98
|
-
| list | what it means | what this worker does |
|
|
99
|
-
|---|---|---|
|
|
100
|
-
| `renewed` | still yours | carry on |
|
|
101
|
-
| `lost` | already someone else's | stop; do **not** release — that would bump the new holder's claim |
|
|
102
|
-
| `cancelled` | still yours, nobody wants it | abort the script and release the claim |
|
|
103
|
-
|
|
104
|
-
`cancelled` is how an operator's `genctl cancel` reaches work already running: the evaluation
|
|
105
|
-
is aborted through an `AbortSignal`, its realm is terminated, and no outcome is submitted —
|
|
106
|
-
the process is stopping, so there is nothing left to answer. `renew_before_ms` on the claim
|
|
107
|
-
says how long the worker may wait before renewing again, so the interval is a value it reads
|
|
108
|
-
rather than one it guesses.
|
|
109
|
-
|
|
110
93
|
## The task input
|
|
111
94
|
|
|
112
95
|
The `input` of the external task IS the evaluation request:
|
package/dist/eval.js
CHANGED
|
@@ -20,31 +20,13 @@ class RealmFault extends Error {
|
|
|
20
20
|
this.name = "RealmFault";
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
* report and worker.ts must release rather than answer. Distinct from RealmFault because the
|
|
25
|
-
* runner did not fault -- nothing is wrong, the answer is simply no longer wanted. */
|
|
26
|
-
export class Cancelled extends Error {
|
|
27
|
-
constructor() {
|
|
28
|
-
super("cancelled");
|
|
29
|
-
this.name = "Cancelled";
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export async function evaluate(req, signal) {
|
|
23
|
+
export async function evaluate(req) {
|
|
33
24
|
const budget = typeof req.timeout_ms === "number" ? req.timeout_ms : DEFAULT_TIMEOUT_MS;
|
|
34
25
|
const worker = new Worker(REALM_URL);
|
|
35
26
|
let timer;
|
|
36
|
-
let onAbort;
|
|
37
27
|
try {
|
|
38
28
|
return await new Promise((resolve, reject) => {
|
|
39
29
|
timer = setTimeout(() => resolve(timedOut(budget)), budget);
|
|
40
|
-
// The abort is wired to the same promise as the budget, so both settle through the one
|
|
41
|
-
// finally below -- which is what guarantees the thread is gone before either returns.
|
|
42
|
-
if (signal) {
|
|
43
|
-
if (signal.aborted)
|
|
44
|
-
reject(new Cancelled());
|
|
45
|
-
onAbort = () => reject(new Cancelled());
|
|
46
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
47
|
-
}
|
|
48
30
|
worker.once("message", (reply) => resolve(reply));
|
|
49
31
|
// A script may end its own realm (`process.exit()`), which is not a throw and would
|
|
50
32
|
// otherwise present as a hang until the budget expired. Our own terminate() raises
|
|
@@ -56,8 +38,6 @@ export async function evaluate(req, signal) {
|
|
|
56
38
|
}
|
|
57
39
|
finally {
|
|
58
40
|
clearTimeout(timer);
|
|
59
|
-
if (signal && onAbort)
|
|
60
|
-
signal.removeEventListener("abort", onAbort);
|
|
61
41
|
// Awaited, and the whole point: on the timeout path a thread is still burning a core, and
|
|
62
42
|
// resolving before it is gone would report an evaluation the machine is still running.
|
|
63
43
|
await worker.terminate();
|
package/dist/worker.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
//
|
|
6
6
|
// See README.md for the contract, and specs/external-task-queue.md for the queue itself.
|
|
7
7
|
import { readFileSync } from "node:fs";
|
|
8
|
-
import {
|
|
8
|
+
import { evaluate } from "./eval.js";
|
|
9
9
|
const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
|
|
10
10
|
const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
11
11
|
// The credential, when the server runs with --auth token. A worker needs exactly the `worker`
|
|
@@ -185,7 +185,7 @@ async function answer(token, outcome) {
|
|
|
185
185
|
console.error(`genroc refused the outcome for ${token}: ${JSON.stringify(data)}`);
|
|
186
186
|
await release(token);
|
|
187
187
|
}
|
|
188
|
-
async function run(job
|
|
188
|
+
async function run(job) {
|
|
189
189
|
let resolved;
|
|
190
190
|
try {
|
|
191
191
|
resolved = await resolveObjects(job);
|
|
@@ -206,17 +206,9 @@ async function run(job, signal) {
|
|
|
206
206
|
}
|
|
207
207
|
let result;
|
|
208
208
|
try {
|
|
209
|
-
result = await evaluate(req
|
|
209
|
+
result = await evaluate(req);
|
|
210
210
|
}
|
|
211
211
|
catch (err) {
|
|
212
|
-
// Cancelled is not a fault: the process was stopped while this ran. The claim still goes
|
|
213
|
-
// back — the row is terminal, so nothing re-claims it, and the release is what stops it
|
|
214
|
-
// waiting out a lease nobody is serving.
|
|
215
|
-
if (err instanceof Cancelled) {
|
|
216
|
-
console.log(`cancelled: ${job.token}`);
|
|
217
|
-
await release(job.token);
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
212
|
// The RUNNER faulted, not the script — the one class where a retry can help. There is no
|
|
221
213
|
// error code for it on purpose: releasing the claim is how a queue spells "retryable", and
|
|
222
214
|
// it puts the task in front of a different worker instead of burning the definition's
|
|
@@ -253,22 +245,11 @@ async function renewLoop() {
|
|
|
253
245
|
tokens,
|
|
254
246
|
lease_ms: LEASE_MS,
|
|
255
247
|
});
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
// own catch, so nothing is released twice.
|
|
262
|
-
for (const token of (data?.cancelled ?? [])) {
|
|
263
|
-
inFlight.get(token)?.abort.abort();
|
|
264
|
-
}
|
|
265
|
-
// Lost: someone else holds this claim now. The work continues and its answer will be
|
|
266
|
-
// refused — that is not fixable here — but say so, because it is the signal that LEASE_MS
|
|
267
|
-
// is too short for what these scripts actually take. Deliberately NOT released: the claim
|
|
268
|
-
// is the new holder's, and releasing would bump the epoch out from under it.
|
|
269
|
-
const lost = (data?.lost ?? []);
|
|
270
|
-
if (lost.length) {
|
|
271
|
-
console.error(`lost ${lost.length}/${tokens.length} claims; a lease lapsed under load`);
|
|
248
|
+
// A short count means a claim lapsed and was taken over. Nothing to do about it — the work
|
|
249
|
+
// continues and its answer will be refused — but say so, because it is the signal that
|
|
250
|
+
// LEASE_MS is too short for what these scripts actually take.
|
|
251
|
+
if (ok && data?.renewed < tokens.length) {
|
|
252
|
+
console.error(`renewed ${data.renewed}/${tokens.length} claims; a lease lapsed under load`);
|
|
272
253
|
}
|
|
273
254
|
}
|
|
274
255
|
}
|
|
@@ -277,9 +258,8 @@ async function pollLoop() {
|
|
|
277
258
|
const free = CONCURRENCY - inFlight.size;
|
|
278
259
|
const jobs = free > 0 ? await claim(free) : [];
|
|
279
260
|
for (const job of jobs) {
|
|
280
|
-
|
|
281
|
-
inFlight.
|
|
282
|
-
void run(job, abort.signal).finally(() => inFlight.delete(job.token));
|
|
261
|
+
inFlight.set(job.token, job);
|
|
262
|
+
void run(job).finally(() => inFlight.delete(job.token));
|
|
283
263
|
}
|
|
284
264
|
// Only idle when there was nothing to take: a full queue should be drained at the speed the
|
|
285
265
|
// realms allow, not at the poll interval.
|
package/eval.ts
CHANGED
|
@@ -57,32 +57,14 @@ class RealmFault extends Error {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
* report and worker.ts must release rather than answer. Distinct from RealmFault because the
|
|
62
|
-
* runner did not fault -- nothing is wrong, the answer is simply no longer wanted. */
|
|
63
|
-
export class Cancelled extends Error {
|
|
64
|
-
constructor() {
|
|
65
|
-
super("cancelled");
|
|
66
|
-
this.name = "Cancelled";
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export async function evaluate(req: EvalRequest, signal?: AbortSignal): Promise<EvalResult> {
|
|
60
|
+
export async function evaluate(req: EvalRequest): Promise<EvalResult> {
|
|
71
61
|
const budget = typeof req.timeout_ms === "number" ? req.timeout_ms : DEFAULT_TIMEOUT_MS;
|
|
72
62
|
|
|
73
63
|
const worker = new Worker(REALM_URL);
|
|
74
64
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
75
|
-
let onAbort: (() => void) | undefined;
|
|
76
65
|
try {
|
|
77
66
|
return await new Promise<EvalResult>((resolve, reject) => {
|
|
78
67
|
timer = setTimeout(() => resolve(timedOut(budget)), budget);
|
|
79
|
-
// The abort is wired to the same promise as the budget, so both settle through the one
|
|
80
|
-
// finally below -- which is what guarantees the thread is gone before either returns.
|
|
81
|
-
if (signal) {
|
|
82
|
-
if (signal.aborted) reject(new Cancelled());
|
|
83
|
-
onAbort = () => reject(new Cancelled());
|
|
84
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
85
|
-
}
|
|
86
68
|
worker.once("message", (reply: WorkerReply) => resolve(reply));
|
|
87
69
|
// A script may end its own realm (`process.exit()`), which is not a throw and would
|
|
88
70
|
// otherwise present as a hang until the budget expired. Our own terminate() raises
|
|
@@ -93,7 +75,6 @@ export async function evaluate(req: EvalRequest, signal?: AbortSignal): Promise<
|
|
|
93
75
|
});
|
|
94
76
|
} finally {
|
|
95
77
|
clearTimeout(timer);
|
|
96
|
-
if (signal && onAbort) signal.removeEventListener("abort", onAbort);
|
|
97
78
|
// Awaited, and the whole point: on the timeout path a thread is still burning a core, and
|
|
98
79
|
// resolving before it is gone would report an evaluation the machine is still running.
|
|
99
80
|
await worker.terminate();
|
package/package.json
CHANGED
package/worker.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// See README.md for the contract, and specs/external-task-queue.md for the queue itself.
|
|
7
7
|
|
|
8
8
|
import { readFileSync } from "node:fs";
|
|
9
|
-
import {
|
|
9
|
+
import { evaluate, type EvalRequest, type FailureKind } from "./eval.ts";
|
|
10
10
|
|
|
11
11
|
const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
|
|
12
12
|
const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
|
|
@@ -135,12 +135,7 @@ function asEvalRequest(input: unknown): EvalRequest | string {
|
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
|
|
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>();
|
|
138
|
+
const inFlight = new Map<string, QueueTask>();
|
|
144
139
|
let running = true;
|
|
145
140
|
|
|
146
141
|
// Whether the last claim reached genroc. A worker polls several times a second, so an
|
|
@@ -208,7 +203,7 @@ async function answer(token: string, outcome: Record<string, unknown>): Promise<
|
|
|
208
203
|
await release(token);
|
|
209
204
|
}
|
|
210
205
|
|
|
211
|
-
async function run(job: QueueTask
|
|
206
|
+
async function run(job: QueueTask): Promise<void> {
|
|
212
207
|
let resolved: unknown;
|
|
213
208
|
try {
|
|
214
209
|
resolved = await resolveObjects(job);
|
|
@@ -229,16 +224,8 @@ async function run(job: QueueTask, signal: AbortSignal): Promise<void> {
|
|
|
229
224
|
|
|
230
225
|
let result;
|
|
231
226
|
try {
|
|
232
|
-
result = await evaluate(req
|
|
227
|
+
result = await evaluate(req);
|
|
233
228
|
} 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
|
-
}
|
|
242
229
|
// The RUNNER faulted, not the script — the one class where a retry can help. There is no
|
|
243
230
|
// error code for it on purpose: releasing the claim is how a queue spells "retryable", and
|
|
244
231
|
// it puts the task in front of a different worker instead of burning the definition's
|
|
@@ -276,22 +263,11 @@ async function renewLoop(): Promise<void> {
|
|
|
276
263
|
tokens,
|
|
277
264
|
lease_ms: LEASE_MS,
|
|
278
265
|
});
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
//
|
|
282
|
-
|
|
283
|
-
|
|
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`);
|
|
266
|
+
// A short count means a claim lapsed and was taken over. Nothing to do about it — the work
|
|
267
|
+
// continues and its answer will be refused — but say so, because it is the signal that
|
|
268
|
+
// LEASE_MS is too short for what these scripts actually take.
|
|
269
|
+
if (ok && data?.renewed < tokens.length) {
|
|
270
|
+
console.error(`renewed ${data.renewed}/${tokens.length} claims; a lease lapsed under load`);
|
|
295
271
|
}
|
|
296
272
|
}
|
|
297
273
|
}
|
|
@@ -301,9 +277,8 @@ async function pollLoop(): Promise<void> {
|
|
|
301
277
|
const free = CONCURRENCY - inFlight.size;
|
|
302
278
|
const jobs = free > 0 ? await claim(free) : [];
|
|
303
279
|
for (const job of jobs) {
|
|
304
|
-
|
|
305
|
-
inFlight.
|
|
306
|
-
void run(job, abort.signal).finally(() => inFlight.delete(job.token));
|
|
280
|
+
inFlight.set(job.token, job);
|
|
281
|
+
void run(job).finally(() => inFlight.delete(job.token));
|
|
307
282
|
}
|
|
308
283
|
// Only idle when there was nothing to take: a full queue should be drained at the speed the
|
|
309
284
|
// realms allow, not at the poll interval.
|