Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit aa0d6e0c4ef428fc68d669300203b023d026fe95
parent c7b52810ac344c69592b36a318f0f4c405a60c7b
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Sat, 21 Nov 2026 04:27:20 -0500

record-live: remove a non-landed part only after the landed file is in the store

The removals added for covered landings ran before landMediaFile: a landing
that then failed (a full store, a drive gone, a probe) left only one copy of
the stream, unlanded. Now what is to go is decided before and removed after:
only once landMediaFile has returned and the stored file is there with its
size. A landing that fails removes nothing; the part that was to land and
every other part with bytes are kept and listed, the run fails and needs a
person.

The SIGINT test waited a fixed 400 ms before its Cancel, which a loaded
machine loses to node's start-up; it now waits for the child's own "ready"
line, printed once its handler is armed.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mcommon/controller/recordLive.test.ts | 32+++++++++++++++++++++++++++++---
Mcommon/controller/recordLive.ts | 86++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
2 files changed, 95 insertions(+), 23 deletions(-)

diff --git a/common/controller/recordLive.test.ts b/common/controller/recordLive.test.ts @@ -199,7 +199,7 @@ test("preclip: the knob parsed (implies transcribe), the pre-clip asked for once fetchText: async () => DVR, spawn: async ({ args }) => { // The pre-clip is asked for while the recorder runs. - await new Promise((r) => setImmediate(r)); + await new Promise((r) => setTimeout(r, 10)); await writeFile(args[args.indexOf("-o") + 1]!, "format=mpegts;seconds=60;"); return { exitCode: 0, stderrTail: "", signalled: false }; }, @@ -379,11 +379,18 @@ test("the VOD poller appends new segments in order, counts a gap, and ends when test("Cancel reaches the recorder as SIGINT, and it gets to finish its file", async () => { const out = path.join(ROOT, "sigint.txt"); - const script = `process.on("SIGINT", () => { require("fs").writeFileSync(${JSON.stringify(out)}, "finalized"); process.exit(0); }); console.log("frame= 1"); setInterval(() => {}, 1000);`; + // The child says "ready" once its SIGINT handler is armed; the Cancel is + // sent then, not after a fixed time (a loaded machine starts node slowly). + const script = `process.on("SIGINT", () => { require("fs").writeFileSync(${JSON.stringify(out)}, "finalized"); process.exit(0); }); console.log("frame= 1"); console.log("ready"); setInterval(() => {}, 1000);`; const ac = new AbortController(); const lines: string[] = []; const p = spawnRecorder({ bin: process.execPath, args: ["-e", script], cwd: ROOT, onLog: (l) => void lines.push(l), signal: ac.signal, label: "t" }); - await new Promise((r) => setTimeout(r, 400)); + const readyBy = Date.now() + 60_000; + // The child's own line (not the echo of its command, which names it too). + while (!lines.some((l) => /^(?:\[t\] )?ready\s*$/.test(l))) { + assert.ok(Date.now() < readyBy, "the child never said it was ready"); + await new Promise((r) => setTimeout(r, 10)); + } ac.abort(); const o = await p; assert.equal(o.signalled, true); @@ -1149,3 +1156,22 @@ test("not covered (d): a stream whose length cannot be established keeps every o assert.equal(row.keptParts?.length, 1); assert.equal(row.needsAttention, true); }); + +test("a landing that fails removes nothing: the covered landing's other part and the part itself stay, listed, and the row needs a person", async () => { + const id = "exmplland01"; + const earlier = await leftover(id, 300); + const { run } = await incidentRun(id, writesAfter(0), { + meta: { ...YT_META(id), release_timestamp: Math.floor(Date.now() / 1000) - 590 }, + // The store's disk is full: landMediaFile throws. + deps: { land: { ...land, freeBytes: async () => 0 } }, + }); + await assert.rejects(run, /the landing failed/); + assert.ok((await stat(earlier)).size > 0, "the earlier part is still on disk"); + const mine = (await readdir(store())).filter((n) => n.startsWith(`.${id}.`) && n !== path.basename(earlier)); + assert.equal(mine.length, 1, "this run's part is still on disk"); + const row = (await rowOf(id))[0]!; + assert.equal(row.status, "failed"); + assert.equal(row.removedParts, undefined); + assert.equal(row.keptParts?.length, 2); + assert.equal(row.needsAttention, true); +}); diff --git a/common/controller/recordLive.ts b/common/controller/recordLive.ts @@ -1398,6 +1398,10 @@ async function recordLiveRunInner( others.push({ group: g, why: "fragments a recorder left, not landable as they are", ...(part ? { part } : {}) }); } const removed: RemovedPart[] = []; + // DECIDED BEFORE THE LANDING, DONE AFTER IT: a part is removed only once the + // landed file is in the store (landMediaFile returned and the stored file is + // there with its size). A landing that fails removes nothing. + const toRemove: { group: LivePartFiles; why: string; extra: string[]; o: Other }[] = []; const removeGroup = async (g: LivePartFiles, why: string, extra: string[] = []) => { let bytes = 0; for (const f of [...new Set([...extra.map((e) => path.basename(e)), ...g.finished, ...g.streams, ...g.scratch])]) { @@ -1454,7 +1458,12 @@ async function recordLiveRunInner( say(`part ${path.basename(c.file)}: ${c.seconds !== null ? `${Math.round(c.seconds)} s` : "length unknown"}${c === best ? " — landed" : ""}`); if (c === best) continue; if (contained(c)) { - await removeGroup(c.group, "a shorter part of this run from its start: inside the landed part", [c.file]); + toRemove.push({ + group: c.group, + why: "a shorter part of this run from its start: inside the landed part", + extra: [c.file], + o: { group: c.group, why: `recorded ${where(c.part)}, not landed`, seconds: c.seconds, ...(c.part ? { part: c.part } : {}), extra: [c.file] }, + }); continue; } others.push({ @@ -1496,29 +1505,66 @@ async function recordLiveRunInner( // not known — every other part with bytes is kept and needs a person. for (const o of others) { if (cov.covers) { - await removeGroup( - o.group, - `inside the landed file, which holds the whole stream from its start (${spanWords(cov.capturedSeconds ?? 0)} of ${spanWords(cov.streamSeconds ?? 0)}, ${cov.basis === "duration" ? "its duration" : "by the clock"})`, - o.extra ?? [], - ); + toRemove.push({ + group: o.group, + why: `inside the landed file, which holds the whole stream from its start (${spanWords(cov.capturedSeconds ?? 0)} of ${spanWords(cov.streamSeconds ?? 0)}, ${cov.basis === "duration" ? "its duration" : "by the clock"})`, + extra: o.extra ?? [], + o, + }); } else { await keep(o.group, o.why, o); } } - const landed = await landMediaFile({ - paths, - slug: req.slug, - channelConfig: opts.channelConfig, - videoId: id, - sourcePath: best.file, - via: "record-live", - move: true, - url: plan.url, - note: req.note ?? `a live recording (${best.part?.method ?? "an earlier run's part"})`, - onLog, - signal: landSignal, - ...(deps.land ? { deps: deps.land } : {}), - }); + const bestBytes = await sizeOf(best.file); + // The landing failed (it threw, or its stored file is not there as it + // should be): nothing is removed, and every part with bytes — the one that + // was to land included — is kept and listed; the run failed. + const landingFailed = async (why: string): Promise<never> => { + await keep(best.group, `the landing failed (${why}): not landed`, { + seconds: best.seconds, + ...(best.part ? { part: best.part } : {}), + extra: [best.file], + }); + for (const r of toRemove) await keep(r.group, `${r.o.why}; kept because the landing failed`, r.o); + record.status = "failed"; + record.endedAt = now().toISOString(); + record.error = `the landing failed: ${why}`; + if (kept.length) record.keptParts = kept; + sayKept(); + await save(); + summary.status = record.status; + say(`${RECORD_LIVE_RESULT_MARKER}${JSON.stringify(summary)}`); + throw new Error(record.error); + }; + let landed: Awaited<ReturnType<typeof landMediaFile>>; + try { + landed = await landMediaFile({ + paths, + slug: req.slug, + channelConfig: opts.channelConfig, + videoId: id, + sourcePath: best.file, + via: "record-live", + move: true, + url: plan.url, + note: req.note ?? `a live recording (${best.part?.method ?? "an earlier run's part"})`, + onLog, + signal: landSignal, + ...(deps.land ? { deps: deps.land } : {}), + }); + } catch (err) { + return landingFailed((err as Error).message); + } + { + const st = landed.container ? await stat(landed.container).catch(() => null) : null; + const sizeOk = st?.isFile() && st.size > 0 && (landed.kept !== "new" || landed.finalized !== "copied" || st.size === bestBytes); + if (!sizeOk) { + await landingFailed( + st ? `the stored file is ${st.size} bytes, the part ${bestBytes}` : `the stored file ${landed.container ?? "(none)"} is not there`, + ); + } + } + for (const r of toRemove) await removeGroup(r.group, r.why, r.extra); // The landed part's files were staging scratch (it is moved or copied into // the store), and so were the streams a merge was made from. await rm(best.file, { force: true });