From 5f74d40fa736745651514853afdca3ed44e5ae74 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 3 Nov 2022 17:15:40 +0200 Subject: Made server update foods at monday morning instead of updating them just randomly once a week. --- Functions/dateFuncs.js | 26 ++++++++++++++++++++++++-- food.js | 2 ++ server.js | 14 +++----------- update.js | 2 ++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js index 9b1237a..55a9372 100644 --- a/Functions/dateFuncs.js +++ b/Functions/dateFuncs.js @@ -35,8 +35,30 @@ function weekdayToNumber(s) } } + +function run_at_monday_mornings(func) { + const ms_in_h = 60/*min*/ * 60/*s*/ * 1000/*ms*/; + + const d = new Date(); + const hour = d.getHours(); // eg. fri 17:20 -> 17 + const weekday = d.getDay() || 7; // 1 = monday, 7 = sunday + + days_to_elapse = 8 - weekday; + ms_to_elapse = ms_in_h * ( + days_to_elapse * 24/*hours in a day*/ + - hour // removes unneccessary hours so that we update it at 0 am + ) + 1 /*+1 so that we know that it won't be exactly midnight + and possibly sunday. idk if this actually is needed.*/; + + setTimeout(() => run_at_monday_mornings(func), ms_to_elapse); + + if (weekday === 1) + func(); +} + module.exports = { fromString: stringToDate, between: isBetweenDates, - weekdayToNumber: weekdayToNumber -} \ No newline at end of file + weekdayToNumber: weekdayToNumber, + run_at_monday_mornings: run_at_monday_mornings +} diff --git a/food.js b/food.js index 4b9ef74..7a9691d 100644 --- a/food.js +++ b/food.js @@ -48,6 +48,8 @@ async function buildFoods(DB) } } await Promise.all(foodInitOperations); + console.log("Foods updated.") + return 0; } function getFoodLink(week) diff --git a/server.js b/server.js index 66f5b19..c496b14 100644 --- a/server.js +++ b/server.js @@ -49,18 +49,10 @@ async function init() // Update... // ...shifts and classes await updateDB.update(SQLDB, "../Updation/shifts.txt", "../Updation/vanhalops.csv", "../Updation/uusilops.csv"); - console.log("Shifts and classes updated."); // ...foods - await food.build(SQLDB); - setInterval( - () => - { - food.build(SQLDB); - }, - 7 * 24 * 60 * 60 * 1000 - ); - console.log("Foods updated.") - + run_at_monday_mornings(() => food.build(SQLDB)); + if ((new Date()).getDay() !== 1) // update if it's not monday. if it's monday, it has already been run by the scheduler above. + await food.build(SQLDB); // server code async function server(req, res) { diff --git a/update.js b/update.js index c946304..9a0c027 100644 --- a/update.js +++ b/update.js @@ -9,7 +9,9 @@ async function buildDB(dbconnection, shiftPath, ...classfiles) shiftCont = shiftCont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which may create problems await parseClasses(dbconnection, ...classfiles), + console.log("Classes updated."); await parse.build(shiftCont, dbconnection) + console.log("Shifts updated."); return 0; } -- cgit v1.2.3