"use server"; // THE COOKIE REFRESH ALERT'S BUTTONS (plans/cookie-refresh-alerts.md), shared // by the dashboard banner and `/api/ops/service-cookies`: // // Recheck ONE request through the service's own path, as a one-step job // (kind `recheck-service-cookies`, its own queue) that the click // waits for — the alert clears when it comes back clean. // Dismiss the operator's word: the alert is cleared as if the service // had answered. // Re-run every job the alert stopped (failed or cancelled), retried // from its replay descriptor ahead of the queue, as /jobs' Retry. // No revalidatePath here: the banner refetches its own view after each click // (CookieAlertsPanel's onChange), and the page is not re-rendered under a job // that started inside this request. import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { runManagedFunction } from "yt-dlp-transcript-common/jobs/streamCommand"; import { isServiceKey, serviceLabel } from "yt-dlp-transcript-common/lib/serviceCookies"; import { dismissServiceCookieAlert, markServiceCookieRerun, } from "yt-dlp-transcript-common/lib/serviceCookies-server"; import { recheckServiceCookies, type RecheckOutcome, } from "yt-dlp-transcript-common/controller/recheckServiceCookies"; import { retryJobAction } from "../jobs/actions"; import { loadCookieAlerts } from "../lib/cookieAlerts"; import { recheckOutcome, withCookieAction } from "../lib/cookieRefreshGuard"; function refuse(service: string): string | null { return isServiceKey(service) ? null : `Not a service: ${service}`; } export type RecheckActionResult = | { ok: true; jobId: string; result: RecheckOutcome["result"]; message: string } // busy: refused because a recheck (or another press) for the service is running. | { ok: false; error: string; jobId?: string; busy?: true }; // Through the one-press-per-service guard (lib/cookieRefreshGuard.ts): a // second Recheck while one runs (or the timer's) is refused, not queued. export async function recheckServiceCookiesAction(service: string): Promise { const bad = refuse(service); if (bad) return { ok: false, error: bad }; return withCookieAction(service, "recheck", () => recheckNow(service), recheckOutcome); } async function recheckNow(service: string): Promise { const paths = getPaths(); let outcome: RecheckOutcome | null = null; const res = await runManagedFunction({ kind: "recheck-service-cookies", queueKey: `service-cookies:${service}`, paths, fn: async (onLog, signal) => { outcome = await recheckServiceCookies({ paths, service, onLog, signal }); }, }); if (!res.ok) return { ok: false, error: res.error }; void res.stream.cancel(); const done = await res.done; const o = outcome as RecheckOutcome | null; if (done.status !== "done" || !o) { return { ok: false, jobId: res.jobId, error: `The ${serviceLabel(service)} recheck ended ${done.status}.` }; } return { ok: true, jobId: res.jobId, result: o.result, message: o.message }; } export async function dismissServiceCookiesAction( service: string, ): Promise<{ ok: true; message: string } | { ok: false; error: string }> { const bad = refuse(service); if (bad) return { ok: false, error: bad }; const had = await dismissServiceCookieAlert(getPaths(), service); return { ok: true, message: had ? `${serviceLabel(service)}'s cookie alert dismissed.` : `${serviceLabel(service)} had no cookie alert to dismiss.`, }; } export async function rerunServiceStoppedJobsAction( service: string, ): Promise< | { ok: true; jobIds: string[]; skipped: { id: string; error: string }[]; message: string } | { ok: false; error: string } > { const bad = refuse(service); if (bad) return { ok: false, error: bad }; const paths = getPaths(); const alert = (await loadCookieAlerts(paths)).alerts.find((a) => a.service === service); if (!alert) return { ok: false, error: `${serviceLabel(service)} has no cookie alert.` }; if (alert.state === "stale") { return { ok: false, error: `${serviceLabel(service)}'s cookies still look stale — Recheck first.` }; } const jobIds: string[] = []; const skipped: { id: string; error: string }[] = []; for (const j of alert.stoppedJobs) { const res = await retryJobAction(j.id); if (res.ok) { void res.stream.cancel(); jobIds.push(res.jobId); } else { skipped.push({ id: j.id, error: res.error }); } } await markServiceCookieRerun(paths, service, jobIds); return { ok: true, jobIds, skipped, message: `Re-ran ${jobIds.length} job${jobIds.length === 1 ? "" : "s"} ${serviceLabel(service)}'s cookies stopped` + (skipped.length ? `; ${skipped.length} could not be (${skipped.map((s) => `${s.id}: ${s.error}`).join("; ")})` : "") + ".", }; }