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) <noreply@anthropic.com>
This commit is contained in:
Claude VM
2026-04-10 15:29:01 +03:00
parent 377b88c48d
commit ddf27d9b17
3 changed files with 19 additions and 10 deletions
+4 -2
View File
@@ -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);
+6 -3
View File
@@ -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.",
});
}
@@ -9,10 +9,10 @@ const WEBHOOK_URL = process.env.N8N_WEBHOOK_URL || "";
export async function firePmtilesRebuild(
event: string,
metadata?: Record<string, unknown>,
): Promise<boolean> {
): 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 };
}
}