From ddf27d9b17566acff1651794b16877b70cb306f8 Mon Sep 17 00:00:00 2001 From: Claude VM Date: Fri, 10 Apr 2026 15:29:01 +0300 Subject: [PATCH] fix(webhook): treat HTTP 409 (rebuild already running) as success, not error The pmtiles-webhook returns 409 when a rebuild is already in progress. Previously this was treated as a failure, showing 'Webhook PMTiles indisponibil' error to the user. Now 409 is handled as a valid state with appropriate messaging. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/app/(modules)/monitor/page.tsx | 6 ++++-- src/app/api/geoportal/monitor/route.ts | 9 ++++++--- .../parcel-sync/services/pmtiles-webhook.ts | 14 +++++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/app/(modules)/monitor/page.tsx b/src/app/(modules)/monitor/page.tsx index ed7d4f1..d6cbe5d 100644 --- a/src/app/(modules)/monitor/page.tsx +++ b/src/app/(modules)/monitor/page.tsx @@ -161,13 +161,15 @@ export default function MonitorPage() { headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "rebuild" }), }); - const result = await res.json() as { ok?: boolean; error?: string; previousPmtiles?: { lastModified: string }; webhookStatus?: number }; + const result = await res.json() as { ok?: boolean; error?: string; alreadyRunning?: boolean; previousPmtiles?: { lastModified: string } }; if (!result.ok) { addLog("error", result.error ?? "Eroare necunoscuta"); setActionLoading(""); return; } - addLog("ok", `Webhook trimis (HTTP ${result.webhookStatus}). Rebuild in curs...`); + addLog("ok", result.alreadyRunning + ? "Rebuild deja in curs. Se monitorizeaza..." + : "Webhook trimis. Rebuild pornit..."); rebuildPrevRef.current = result.previousPmtiles?.lastModified ?? null; // Poll every 15s to check if PMTiles was updated if (pollRef.current) clearInterval(pollRef.current); diff --git a/src/app/api/geoportal/monitor/route.ts b/src/app/api/geoportal/monitor/route.ts index 508fcd3..15fbae0 100644 --- a/src/app/api/geoportal/monitor/route.ts +++ b/src/app/api/geoportal/monitor/route.ts @@ -162,8 +162,8 @@ export async function POST(request: NextRequest) { if (action === "rebuild") { // Get current PMTiles state before rebuild const before = await getPmtilesInfo(); - const ok = await firePmtilesRebuild("manual-rebuild"); - if (!ok) { + const result = await firePmtilesRebuild("manual-rebuild"); + if (!result.ok) { return NextResponse.json( { error: "Webhook PMTiles indisponibil — verifica N8N_WEBHOOK_URL si serviciul pmtiles-webhook" }, { status: 500 }, @@ -172,8 +172,11 @@ export async function POST(request: NextRequest) { return NextResponse.json({ ok: true, action: "rebuild", + alreadyRunning: result.alreadyRunning ?? false, previousPmtiles: before, - message: "Rebuild PMTiles pornit. Dureaza ~8 min. Urmareste PMTiles last-modified.", + message: result.alreadyRunning + ? "Rebuild PMTiles deja in curs. Urmareste PMTiles last-modified." + : "Rebuild PMTiles pornit. Dureaza ~8 min. Urmareste PMTiles last-modified.", }); } diff --git a/src/modules/parcel-sync/services/pmtiles-webhook.ts b/src/modules/parcel-sync/services/pmtiles-webhook.ts index f0a1bc1..42071b8 100644 --- a/src/modules/parcel-sync/services/pmtiles-webhook.ts +++ b/src/modules/parcel-sync/services/pmtiles-webhook.ts @@ -9,10 +9,10 @@ const WEBHOOK_URL = process.env.N8N_WEBHOOK_URL || ""; export async function firePmtilesRebuild( event: string, metadata?: Record, -): Promise { +): Promise<{ ok: boolean; alreadyRunning?: boolean }> { if (!WEBHOOK_URL) { console.warn("[pmtiles-webhook] N8N_WEBHOOK_URL not configured — skipping rebuild trigger"); - return false; + return { ok: false }; } try { @@ -27,13 +27,17 @@ export async function firePmtilesRebuild( }); if (res.ok) { console.log(`[pmtiles-webhook] Rebuild triggered (event: ${event}, HTTP ${res.status})`); - return true; + return { ok: true }; + } + if (res.status === 409) { + console.log(`[pmtiles-webhook] Rebuild already running (event: ${event})`); + return { ok: true, alreadyRunning: true }; } console.warn(`[pmtiles-webhook] Webhook returned HTTP ${res.status}`); - return false; + return { ok: false }; } catch (err) { const msg = err instanceof Error ? err.message : String(err); console.warn(`[pmtiles-webhook] Failed: ${msg}`); - return false; + return { ok: false }; } }