sweepJobs
uses node:path
Clear away the files of jobs in dir that ended (or whose process is gone) more than age ms ago. The ids they
had aren't given again (lastJobId).
import { jobIds, lastJobId, processAlive } from "#codebase/tcmts/cli/mcp"; import type { Job, JobEnd } from "#codebase/tcmts/cli/mcp"; import fs from "node:fs"; import path from "node:path"; export function sweepJobs(dir: string, age = 24 * 60 * 60_000, now = Date.now()): number[] { // (the last id said first: it isn't known from the files once they're gone) const last = lastJobId(dir); const gone = jobIds(dir).filter((id) => { const file = (ext: string) => path.join(dir, `${id}.${ext}`); let at: number | undefined; try { at = (JSON.parse(fs.readFileSync(file("exit"), "utf8")) as JobEnd).at; } catch { try { const job = JSON.parse(fs.readFileSync(file("json"), "utf8")) as Job; if (!processAlive(job.pid)) at = job.started; } catch {} } if (at === undefined || at > now - age) return false; for (const ext of ["json", "log", "exit"]) fs.rmSync(file(ext), { force: true }); return true; }); if (gone.length) fs.writeFileSync(path.join(dir, "last"), String(last)); return gone; }
uses
- tcmts.cli.mcp.Job
- tcmts.cli.mcp.JobEnd
- tcmts.cli.mcp.jobIds
- tcmts.cli.mcp.lastJobId
- tcmts.cli.mcp.processAlive