refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
This commit is contained in:
70
cron-runner.ts
Normal file
70
cron-runner.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.');
|
||||
Reference in New Issue
Block a user