// Bama crawl trigger — fires the server's /api/crawl every 15 min via PM2 // cron_restart. The web app itself never crawls on page load. const fs = require("fs"); const path = require("path"); // Load .env.production (KEY=VALUE lines) so the cron process has the real // secret + port. const envFile = path.join(__dirname, "..", ".env.production"); try { for (const line of fs.readFileSync(envFile, "utf8").split("\n")) { const m = line.match(/^([A-Z0-9_]+)=(.*)$/); if (m && !process.env[m[1]]) process.env[m[1]] = m[2]; } } catch (e) {} const SECRET = process.env.CRON_SECRET || "bama-cron"; const PORT = process.env.PORT || "3000"; let run = async () => { const res = await fetch(`http://127.0.0.1:${PORT}/api/crawl`, { method: "POST", headers: { "x-bama-cron": SECRET }, signal: AbortSignal.timeout(280000), }); const j = await res.json().catch(() => ({})); const c = (j && j.crawl) || {}; console.log( `[bama-crawl] ${res.status} added=${c.added} named=${c.named} fallback=${c.usedFallback ? "yes" : "no"} gated=${c.gatedOut} unverified=${c.skippedUnverified} imgs=${c.withImage} articles=${c.articles ? c.articles.ok + "/" + c.articles.fetched : "?"} ${c.errors && c.errors.length ? "errs=" + c.errors.length : ""}` ); process.exit(res.ok ? 0 : 1); }; run().catch((e) => { console.error("[bama-crawl] FAILED", e && e.message); process.exit(1); });