1
0
Files
jecnarozvrh/cron-runner.ts
jzitnik-dev ae17dc241a
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
refactor: Rewrite to typescript
2026-02-11 08:20:56 +01:00

71 lines
2.2 KiB
TypeScript

/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
import cron from 'node-cron';
import { exec } from 'child_process';
import { scheduleRules, toMinutes, ScheduleRule } from './scheduleRules.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function runScraper() {
console.log(`Running scraper at ${new Date().toLocaleString()}...`);
let command;
if (process.env.NODE_ENV === 'development') {
const scriptPath = path.resolve(__dirname, 'scrape/scraper.ts');
command = `npx tsx "${scriptPath}"`;
} else {
const scriptPath = path.resolve(__dirname, 'scrape/scraper.js');
command = `node "${scriptPath}"`;
}
exec(command, (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: ScheduleRule[]) {
rules.forEach(rule => {
const startMin = toMinutes(rule.start);
const endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end);
const times: { h: number; m: number }[] = [];
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.');