feat: Custom schedule rules
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
const cron = require('node-cron');
|
||||
const { exec } = require('child_process');
|
||||
const { scheduleRules, toMinutes } = require('./scheduleRules');
|
||||
|
||||
function runScraper() {
|
||||
console.log('Running scraper...');
|
||||
console.log(`Running scraper at ${new Date().toLocaleString()}...`);
|
||||
exec('node scrape/scraper.js', (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`Scraper error: ${error.message}`);
|
||||
@@ -13,10 +14,30 @@ function runScraper() {
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// Schedule to run every 10 minutes
|
||||
cron.schedule('*/10 * * * *', runScraper);
|
||||
createSchedules(scheduleRules);
|
||||
|
||||
console.log('Cron scheduler started. Scraper will run every 10 minutes.');
|
||||
console.log('Cron scheduler started with custom intervals.');
|
||||
|
||||
Reference in New Issue
Block a user