// Race participant for the lock tests: waits for the go file, then claims the // binding once. Prints "claimed" or "refused" on stdout and its pid on // stderr. A winner stays alive (holding the lock) until the done file // appears, then releases it. import { existsSync } from "node:fs"; import { writePid, clearPid } from "../src/journal.mjs"; const [dir, go, done] = process.argv.slice(2); function waitFor(path) { const deadline = Date.now() + 10_000; while (!existsSync(path)) { if (Date.now() > deadline) { process.stderr.write(" timeout"); process.exit(2); } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 2); } } waitFor(go); process.stderr.write(String(process.pid)); let claimed = false; try { writePid(dir, process.pid); claimed = true; process.stdout.write("claimed\n"); } catch (err) { if (!/another connector is running|without an owner record|which is gone|cannot be verified|cannot be read|STOP is present/.test(err.message)) { process.stderr.write(" " + err.message); process.exit(1); } process.stdout.write(/STOP is present/.test(err.message) ? "stopped\n" : "refused\n"); } if (claimed) { waitFor(done); clearPid(dir, process.pid); }