import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getRegistry, onJobEnd } from "yt-dlp-transcript-common/jobs/registry"; import { platformCooldownRemainingMs } from "yt-dlp-transcript-common/jobs/downloadBackoff"; import { assertChannelMediaReachable } from "yt-dlp-transcript-common/lib/channelMedia"; import { armCleanRetriesAtBoot, cancelCleanRetries, type CleanRetryDeps, } from "yt-dlp-transcript-common/controller/cleanRetrySchedule"; // THE EDITOR'S WIRING of a rate-limited clean's off-queue retry // (common/controller/cleanRetrySchedule.ts): how its timer queues the retry // job (cleanAudioRetryAction, on the channel's queue), how a cancel reaches a // job it already queued (the registry's cancel), and the platform cooldown it // re-reads. Not a "use server" module: nothing here is a client-callable // action. export function cleanRetryDeps(): CleanRetryDeps { const paths = getPaths(); return { cooldownRemainingMs: (pf) => platformCooldownRemainingMs(pf, paths), enqueue: async (entry) => { // Lazy: whisperActions imports this module. const { cleanAudioRetryAction } = await import("./whisperActions"); const r = await cleanAudioRetryAction(entry.slug, { attempt: entry.attempt, of: entry.of, notBefore: entry.notBefore, platform: entry.platform, id: entry.id, }); if (!r.ok) return { ok: false, error: r.error }; // Nobody reads this job's stream; it is followed on /jobs. void r.stream.cancel().catch(() => {}); return { ok: true, jobId: r.jobId }; }, cancelJob: (jobId) => getRegistry().cancel(jobId), // A relocation in flight or a drive away when its time comes: the retry // waits and tries again (cleanRetrySchedule: HOLD_PAUSE_MS, MAX_HOLDS). mediaHold: async (slug) => { try { await assertChannelMediaReachable(paths, slug); return null; } catch (err) { return (err as Error).message; } }, log: (m) => console.log(m), }; } // A RETRY JOB CANCELLED WHILE QUEUED is a person's decision (a /jobs Cancel, // Drain all): its record goes too, or the next boot would re-arm it. Not at // shutdown — the reaper's cancels are nobody's, and the boot re-arms those. // (A retry that ran removes its own record when it ends.) Once per process. declare global { // eslint-disable-next-line no-var var __yttCleanRetryEndListener__: boolean | undefined; } if (!globalThis.__yttCleanRetryEndListener__) { globalThis.__yttCleanRetryEndListener__ = true; onJobEnd((job) => { if (job.kind !== "clean-audio-retry" || job.status !== "cancelled") return; if (getRegistry().isShuttingDown()) return; void cancelCleanRetries(getPaths(), { id: job.id }, cleanRetryDeps()).catch(() => {}); }); } // The boot (instrumentation.ts, below the idle gate): arm every scheduled // retry again from its record. export async function armCleanRetriesOnBoot(): Promise { const out = await armCleanRetriesAtBoot(getPaths(), cleanRetryDeps()); if (out.armed.length || out.dropped.length) { console.log( `[boot] clean retries: ${out.armed.length} re-armed from their schedule` + (out.dropped.length ? `, ${out.dropped.length} dropped (interrupted twice)` : ""), ); } }