feat(sync): auto-trigger PMTiles rebuild after sync + fix progress display

- Add pmtiles-webhook.ts shared helper for triggering PMTiles rebuild
- sync-county: trigger rebuild when new features synced, pass jobId to
  syncLayer for sub-progress, update % after UAT completion (not before)
- sync-all-counties: same progress fix + rebuild trigger at end
- geoportal monitor: use shared helper instead of raw fetch
- weekend-deep-sync + auto-refresh: consolidate webhook code via helper
- docker-compose: default N8N_WEBHOOK_URL to pmtiles-webhook on satra:9876

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude VM
2026-04-09 14:59:18 +03:00
parent b356e70148
commit 377b88c48d
8 changed files with 120 additions and 73 deletions
@@ -159,26 +159,9 @@ async function runAutoRefresh() {
g.__autoRefreshLastRun = today;
console.log(`[auto-refresh] Finalizat: ${processed}/${uats.length} UATs, ${errors} erori.`);
// Trigger PMTiles rebuild via N8N webhook
const webhookUrl = process.env.N8N_WEBHOOK_URL;
if (webhookUrl) {
try {
await fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
event: "auto-refresh-complete",
uatCount: processed,
errors,
timestamp: new Date().toISOString(),
}),
});
console.log("[auto-refresh] Webhook PMTiles rebuild trimis la N8N.");
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.warn(`[auto-refresh] Webhook N8N esuat: ${msg}`);
}
}
// Trigger PMTiles rebuild
const { firePmtilesRebuild } = await import("./pmtiles-webhook");
await firePmtilesRebuild("auto-refresh-complete", { uatCount: processed, errors });
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error(`[auto-refresh] Eroare generala: ${msg}`);
@@ -0,0 +1,39 @@
/**
* Shared helper — triggers PMTiles rebuild via webhook after sync operations.
* The webhook server (pmtiles-webhook systemd service on satra) runs
* `docker run architools-tippecanoe` to regenerate overview tiles.
*/
const WEBHOOK_URL = process.env.N8N_WEBHOOK_URL || "";
export async function firePmtilesRebuild(
event: string,
metadata?: Record<string, unknown>,
): Promise<boolean> {
if (!WEBHOOK_URL) {
console.warn("[pmtiles-webhook] N8N_WEBHOOK_URL not configured — skipping rebuild trigger");
return false;
}
try {
const res = await fetch(WEBHOOK_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
event,
timestamp: new Date().toISOString(),
...metadata,
}),
});
if (res.ok) {
console.log(`[pmtiles-webhook] Rebuild triggered (event: ${event}, HTTP ${res.status})`);
return true;
}
console.warn(`[pmtiles-webhook] Webhook returned HTTP ${res.status}`);
return false;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.warn(`[pmtiles-webhook] Failed: ${msg}`);
return false;
}
}
@@ -686,26 +686,10 @@ export async function triggerForceSync(options?: {
}
/* ------------------------------------------------------------------ */
/* N8N Webhook — trigger PMTiles rebuild after sync cycle */
/* PMTiles Webhook — trigger rebuild after sync cycle */
/* ------------------------------------------------------------------ */
async function fireSyncWebhook(cycle: number): Promise<void> {
const url = process.env.N8N_WEBHOOK_URL;
if (!url) return;
try {
await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
event: "weekend-sync-cycle-complete",
cycle,
timestamp: new Date().toISOString(),
}),
});
console.log(`[weekend-sync] Webhook trimis la N8N (ciclu #${cycle})`);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.warn(`[weekend-sync] Webhook N8N esuat: ${msg}`);
}
const { firePmtilesRebuild } = await import("./pmtiles-webhook");
await firePmtilesRebuild("weekend-sync-cycle-complete", { cycle });
}