@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/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
- const inFlight = new Map<string, QueueTask>();
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
- // 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`);
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
- inFlight.set(job.token, job);
281
- void run(job).finally(() => inFlight.delete(job.token));
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.