api / src /instrumentation.ts
lljz66's picture
Put src/instrumentation.ts
3d27ecd verified
Raw History Blame Contribute Delete
1.01 kB
// Background automation: keeps the job index fresh and runs alert matching +
// autopilot for every onboarded user without any manual action.
export async function register() {
if (process.env.NEXT_RUNTIME !== "nodejs") return;
const g = globalThis as typeof globalThis & { __hfScheduler?: boolean };
if (g.__hfScheduler) return;
g.__hfScheduler = true;
const { runPipeline } = await import("@/lib/automation");
const { lastSyncAt } = await import("@/lib/sync");
const tick = async () => {
try {
const last = await lastSyncAt();
const needsSync = !last || Date.now() - last.getTime() > 6 * 3600_000;
const r = await runPipeline({ sync: needsSync });
console.log(
"[huntflow] pipeline ok · synced=" + needsSync + " users=" + r.users + " notifications=" + r.notifications + " queued=" + r.queued,
);
} catch (e) {
console.error("[huntflow] pipeline failed", e);
}
};
setTimeout(tick, 15_000);
setInterval(tick, 30 * 60_000);
}