aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kronqvist <joelkronqvist@proton.me>2023-06-21 12:30:58 +0300
committerJoel Kronqvist <joelkronqvist@proton.me>2023-06-21 12:30:58 +0300
commit9760c6242572f6ed17890b84ec3d14fa414b7579 (patch)
tree710049705e74f5ecbc2b79ffa21eabe637f2b5ec
parent5f74d40fa736745651514853afdca3ed44e5ae74 (diff)
downloadLYLLRuoka-9760c6242572f6ed17890b84ec3d14fa414b7579.tar.gz
LYLLRuoka-9760c6242572f6ed17890b84ec3d14fa414b7579.zip
Some patches made in production :facepalm:
At least some of them have to do with running updations timely?
-rw-r--r--Functions/dateFuncs.js5
-rw-r--r--README.md4
-rw-r--r--food.js6
-rwxr-xr-xinit.sh4
-rw-r--r--server.js17
-rwxr-xr-xsetup.sh2
6 files changed, 20 insertions, 18 deletions
diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js
index 55a9372..1c8a9df 100644
--- a/Functions/dateFuncs.js
+++ b/Functions/dateFuncs.js
@@ -46,9 +46,8 @@ function run_at_monday_mornings(func) {
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.*/;
+ - hour + 1.5 // removes unneccessary hours so that we update it at 1:30 am
+ );
setTimeout(() => run_at_monday_mornings(func), ms_to_elapse);
diff --git a/README.md b/README.md
index d2f8d41..05b2880 100644
--- a/README.md
+++ b/README.md
@@ -139,9 +139,9 @@ If you need to troubleshoot the starting, you can find some logs in /tmp/slogs.
Currently the notifications have to be added manually to the MySQL database. Here's an example:
```
USE lyllruoka;
-INSERT INTO exams VALUES ('2021-11-22', '2021-11-30', '<h2>Koeviikko</h2><br>22.11. - 30.11..<br>Kouluruokaa on tarjolla 10:45-11:30.');
+INSERT INTO exams VALUES ('2021-11-22', '2021-11-30', '<h2>Koeviikko</h2>22.11. - 30.11..<br>Kouluruokaa on tarjolla 10:45-11:30.');
```
-The first value in the parenthesis is the start date of the notification, the second the end date of the notification and the third value is the message to display. HTML is supported. The message will override the food shift search.
+The first value in the parenthesis is the start date of the notification, the second the end date of the notification and the third value is the message to display. HTML is supported. The message will override the food shift result.
## Updating the developer table
diff --git a/food.js b/food.js
index 7a9691d..5744090 100644
--- a/food.js
+++ b/food.js
@@ -4,7 +4,7 @@ const { weekdayToNumber } = require("./Functions/dateFuncs.js");
function* scrapeFood(data)
{
- const foodRegex = /<title>(\w{2} (?:\d\d?\.){2}\d{4})<\/title><description><!\[CDATA\[(Lounas) ?:? ?(.*?)(Kasvislounas) ?:? ?(.*?)]]><\/description>/gm;
+ const foodRegex = /<title>(\w{2} (?:\d\d?\.){2}\d{4})<\/title><description><!\[CDATA\[(Lounas) ?:? ?(.*?)<br>(Kasvislounas) ?:? ?(.*?)]]><\/description>/gm
const foods = data.matchAll(foodRegex);
for(const food of foods)
{
@@ -31,14 +31,14 @@ async function buildFoods(DB)
{
for(const food of foods[week - 1])
{
- foodInitOperations.push(DB.execute("INSERT INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [
+ foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, FALSE, ?, ?, ?)", [
week,
food[0],
food[2][0],
food[1],
food[2][1]
]));
- foodInitOperations.push(DB.execute("INSERT INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [
+ foodInitOperations.push(DB.execute("INSERT IGNORE INTO foods VALUES (?, ?, TRUE, ?, ?, ?)", [
week,
food[0],
food[3][0],
diff --git a/init.sh b/init.sh
index 5fb171f..3a85f04 100755
--- a/init.sh
+++ b/init.sh
@@ -2,7 +2,7 @@
exec 1>>/var/slogs
exec 2>>/var/slogs
echo ""
-echo "# Init running"
+echo "# Init running at $(date)"
echo "# Waiting for connection..."
while ! ping 'example.org' -c 1; do
@@ -26,6 +26,6 @@ while echo "# node server.js:"; do
# Sleep below, so that the loop can't cause too big a load to the server, if the server terminates very fast.
sleep 5
- echo "SERVER TERMINATED!"
+ echo "# SERVER TERMINATED at $(date)!"
echo "--------"
done
diff --git a/server.js b/server.js
index c496b14..04192a3 100644
--- a/server.js
+++ b/server.js
@@ -50,7 +50,7 @@ async function init()
// ...shifts and classes
await updateDB.update(SQLDB, "../Updation/shifts.txt", "../Updation/vanhalops.csv", "../Updation/uusilops.csv");
// ...foods
- run_at_monday_mornings(() => food.build(SQLDB));
+ dateFuncs.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
@@ -239,16 +239,19 @@ async function buildMain(args)
const examInfo = await SQLDB.query("SELECT * FROM exams");
for(let week = 0; week < examInfo.length; week++)
{
+ const nextDate = new Date(
+ d.getFullYear(),
+ d.getMonth(),
+ d.getDate() + day - actualDay + (day < actualDay)*7
+ );
if (dateFuncs.between(
- d,
+ nextDate,
new Date(examInfo[week].start),
new Date(examInfo[week].end)
))
{
- const message = "<div id=\"foodshift\">" +
- `<div class="float-block">${examInfo[week].message}</div>` +
- "</div";
- data_string = strFuncs.replaceElement(data_string, "div id=\"foodshift\"", message);
+ const message = `<div class="shift-result float-block">${examInfo[week].message}</div>`;
+ data_string = strFuncs.replaceElement(data_string, "div id=\"shift-result\" class=\"float-block\"", message);
}
}
@@ -283,7 +286,7 @@ async function buildMain(args)
res["food-header"] = `Kouluruoka ${weekdays[day]}`;
res["food"] = "Päivän ruoka puuttuu tietokannasta.";
}
- if (vege[0] !== undefined) {
+ if ((vege[0] !== undefined) && (vege[0].food !== res["food"])) {
res["vege-header"] = vege[0].header;
res["vege"] = vege[0].food;
} else {
diff --git a/setup.sh b/setup.sh
index 729a5ce..b4111fe 100755
--- a/setup.sh
+++ b/setup.sh
@@ -89,7 +89,7 @@ if [ "$certans" != "n" ]; then
fi
echo 'Setting up MySQL database...'
-echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exams (\n\tstart DATE,\n\tend DATE,\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(256)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null
+echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exams (\n\tstart DATE,\n\tend DATE,\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(280)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null
echo -e "{\n\t\"host\": \"localhost\",\n\t\"user\": \"$name\",\n\t\"password\": \"$passw\",\n\t\"database\": \"lyllruoka\"\n}" > dblogin.txt
echo -e 'Done!\n'