import { NextRequest, NextResponse } from "next/server"; import { isExtAuthorized } from "@/lib/render-pipeline/ext-auth"; import { toErrorResponse } from "@/lib/render-pipeline/errors"; import { claimNextJob } from "@/lib/render-pipeline/jobs"; // ── POST /api/ext/render/jobs/claim (E8) ───────────────────────────────────── // // Atomically claim the next queued job (FOR UPDATE SKIP LOCKED). Enforces // machine availability windows / Render Now / urgent priority server-side // (§7.10). 204 when nothing is claimable. export async function POST(req: NextRequest) { if (!isExtAuthorized(req)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } let body: { machineId?: string; types?: string[] }; try { body = await req.json(); } catch { return NextResponse.json({ error: "Invalid JSON" }, { status: 400 }); } if (!body.machineId) { return NextResponse.json({ error: "machineId is required" }, { status: 400 }); } try { const result = await claimNextJob(body.machineId, body.types); if (!result) return new NextResponse(null, { status: 204 }); return NextResponse.json(result); } catch (err) { const { body: errBody, status } = toErrorResponse(err); return NextResponse.json(errBody, { status }); } }