/** * Keeps what n8n says up to date: every five minutes each n8n integration is read (its workflows, and from the latest runs each one's last outcome and failures in the last day) * and the state is stored. Pages and alerts are built from what is stored, so none of them talks to n8n. * * What is raised is an **active workflow whose last run failed** — an automation that has quietly stopped doing its job — and it clears by itself when the workflow next succeeds. */ import { and, eq } from "drizzle-orm"; import { db } from "../db/client.js"; import { integrations, n8nState } from "../db/schema.js"; import { createN8nAdapter, FAILED_STATUSES, type N8nRead, type N8nWorkflow } from "../integrations/n8n/adapter.js"; import { loadIntegrationConfig } from "../integrations/loadIntegration.js"; import { getInternalFlag, setInternalFlag } from "./settingsStore.js"; export const SYNC_INTERVAL_MS = 5 * 60_000; /** After this long without a good read, what is stored is no longer believed. */ export const STALE_AFTER_MS = 20 * 60_000; /** A failure older than this is no longer news (a workflow that ran once, failed, and is never run again). */ export const FAILED_WITHIN_MS = 7 * 86_400_000; const STATUS_FLAG = "n8nSync"; export interface N8nSyncStatus { /** The last read that worked (kept when a later one failed). */ at: string; ok: boolean; error: string | null; } async function readStatuses(): Promise> { try { const raw = await getInternalFlag(STATUS_FLAG); return raw ? (JSON.parse(raw) as Record) : {}; } catch { return {}; } } export async function n8nSyncStatuses(): Promise> { return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v])); } export const isN8nFresh = (status: N8nSyncStatus | undefined, nowMs: number) => !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS; export type StateRow = typeof n8nState.$inferSelect; export const workflowsOf = (row: Pick): N8nWorkflow[] => { try { const v = JSON.parse(row.workflows); return Array.isArray(v) ? (v as N8nWorkflow[]) : []; } catch { return []; } }; /** Writes down one read, replacing the last. */ export async function storeRead(integrationId: number, r: N8nRead, now: Date = new Date()): Promise { const values = { workflows: JSON.stringify(r.workflows), executionsKnown: r.executionsKnown, updatedAt: now.toISOString() }; await db.insert(n8nState).values({ integrationId, ...values }).onConflictDoUpdate({ target: n8nState.integrationId, set: values }); } /** Reads one n8n and stores it. Never throws: how it went is the answer, and what was stored is left alone when it failed. */ export async function syncN8n(integrationId: number, now: Date = new Date(), read?: (config: Record) => Promise): Promise { let status: N8nSyncStatus; try { const loaded = await loadIntegrationConfig(integrationId); if (!loaded || loaded.integration.type !== "n8n") throw new Error("not an n8n integration"); const result = await (read ? read(loaded.config) : createN8nAdapter(loaded.config as never).read(now)); await storeRead(integrationId, result, now); status = { at: now.toISOString(), ok: true, error: null }; } catch (err) { const previous = (await readStatuses())[String(integrationId)]; status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err) }; console.error(`[n8n] couldn't read integration ${integrationId}:`, status.error); } try { const all = await readStatuses(); all[String(integrationId)] = status; await setInternalFlag(STATUS_FLAG, JSON.stringify(all)); } catch (err) { console.error("[n8n] couldn't record the result:", err); } return status; } export async function syncAllN8n(now: Date = new Date()): Promise> { const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "n8n"), eq(integrations.enabled, true))); const out: Record = {}; for (const r of rows) out[r.id] = await syncN8n(r.id, now); return out; } export async function forgetN8n(integrationId: number): Promise { const all = await readStatuses(); if (String(integrationId) in all) { delete all[String(integrationId)]; await setInternalFlag(STATUS_FLAG, JSON.stringify(all)); } } // ─── What is wrong ────────────────────────────────────────────────────────── export interface N8nProblem { key: string; message: string; severity: "warning" | "critical"; } const ago = (iso: string, now: Date) => { const min = Math.max(0, Math.round((now.getTime() - Date.parse(iso)) / 60_000)); return min < 90 ? `${min} min ago` : min < 2 * 1440 ? `${Math.round(min / 60)} h ago` : `${Math.round(min / 1440)} days ago`; }; /** Pure: the active workflows whose last run failed, within the last week. A workflow that is switched off is a person's choice and says nothing. */ export function n8nProblems(id: number, name: string, s: Pick, now: Date = new Date()): N8nProblem[] { if (!s.executionsKnown) return []; const out: N8nProblem[] = []; for (const w of workflowsOf(s)) { if (!w.active || !w.lastStatus || !w.lastAt || !FAILED_STATUSES.has(w.lastStatus)) continue; if (now.getTime() - Date.parse(w.lastAt) > FAILED_WITHIN_MS) continue; const many = w.failed24h > 1 ? `, and ${w.failed24h} runs failed in the last day` : ""; out.push({ key: `n8n-failed:${id}:${w.id}`, severity: "warning", message: `${name}: the workflow “${w.name}” failed on its last run (${ago(w.lastAt, now)}${w.lastStatus === "crashed" ? ", crashed" : ""})${many}` }); } return out; } let timer: ReturnType | null = null; export function initN8nSync(): void { if (timer) return; const pass = () => void syncAllN8n().catch((err) => console.error("[n8n] pass failed:", err)); timer = setInterval(pass, SYNC_INTERVAL_MS); setTimeout(pass, 25_000); console.log(`[n8n] reading n8n every ${SYNC_INTERVAL_MS / 60000} minutes`); }