import { NextRequest, NextResponse } from "next/server"; import { isExtAuthorized } from "@/lib/render-pipeline/ext-auth"; import { toErrorResponse } from "@/lib/render-pipeline/errors"; import { recordHeartbeat } from "@/lib/render-pipeline/machines"; // ── POST /api/ext/workers/{machineId}/heartbeat (E7) ───────────────────────── // // Heartbeat + the server→worker command channel: cancellation piggybacks on // the response (`commands: [{ type: "CANCEL_JOB", jobId }]`) so no server→ // worker connection is ever needed. export async function POST( req: NextRequest, { params }: { params: Promise<{ machineId: string }> } ) { if (!isExtAuthorized(req)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const { machineId } = await params; let body: { cpuPercent?: number; memPercent?: number; diskFreeGb?: number; currentJobId?: string | null } = {}; try { body = await req.json(); } catch { // heartbeat with empty body is fine } try { const result = await recordHeartbeat(machineId, body); return NextResponse.json(result); } catch (err) { const { body: errBody, status } = toErrorResponse(err); return NextResponse.json(errBody, { status }); } }