mirror of
https://github.com/badlogic/heissepreise.git
synced 2024-07-01 21:05:49 +02:00
Fix scheduling of data fetch, do it daily at 7am instead of every 24h after launching the server.
This commit is contained in:
parent
8c202de9a6
commit
1fec42bfe1
26
index.js
26
index.js
|
@ -10,6 +10,28 @@ function copyItemsToSite(dataDir) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function scheduleFunction(hour, minute, second, func) {
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
const scheduledTime = new Date();
|
||||||
|
scheduledTime.setHours(hour);
|
||||||
|
scheduledTime.setMinutes(minute);
|
||||||
|
scheduledTime.setSeconds(second);
|
||||||
|
|
||||||
|
if (now > scheduledTime) {
|
||||||
|
scheduledTime.setDate(scheduledTime.getDate() + 1);
|
||||||
|
}
|
||||||
|
const delay = scheduledTime.getTime() - now.getTime();
|
||||||
|
|
||||||
|
console.log("Scheduling next function call: " + scheduledTime.toString());
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
await func();
|
||||||
|
scheduleFunction(hour, minute, second, func);
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const dataDir = 'data';
|
const dataDir = 'data';
|
||||||
|
|
||||||
|
@ -26,10 +48,10 @@ function copyItemsToSite(dataDir) {
|
||||||
await analysis.updateData(dataDir)
|
await analysis.updateData(dataDir)
|
||||||
copyItemsToSite(dataDir);
|
copyItemsToSite(dataDir);
|
||||||
}
|
}
|
||||||
setInterval(async () => {
|
scheduleFunction(7, 0, 0, async () => {
|
||||||
items = await analysis.updateData(dataDir)
|
items = await analysis.updateData(dataDir)
|
||||||
copyItemsToSite(dataDir);
|
copyItemsToSite(dataDir);
|
||||||
}, 1000 * 60 * 60 * 24);
|
});
|
||||||
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
|
|
Loading…
Reference in New Issue
Block a user