Files
PAGIVA/scripts/cron-runner.mjs
Loma Bot Ops 4a673ebb28 Support sub-path deploy via NEXT_PUBLIC_BASE_PATH (loma.chat/pagiva)
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies,
OAuth redirects, manifest, service worker and cron runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:48:42 +07:00

38 lines
1.4 KiB
JavaScript

// In-container replacement for Vercel Cron (see vercel.json's old schedule).
// Runs alongside `node server.js` in the Docker image — see Dockerfile.
import "dotenv/config";
const PORT = process.env.PORT || 3000;
const BASE_PATH = (process.env.NEXT_PUBLIC_BASE_PATH ?? "").replace(/\/+$/, "");
const BASE = `http://127.0.0.1:${PORT}${BASE_PATH}`;
const SECRET = process.env.CRON_SECRET;
if (!SECRET) {
console.error("[cron] CRON_SECRET not set — scheduler exiting");
process.exit(1);
}
// Schedules match the original vercel.json (UTC — containers default to UTC).
const JOBS = [
{ path: "/api/cron/insights-snapshot", match: (d) => d.getUTCHours() === 2 && d.getUTCMinutes() === 0 },
{ path: "/api/cron/reconcile-scheduled", match: (d) => d.getUTCHours() === 3 && d.getUTCMinutes() === 0 },
{ path: "/api/cron/token-health", match: (d) => d.getUTCHours() === 3 && d.getUTCMinutes() === 30 },
{ path: "/api/cron/ig-publisher", match: (d) => d.getUTCMinutes() % 10 === 0 },
];
async function run(jobPath) {
try {
const res = await fetch(`${BASE}${jobPath}`, { headers: { authorization: `Bearer ${SECRET}` } });
console.log(`[cron] ${jobPath} -> ${res.status}`);
} catch (e) {
console.error(`[cron] ${jobPath} failed:`, e?.message ?? e);
}
}
setInterval(() => {
const now = new Date();
for (const job of JOBS) if (job.match(now)) run(job.path);
}, 60_000);
console.log("[cron] scheduler started");