diff options
| -rw-r--r-- | Functions/dateFuncs.js | 26 | ||||
| -rw-r--r-- | food.js | 2 | ||||
| -rw-r--r-- | server.js | 14 | ||||
| -rw-r--r-- | 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 +} @@ -48,6 +48,8 @@ async function buildFoods(DB)  		}  	}  	await Promise.all(foodInitOperations); +	console.log("Foods updated.") +	return 0;  }  function getFoodLink(week) @@ -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)  	{ @@ -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;  }  | 
