import path from "node:path"; import { readRenderRecord } from "@/lib/report/renderRecord.mjs"; import { recentRenders } from "./cli.mjs"; import { queueDir, queueRunnerStatus } from "./runner"; import { listSteps, readLease, resolveStep, TERMINAL } from "./store.mjs"; // --------------------------------------------------------------------------- // What the pages show of the queue and the render records (/jobs, the Takes // panel, GET /api/queue): read from the queue dir and the takes' own // render.json, resolved as the CLI resolves them (a running step or render // whose process is gone reads failed). SERVER ONLY. // --------------------------------------------------------------------------- export type StepView = { qid: string; label: string; argv: string[]; state: "queued" | "running" | "done" | "failed" | "blocked" | "cancelled"; after: string | null; target: { project: string | null; take: string | null } | null; createdAt: string; startedAt: string | null; endedAt: string | null; exitCode: number | null; error: string | null; blockedBy: string | null; attempts: number; cancelRequested: { at: string; by: string } | null; }; export type RenderView = { project: string | null; take: string; label: string | null; state: "queued" | "waiting" | "running" | "done" | "failed" | "cancelled"; startedAt: string; endedAt: string | null; exitCode: number | null; stage: string | null; percent: number | null; entries?: { done: number; of: number | null }; error: string | null; tail: string[]; mp4: string | null; /** The project's directory (the jobs' pointer, or the take's reader): the mp4's absolute path is projectDir/takes//. */ projectDir?: string | null; seconds: number | null; pid: number; gone?: boolean; }; export type RunnerView = { live: boolean; pid: number | null; since: string | null; here: boolean; waiting: string | null; lastError: string | null }; const DAY_MS = 86_400_000; /** The queue: the runner, and the steps (not finished, or finished in the last day; `all` every one). */ export async function queueView({ project = null, all = false }: { project?: string | null; all?: boolean } = {}) { const dir = queueDir(); const status = queueRunnerStatus(); const owned = new Set(status.running); const [lease, raw] = await Promise.all([readLease(dir), listSteps(dir)]); const cutoff = Date.now() - DAY_MS; const steps = (raw as StepView[]) .map((s) => resolveStep(s, { owned }) as StepView) .filter((s) => all || !TERMINAL.has(s.state) || Date.parse(s.endedAt ?? s.createdAt) > cutoff) .filter((s) => !project || s.target?.project === project) .reverse(); const runner: RunnerView = { live: !!lease?.live, pid: lease?.pid ?? null, since: lease?.since ?? null, here: !!lease?.live && lease.pid === process.pid, waiting: status.waiting, lastError: status.lastError, }; return { dir, runner, steps }; } /** The recent take renders across projects (the queue dir's pointers). */ export async function rendersView(): Promise { return (await recentRenders(queueDir())) as unknown as RenderView[]; } /** One take's render record, resolved, or null. */ export async function takeRender(projectDir: string, take: string): Promise { const rec = (await readRenderRecord(path.join(/* turbopackIgnore: true */ projectDir, "takes", take))) as RenderView | null; return rec ? { ...rec, projectDir } : null; }