// `wayback.auto` IN THE DOWNLOAD LANE (release 37 S2): a Wayback pages channel // that opts in has its new scan rows imported by the lane — one row per unit, // one `import-page` job per row (controller/waybackImport.ts), on archive.org's // queue, waited on like the metadata scan's child job, at the lane's pace for // the `archive.org` platform (one unit at a time; the gap and back-off its // outcomes earn). Default off: no unattended crawling of a host unless the // channel opts in. // // What a unit takes is `pendingAutoImports`: rows first listed after the // channel's first pass that no record holds yet, oldest first. A channel's list // is read at most once a minute (the scan file and the channel's posts), and a // row the runner has tried is not offered again in its lifetime — a failed // import waits for the operator or a restart, never a re-hammer. import type { Paths } from "../lib/paths"; import { onJobEnd, getRegistry, type JobStatus } from "../jobs/registry"; import { waybackConfigOf } from "../lib/waybackPages/targets"; import type { CaptureRowView } from "../lib/waybackPages/scanView"; import { pendingAutoImports, startCaptureImport } from "./waybackImport"; export const WAYBACK_AUTO_LEAF_ID = "wayback-auto"; export const WAYBACK_AUTO_UNIT_PREFIX = "wayback-import "; // The platform key a unit holds: one Wayback import at a time in the lane. export const WAYBACK_AUTO_PLATFORM = "archive.org"; export const WAYBACK_AUTO_TTL_MS = 60_000; export function waybackAutoOn(config: unknown): boolean { return waybackConfigOf(config)?.auto === true; } export type WaybackAutoPick = { slug: string; row: CaptureRowView; unitId: string }; export class WaybackAutoPicker { private readonly cache = new Map(); private readonly tried = new Set(); constructor( private readonly deps: { pending: (slug: string) => Promise; now?: () => number; ttlMs?: number; }, ) {} static forPaths(paths: Paths): WaybackAutoPicker { return new WaybackAutoPicker({ pending: (slug) => pendingAutoImports(paths, slug) }); } private async rowsOf(slug: string): Promise { const now = (this.deps.now ?? Date.now)(); const hit = this.cache.get(slug); if (hit && now - hit.at < (this.deps.ttlMs ?? WAYBACK_AUTO_TTL_MS)) return hit.rows; const rows = await this.deps.pending(slug).catch(() => [] as CaptureRowView[]); this.cache.set(slug, { at: now, rows }); return rows; } // The next row to import: the first opted-in channel (in the lane's order) // with a row not tried yet. None while the platform is skipped (busy, // cooling, held, paced) or, under a holding focus, outside it. async pick( channels: ReadonlyArray<{ slug: string; config: unknown }>, opts: { platformSkip: ReadonlySet; focus?: { slugs: ReadonlySet; holding: boolean } }, ): Promise { if (opts.platformSkip.has(WAYBACK_AUTO_PLATFORM)) return null; for (const c of channels) { if (!waybackAutoOn(c.config)) continue; if (opts.focus?.holding && !opts.focus.slugs.has(c.slug)) continue; for (const row of await this.rowsOf(c.slug)) { const unitId = `${WAYBACK_AUTO_UNIT_PREFIX}${c.slug} ${row.key}`; if (this.tried.has(unitId)) continue; this.tried.add(unitId); return { slug: c.slug, row, unitId }; } } return null; } } // A job's end: its terminal status, or "unknown" for an id the registry does // not hold. export function awaitJobEnd(jobId: string): Promise { const terminal = (s: JobStatus | undefined) => s === "done" || s === "failed" || s === "cancelled"; const now = getRegistry().get(jobId); if (!now) return Promise.resolve("unknown"); if (terminal(now.status)) return Promise.resolve(now.status); return new Promise((resolve) => { const stop = onJobEnd((j) => { if (j.id !== jobId) return; stop(); resolve(j.status); }); // The end may have landed between the read above and the listener. const again = getRegistry().get(jobId); if (again && terminal(again.status)) { stop(); resolve(again.status); } }); } // ONE UNIT: the row's import-page job, waited on. "transcribed" (the lane's // word for done), "failed", or "skipped" (refused before a job, or cancelled). export async function launchWaybackAutoImport(args: { pick: WaybackAutoPick; onLog: (line: string) => void; onChildJob?: (jobId: string) => void; }): Promise<{ outcome: "transcribed" | "failed" | "skipped" }> { let jobId: string; try { ({ jobId } = await startCaptureImport(args.pick.slug, args.pick.row, "auto-download")); } catch (e) { args.onLog(`Auto-download: could not queue the Wayback import of ${args.pick.row.replayUrl}: ${(e as Error).message}`); return { outcome: "skipped" }; } args.onChildJob?.(jobId); const end = await awaitJobEnd(jobId); if (end === "done") return { outcome: "transcribed" }; if (end === "failed") { args.onLog(`Auto-download: the Wayback import of ${args.pick.row.replayUrl} failed.`); return { outcome: "failed" }; } return { outcome: "skipped" }; }