import cron from 'node-cron'; import { exec } from 'child_process'; import { scheduleRules, toMinutes } from './scheduleRules.js'; function runScraper() { console.log(`Running scraper at ${new Date().toLocaleString()}...`); exec('node scrape/scraper.js', (error, stdout, stderr) => { if (error) { console.error(`Scraper error: ${error.message}`); return; } if (stderr) console.error(`Scraper stderr: ${stderr}`); if (stdout) console.log(`Scraper output:\n${stdout}`); }); } function createSchedules(rules) { rules.forEach(rule => { const startMin = toMinutes(rule.start); const endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end); const times = []; const adjustedEnd = endMin <= startMin ? endMin + 1440 : endMin; for (let t = startMin; t < adjustedEnd; t += rule.interval) { const h = Math.floor(t % 1440 / 60); const m = t % 60; times.push({ h, m }); } times.forEach(({ h, m }) => { const cronExpr = `${m} ${h} * * *`; cron.schedule(cronExpr, runScraper); console.log(`Scheduled: ${cronExpr}`); }); }); } // Run immediately at start runScraper(); createSchedules(scheduleRules); console.log('Cron scheduler started with custom intervals.');