import { mkdir, open, writeFile, readFile, rm } from "node:fs/promises"; import { constants } from "node:fs"; const jobs = process.env.JOB_ROOT; if (!jobs) throw new Error("JOB_ROOT required"); const id = process.env.RUN_ID ?? "a".repeat(32); const barrier = process.env.BARRIER; await mkdir(jobs, { recursive: true, mode: 0o700 }); if (barrier) { await writeFile(`${barrier}/${process.pid}.ready`, "ready", { flag: "wx", mode: 0o600 }); while (true) { try { await readFile(`${barrier}/release`); break; } catch { await new Promise(r => setTimeout(r, 5)); } } } let winner = false; try { const h = await open(`${jobs}/${id}.json`, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); await h.writeFile(JSON.stringify({ schemaVersion: 1, runId: id, phase: "request_claimed" }) + "\n"); await h.sync(); await h.close(); winner = true; } catch (e) { if (e?.code !== "EEXIST") throw e; } process.stdout.write(JSON.stringify({ ok: true, runId: id, winner }) + "\n");