@genroc/eval-node 0.0.0-edge.a60e314 → 0.0.0-edge.afdb7a4
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 +202 -0
- package/README.md +58 -34
- package/dist/eval.js +25 -4
- package/dist/import.js +104 -59
- package/dist/realm.js +66 -75
- package/dist/worker.js +30 -10
- package/eval.ts +25 -4
- package/import.ts +189 -80
- package/package.json +1 -1
- package/realm.ts +61 -77
- package/worker.ts +36 -11
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 { evaluate, type EvalRequest, type FailureKind } from "./eval.ts";
|
|
9
|
+
import { Cancelled, 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,7 +135,12 @@ function asEvalRequest(input: unknown): EvalRequest | string {
|
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
|
|
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>();
|
|
139
144
|
let running = true;
|
|
140
145
|
|
|
141
146
|
// Whether the last claim reached genroc. A worker polls several times a second, so an
|
|
@@ -203,7 +208,7 @@ async function answer(token: string, outcome: Record<string, unknown>): Promise<
|
|
|
203
208
|
await release(token);
|
|
204
209
|
}
|
|
205
210
|
|
|
206
|
-
async function run(job: QueueTask): Promise<void> {
|
|
211
|
+
async function run(job: QueueTask, signal: AbortSignal): Promise<void> {
|
|
207
212
|
let resolved: unknown;
|
|
208
213
|
try {
|
|
209
214
|
resolved = await resolveObjects(job);
|
|
@@ -224,8 +229,16 @@ async function run(job: QueueTask): Promise<void> {
|
|
|
224
229
|
|
|
225
230
|
let result;
|
|
226
231
|
try {
|
|
227
|
-
result = await evaluate(req);
|
|
232
|
+
result = await evaluate(req, signal);
|
|
228
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
|
+
}
|
|
229
242
|
// The RUNNER faulted, not the script — the one class where a retry can help. There is no
|
|
230
243
|
// error code for it on purpose: releasing the claim is how a queue spells "retryable", and
|
|
231
244
|
// it puts the task in front of a different worker instead of burning the definition's
|
|
@@ -263,11 +276,22 @@ async function renewLoop(): Promise<void> {
|
|
|
263
276
|
tokens,
|
|
264
277
|
lease_ms: LEASE_MS,
|
|
265
278
|
});
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
//
|
|
269
|
-
|
|
270
|
-
|
|
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`);
|
|
271
295
|
}
|
|
272
296
|
}
|
|
273
297
|
}
|
|
@@ -277,8 +301,9 @@ async function pollLoop(): Promise<void> {
|
|
|
277
301
|
const free = CONCURRENCY - inFlight.size;
|
|
278
302
|
const jobs = free > 0 ? await claim(free) : [];
|
|
279
303
|
for (const job of jobs) {
|
|
280
|
-
|
|
281
|
-
|
|
304
|
+
const abort = new AbortController();
|
|
305
|
+
inFlight.set(job.token, { job, abort });
|
|
306
|
+
void run(job, abort.signal).finally(() => inFlight.delete(job.token));
|
|
282
307
|
}
|
|
283
308
|
// Only idle when there was nothing to take: a full queue should be drained at the speed the
|
|
284
309
|
// realms allow, not at the poll interval.
|