import { NextResponse } from "next/server"; import { buildWorkersPayload } from "../../../workers/buildWorkers"; import { disableWorkerAction, drainWorkerAction, enableWorkerAction, } from "../../../workers/actions"; import { oneOf, ops, opsFail, reqStringArray } from "../_lib"; import { readRoute, redactSecrets } from "../_read"; export const dynamic = "force-dynamic"; // GET /api/ops/workers // // The /workers page's live payload (`buildWorkersPayload`, the same one // /api/workers polls): every configured transcription worker — enabled or not, // busy or idle, what it is running — and whether the pool is paused. Behind the // token, with any remote worker's credential redacted. export async function GET(request: Request) { return readRoute(request, [], async () => ({ workers: redactSecrets(buildWorkersPayload()), })); } // POST { op: "enable" | "disable" | "drain", ids: [, …] } // // The /workers row's switch and its Drain, per id (release 19, A4): enable // and disable are the switch — the pool's LIVE state, as the page sets it: a // restart brings back the launch default (/workers' "Set as default"); drain // stops giving the worker new transcriptions and lets the one in flight // finish. An unknown id is named and does not stop the others; the answer is // `ok: false` (400) when any id was refused, with every id's result. const OPS = ["enable", "disable", "drain"] as const; export async function POST(request: Request) { return ops(request, ["op", "ids"], async (body) => { const op = oneOf(body, "op", OPS); const ids = [...new Set(reqStringArray(body, "ids"))]; const run = { enable: enableWorkerAction, disable: disableWorkerAction, drain: drainWorkerAction }[op]; const results: { id: string; ok: boolean; error?: string }[] = []; for (const id of ids) results.push({ id, ...(await run(id)) }); const failed = results.filter((r) => !r.ok); if (failed.length) { return opsFail(failed.map((r) => r.error ?? r.id).join("; "), 400, { op, results }); } return NextResponse.json({ ok: true, op, results }); }); }