@genroc/eval-node 0.0.0-edge.b8f0518 → 0.0.0-edge.c42aeea

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
@@ -5,7 +5,8 @@
5
5
  //
6
6
  // See README.md for the contract, and specs/external-task-queue.md for the queue itself.
7
7
 
8
- import { evaluate, type EvalRequest, type FailureKind } from "./eval.ts";
8
+ import { readFileSync } from "node:fs";
9
+ import { Cancelled, evaluate, type EvalRequest, type FailureKind } from "./eval.ts";
9
10
 
10
11
  const SERVER = (process.env.GENROC_SERVER ?? "http://localhost:8448").replace(/\/$/, "");
11
12
  const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
@@ -17,7 +18,12 @@ const WORKER_ID = process.env.WORKER_ID ?? `evaluator-${process.pid}`;
17
18
  // Sent as a header rather than in the URL because Node's fetch REFUSES a URL carrying
18
19
  // credentials ("Request cannot be constructed from a URL that includes credentials"), so the
19
20
  // basic-auth-in-the-URL trick that works for genctl is not available here.
20
- const TOKEN = process.env.GENROC_TOKEN ?? "";
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() : "");
21
27
  const authHeaders: Record<string, string> = TOKEN ? { authorization: `Bearer ${TOKEN}` } : {};
22
28
  // Concurrency is the worker's to set, and that is the point of pulling: under the old fetch
23
29
  // shape genroc decided how many scripts ran at once (--max-concurrent, default 200) and the
@@ -129,7 +135,12 @@ function asEvalRequest(input: unknown): EvalRequest | string {
129
135
  };
130
136
  }
131
137
 
132
- 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>();
133
144
  let running = true;
134
145
 
135
146
  // Whether the last claim reached genroc. A worker polls several times a second, so an
@@ -197,7 +208,7 @@ async function answer(token: string, outcome: Record<string, unknown>): Promise<
197
208
  await release(token);
198
209
  }
199
210
 
200
- async function run(job: QueueTask): Promise<void> {
211
+ async function run(job: QueueTask, signal: AbortSignal): Promise<void> {
201
212
  let resolved: unknown;
202
213
  try {
203
214
  resolved = await resolveObjects(job);
@@ -218,8 +229,16 @@ async function run(job: QueueTask): Promise<void> {
218
229
 
219
230
  let result;
220
231
  try {
221
- result = await evaluate(req);
232
+ result = await evaluate(req, signal);
222
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
+ }
223
242
  // The RUNNER faulted, not the script — the one class where a retry can help. There is no
224
243
  // error code for it on purpose: releasing the claim is how a queue spells "retryable", and
225
244
  // it puts the task in front of a different worker instead of burning the definition's
@@ -257,11 +276,22 @@ async function renewLoop(): Promise<void> {
257
276
  tokens,
258
277
  lease_ms: LEASE_MS,
259
278
  });
260
- // A short count means a claim lapsed and was taken over. Nothing to do about it — the work
261
- // continues and its answer will be refused — but say so, because it is the signal that
262
- // LEASE_MS is too short for what these scripts actually take.
263
- if (ok && data?.renewed < tokens.length) {
264
- 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`);
265
295
  }
266
296
  }
267
297
  }
@@ -271,8 +301,9 @@ async function pollLoop(): Promise<void> {
271
301
  const free = CONCURRENCY - inFlight.size;
272
302
  const jobs = free > 0 ? await claim(free) : [];
273
303
  for (const job of jobs) {
274
- inFlight.set(job.token, job);
275
- 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));
276
307
  }
277
308
  // Only idle when there was nothing to take: a full queue should be drained at the speed the
278
309
  // realms allow, not at the poll interval.