Put src/instrumentation.ts
Browse files- src/instrumentation.ts +27 -0
src/instrumentation.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Background automation: keeps the job index fresh and runs alert matching +
|
| 2 |
+
// autopilot for every onboarded user without any manual action.
|
| 3 |
+
export async function register() {
|
| 4 |
+
if (process.env.NEXT_RUNTIME !== "nodejs") return;
|
| 5 |
+
const g = globalThis as typeof globalThis & { __hfScheduler?: boolean };
|
| 6 |
+
if (g.__hfScheduler) return;
|
| 7 |
+
g.__hfScheduler = true;
|
| 8 |
+
|
| 9 |
+
const { runPipeline } = await import("@/lib/automation");
|
| 10 |
+
const { lastSyncAt } = await import("@/lib/sync");
|
| 11 |
+
|
| 12 |
+
const tick = async () => {
|
| 13 |
+
try {
|
| 14 |
+
const last = await lastSyncAt();
|
| 15 |
+
const needsSync = !last || Date.now() - last.getTime() > 6 * 3600_000;
|
| 16 |
+
const r = await runPipeline({ sync: needsSync });
|
| 17 |
+
console.log(
|
| 18 |
+
"[huntflow] pipeline ok 路 synced=" + needsSync + " users=" + r.users + " notifications=" + r.notifications + " queued=" + r.queued,
|
| 19 |
+
);
|
| 20 |
+
} catch (e) {
|
| 21 |
+
console.error("[huntflow] pipeline failed", e);
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
setTimeout(tick, 15_000);
|
| 26 |
+
setInterval(tick, 30 * 60_000);
|
| 27 |
+
}
|