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 cron = require('node-cron');
|
||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
|
const { scheduleRules, toMinutes } = require('./scheduleRules');
|
||||||
|
|
||||||
function runScraper() {
|
function runScraper() {
|
||||||
console.log('Running scraper...');
|
console.log(`Running scraper at ${new Date().toLocaleString()}...`);
|
||||||
exec('node scrape/scraper.js', (error, stdout, stderr) => {
|
exec('node scrape/scraper.js', (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Scraper error: ${error.message}`);
|
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
|
// Run immediately at start
|
||||||
runScraper();
|
runScraper();
|
||||||
|
|
||||||
// Schedule to run every 10 minutes
|
createSchedules(scheduleRules);
|
||||||
cron.schedule('*/10 * * * *', runScraper);
|
|
||||||
|
|
||||||
console.log('Cron scheduler started. Scraper will run every 10 minutes.');
|
console.log('Cron scheduler started with custom intervals.');
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "tablescraper",
|
"name": "jecnarozvrh",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "tablescraper",
|
"name": "jecnarozvrh",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "tablescraper",
|
"name": "jecnarozvrh",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
|
||||||
"start": "concurrently \"node server.js\" \"node cron-runner.js\""
|
"start": "concurrently \"node server.js\" \"node cron-runner.js\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
43
scheduleRules.js
Normal file
43
scheduleRules.js
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// Rules: start and end in 24h format, interval in minutes
|
||||||
|
const scheduleRules = [
|
||||||
|
{ start: "0:00", end: "3:00", interval: 180 },
|
||||||
|
{ start: "3:00", end: "4:00", interval: 60 },
|
||||||
|
{ start: "5:00", end: "6:00", interval: 30 },
|
||||||
|
{ start: "6:00", end: "7:30", interval: 15 },
|
||||||
|
{ start: "7:30", end: "16:00", interval: 5 },
|
||||||
|
{ start: "16:00", end: "19:00", interval: 60 },
|
||||||
|
{ start: "19:00", end: "0:00", interval: 180 }
|
||||||
|
];
|
||||||
|
|
||||||
|
function toMinutes(timeStr) {
|
||||||
|
const [h, m] = timeStr.split(":").map(Number);
|
||||||
|
return h * 60 + (m || 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurrentInterval(date = new Date()) {
|
||||||
|
const nowMinutes = date.getHours() * 60 + date.getMinutes();
|
||||||
|
|
||||||
|
for (const rule of scheduleRules) {
|
||||||
|
let startMin = toMinutes(rule.start);
|
||||||
|
let endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end);
|
||||||
|
|
||||||
|
// Handle wrap-around midnight
|
||||||
|
if (endMin <= startMin) {
|
||||||
|
if (nowMinutes >= startMin || nowMinutes < endMin) {
|
||||||
|
return rule.interval;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (nowMinutes >= startMin && nowMinutes < endMin) {
|
||||||
|
return rule.interval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
scheduleRules,
|
||||||
|
getCurrentInterval,
|
||||||
|
toMinutes
|
||||||
|
};
|
||||||
12
server.js
12
server.js
@@ -2,11 +2,19 @@ const express = require("express");
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const app = express();
|
const app = express();
|
||||||
const fs = require("fs/promises");
|
const fs = require("fs/promises");
|
||||||
|
const { getCurrentInterval } = require("./scheduleRules");
|
||||||
|
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
app.get('/', (_, res) => {
|
app.get('/', async (_, res) => {
|
||||||
res.sendFile(path.join(__dirname, "db", "current.json"));
|
const dataStr = fs.readFile(path.join(__dirname, "db", "current.json"));
|
||||||
|
const data = JSON.parse(dataStr);
|
||||||
|
|
||||||
|
data["status"] = {
|
||||||
|
currentUpdateSchedule: getCurrentInterval(),
|
||||||
|
};
|
||||||
|
|
||||||
|
res.json(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/status", async (_, res) => {
|
app.get("/status", async (_, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user