import { channelMediaStall, type ChannelMediaConfig } from "./channelMedia"; import { healthTimings, locationHealth, recordLocationHealth, sinceText, type LocationHealth, type LocationHealthState, } from "./storageHealth"; import { probeLocationHealth } from "./storageVolumes"; // A BATCH THAT OPENS MEDIA HOLDS WHILE ITS CHANNEL'S MEDIA DRIVE IS NOT // ANSWERING (lane drive-stall). // // The start-of-work guard (`assertChannelMediaReachable`) refuses a job whose // channel's drive is already stalled. A batch that started while the drive // answered, and is still pulling videos when it stops, was not asked again: // each next unit opened its audio in-process, held one of Node's file threads // for the ~30 s the drive takes to give up, and failed — over and over, a // thread at a time. So a batch over a channel's media asks this before every // pull: while the drive is stalled the pool's limit is 0 (runPool PAUSES, as // it does with every worker disabled), and when it answers the batch resumes. // The units already in flight finish or fail on their own; nothing cancels a // read the kernel holds. // // THE CHECK IS MEMORY: `channelMediaStall` reads the process's health record // (the health pass and the watchdog write it), never the drive. Edge-logged, // once when the hold begins and once when it ends, and "still holding" every // `stillHoldingEveryMs` (10 min) between. // // IT HEALS ITSELF (review of lane drive-stall). The record is cleared by the // health pass; a process with no pass covering the location (a CLI, a stall // recorded for a location the pass no longer covers) would otherwise hold for // ever. So once the hold has lasted `reprobeAfterMs` (5 min), and at most once // per that interval, it asks the drive itself — OUT OF PROCESS, a child `stat` // of the root raced against the probe timeout, which holds no thread of ours. // When the root answers: // - a record no pass has refreshed for three pass intervals is stale: the // answer clears it (as many clean answers as the clear rule needs), and // the batch resumes; // - a record a pass still keeps counts the answer once, as /storage's // Refresh does, and the pass decides: a child `stat` can be answered from // the kernel's cache while the device's own counters say stalled. // // THE CONFIG IS RE-READ (`reloadConfig`, at most every `reloadEveryMs`, 10 s, // in the background): a channel moved to another location mid-batch is held // by the drive its media is on now. export type MediaStallHold = { // The stalled location now, or null. Logs on each edge. check: () => LocationHealth | null; }; export type MediaStallHoldOptions = { // Re-read the channel's targets (default: never). The check uses the last // answer; a read that fails keeps the one before. reloadConfig?: () => Promise; reloadEveryMs?: number; // The out-of-process probe of a root (default: a child `stat`, // `probeLocationHealth`). Test seam. reprobe?: (root: string) => Promise; reprobeAfterMs?: number; stillHoldingEveryMs?: number; now?: () => number; }; export const MEDIA_HOLD_REPROBE_AFTER_MS = 5 * 60_000; export const MEDIA_HOLD_STILL_EVERY_MS = 10 * 60_000; const RELOAD_EVERY_MS = 10_000; function minutesText(ms: number): string { const m = Math.max(0, Math.round(ms / 60_000)); return `${m} min`; } export function mediaStallHold( initialConfig: ChannelMediaConfig | null | undefined, log: (line: string) => void, what: string, opts: MediaStallHoldOptions = {}, ): MediaStallHold { const now = opts.now ?? Date.now; const reprobeAfter = opts.reprobeAfterMs ?? MEDIA_HOLD_REPROBE_AFTER_MS; const stillEvery = opts.stillHoldingEveryMs ?? MEDIA_HOLD_STILL_EVERY_MS; const reloadEvery = opts.reloadEveryMs ?? RELOAD_EVERY_MS; const reprobe = opts.reprobe ?? ((root: string) => probeLocationHealth({ root })); let config = initialConfig; let lastReload = now(); let reloading = false; let holding: LocationHealth | null = null; let heldAt = 0; let lastStill = 0; let lastReprobe = 0; let reprobing = false; const maybeReload = () => { if (!opts.reloadConfig || reloading || now() - lastReload < reloadEvery) return; reloading = true; lastReload = now(); void opts .reloadConfig() .then( (next) => { if (next !== undefined) config = next; }, () => {}, ) .finally(() => { reloading = false; }); }; const maybeReprobe = (stall: LocationHealth) => { const t = now(); if (reprobing || t - heldAt < reprobeAfter || t - lastReprobe < reprobeAfter) return; reprobing = true; lastReprobe = t; void reprobe(stall.root) .then( (answer) => { if (answer === "stalled") return; const current = locationHealth(stall.id); if (!current || current.root !== stall.root || current.state !== "stalled") return; const loc = { id: stall.id, label: stall.label, root: stall.root }; const stale = now() - current.checkedAt >= healthTimings().passIntervalMs * 3; // Stale: no pass is keeping it, and the drive answered — enough // clean answers to clear it. Kept by a pass: one answer. const answers = stale ? healthTimings().clearAfterCleanPasses : 1; for (let i = 0; i < answers; i++) { recordLocationHealth(loc, answer, { detector: "stat", device: null }); } log( `The media drive "${stall.label}" answered a stat of its root after ` + `${minutesText(t - heldAt)} held` + (stale ? " — no health pass was keeping its stall record, so the stall is cleared." : "; its health pass decides when the stall ends."), ); }, () => {}, ) .finally(() => { reprobing = false; }); }; return { check: () => { maybeReload(); const stall = channelMediaStall(config); const t = now(); if (stall && !holding) { heldAt = t; lastStill = t; log( `Holding: the media drive "${stall.label}" is not answering (${sinceText(stall.since)}) — ` + `no new ${what} starts until it answers again.`, ); } else if (stall && holding) { if (t - lastStill >= stillEvery) { lastStill = t; log( `Still holding: the media drive "${stall.label}" has not answered after ${minutesText(t - heldAt)}.`, ); } } else if (!stall && holding) { log(`The media drive "${holding.label}" answers again — resuming.`); } holding = stall; if (stall) maybeReprobe(stall); return stall; }, }; } // The sentence a unit refuses with when its channel's drive is not answering. export function mediaDriveHeldText(stall: Pick): string { return `the media drive "${stall.label}" is not answering (${sinceText(stall.since)})`; }