File size: 1,005 Bytes
3d27ecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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);
}