import { fork } from "node:child_process"; import { fileURLToPath } from "node:url"; const script = fileURLToPath(new URL("./import.js", import.meta.url)); const HOUR = 3600000; /** * Checks for new product data shortly after start-up and every six hours. * The import itself decides whether anything needs downloading. */ export function scheduleProductImports(dataDir, { firstDelay = 60000 } = {}) { let child = null; const run = () => { if (child) return; child = fork(script, [dataDir], { execArgv: ["--max-old-space-size=1024"], stdio: "inherit", }); child.on("exit", () => (child = null)); }; const timers = [setTimeout(run, firstDelay), setInterval(run, 6 * HOUR)]; for (const timer of timers) timer.unref(); return () => { for (const timer of timers) clearInterval(timer); child?.kill(); }; }