// A TEST'S TEARDOWN, BEFORE IT REMOVES ITS TEMP CORPUS. // // A managed job's status reads `done` (registry.finalize) BEFORE the job has // stopped writing: its `.finally` still flushes the log and rewrites the // sidecar in `.jobs/`, and its end arms the snapshot scheduler's debounce // (jobs/snapshotScheduler.ts, ~1 s on the default preset), whose // refresh-report job then writes `channels//snapshot.json` and a job // of its own. A test that polls the status and then `rm`s its temp dir races // all of that — `ENOTEMPTY: rmdir …/channels/` when the regen lands // mid-removal, which a loaded machine makes likely. // // `quiesceJobs` waits until no job is queued or running, the scheduler has no // regeneration pending or starting (a pending one is fired now, not waited // out), and every job's sidecar on disk says it ended. `settleJob` waits for // one job the same way. Both are bounded by a deadline that is a backstop // against a real hang (it throws, naming what is still live), not a measure // of how long a job should take. import { getRegistry, type JobStatus } from "../../jobs/registry"; import { stat } from "node:fs/promises"; import { metaPath, readJobMeta } from "../../jobs/jobMeta"; import { flushChannelSnapshotsNow } from "../../jobs/snapshotScheduler"; import { getPaths, type Paths } from "../../lib/paths"; const LIVE: ReadonlySet = new Set(["queued", "running"]); const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); // A job's end is on disk: its sidecar names a terminal status. A job with no // sidecar at all (a record a test registered by hand) has nothing left to // write; one mid-rewrite reads as unparseable, which is not ended yet. async function endedOnDisk(paths: Paths, id: string): Promise { const meta = await readJobMeta(paths, id); if (meta) return !LIVE.has(meta.status); try { await stat(metaPath(paths, id)); return false; // there, but not readable yet: being rewritten } catch { return true; } } function schedulerBusy(): boolean { const s = globalThis.__yttSnapshotScheduler__; return !!s && (s.timer !== null || s.dirty.size > 0 || s.starting.size > 0); } export async function settleJob( id: string, opts: { paths?: Paths; timeoutMs?: number } = {}, ): Promise { const paths = opts.paths ?? getPaths(); const deadline = Date.now() + (opts.timeoutMs ?? 120_000); for (;;) { const status = getRegistry().get(id)?.status; if (status === undefined) return undefined; if (!LIVE.has(status) && (await endedOnDisk(paths, id))) return status; if (Date.now() > deadline) throw new Error(`job ${id} still ${status} after ${opts.timeoutMs ?? 120_000} ms`); await sleep(25); } } export async function quiesceJobs(opts: { paths?: Paths; timeoutMs?: number } = {}): Promise { const paths = opts.paths ?? getPaths(); const timeoutMs = opts.timeoutMs ?? 120_000; const deadline = Date.now() + timeoutMs; for (;;) { // A pending regeneration is fired now and awaited (its stream drained). await flushChannelSnapshotsNow(); const jobs = getRegistry().list(); const live = jobs.filter((j) => LIVE.has(j.status)); let unwritten: string[] = []; if (live.length === 0 && !schedulerBusy()) { const ended = await Promise.all(jobs.map((j) => endedOnDisk(paths, j.id))); unwritten = jobs.filter((_, i) => !ended[i]).map((j) => j.id); if (unwritten.length === 0 && !schedulerBusy()) return; } if (Date.now() > deadline) { throw new Error( `jobs still live after ${timeoutMs} ms: ` + [...live.map((j) => `${j.kind} ${j.id} ${j.status}`), ...unwritten.map((id) => `${id} (sidecar not terminal)`)].join(", ") + (schedulerBusy() ? "; a report regeneration is pending" : ""), ); } await sleep(25); } }