From a4a53038afec9cedcefdc7bbea4b67525985b139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolas=20G=C3=B6rlitz?= Date: Thu, 7 Dec 2023 11:30:50 +0100 Subject: [PATCH] add endorsement worker --- Dockerfile | 8 +++++++- entry.sh | 7 +++++++ misc/CheckEndorsements.js | 32 ++++++++++++++++++++++++++++++++ misc/Permissions.txt | 14 -------------- misc/crontab.txt | 1 + 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 entry.sh create mode 100644 misc/CheckEndorsements.js delete mode 100644 misc/Permissions.txt create mode 100644 misc/crontab.txt diff --git a/Dockerfile b/Dockerfile index f7b7e70..2ef285f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,12 @@ RUN npm install --quiet --unsafe-perm --no-progress --no-audit --include=dev COPY . . +# Init cron +ADD misc/crontab.txt /crontab.txt +ADD entry.sh /entry.sh +RUN chmod 755 /entry.sh +RUN /usr/bin/crontab /crontab.txt + RUN npm run build -CMD npm run run \ No newline at end of file +CMD ["entry.sh"] \ No newline at end of file diff --git a/entry.sh b/entry.sh new file mode 100644 index 0000000..1e04e26 --- /dev/null +++ b/entry.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +# start cron +/usr/sbin/crond -f -l 8 + +# Run Node +cd opt/trainingcenter_backend && npm run run \ No newline at end of file diff --git a/misc/CheckEndorsements.js b/misc/CheckEndorsements.js new file mode 100644 index 0000000..540f91e --- /dev/null +++ b/misc/CheckEndorsements.js @@ -0,0 +1,32 @@ +const Config = require("../dist/core/Config"); +const Sequelize = require("sequelize") +const dayjs = require("dayjs"); + +/** + * This script periodically checks, if a user has an endorsement bound to a solo, whilst the solo has already passed + * Once every 24 Hours. + */ +const newConf = {...Config.SequelizeConfig, logging: (message) => {console.log(message)}} +const seq = new Sequelize(newConf); +seq.authenticate() + .catch(() => { + console.log("[SEQ] Failed to authenticate..."); + }); + +seq.query("SELECT endorsement_groups_belong_to_users.id, endorsement_groups_belong_to_users.user_id, endorsement_groups_belong_to_users.solo_id, user_solos.current_solo_start, user_solos.current_solo_end FROM endorsement_groups_belong_to_users JOIN user_solos ON user_solos.id = endorsement_groups_belong_to_users.solo_id", { + type: Sequelize.QueryTypes.SELECT +}) +.then((res) => { + res.forEach(async (solo) => { + if (dayjs.utc(solo.current_solo_end).isBefore(dayjs.utc())) { + console.log(`Solo ID ${solo.solo_id} has expired. Removing Endorsement Group...`); + } else { + console.log(`Solo ID ${solo.solo_id} is expiring on ${dayjs.utc(solo.current_solo_end)} (${Math.abs(dayjs.utc(solo.current_solo_end).diff(dayjs.utc()))} Day(s) remaining).`) + } + + await seq.query("DELETE FROM endorsement_groups_belong_to_users WHERE ID = ?", {replacements: [solo.id], type: Sequelize.QueryTypes.DELETE}) + }); +}) +.finally(async() => { + await seq.close() +}); \ No newline at end of file diff --git a/misc/Permissions.txt b/misc/Permissions.txt deleted file mode 100644 index b206f9e..0000000 --- a/misc/Permissions.txt +++ /dev/null @@ -1,14 +0,0 @@ -############ -## COURSE ## -############ -COURSE.CREATE -COURSE.UPDATE - -########## -## Tech ## -########## -TECH.SYSLOG.VIEW -TECH.PERMISSIONS.VIEW -TECH.ROLES.VIEW -... - diff --git a/misc/crontab.txt b/misc/crontab.txt new file mode 100644 index 0000000..458a02f --- /dev/null +++ b/misc/crontab.txt @@ -0,0 +1 @@ +* * * * * cd /opt/trainingcenter_backend && node /opt/trainingcenter_backend/misc/CheckEndorsements.js >> /var/log/check_endorsement.log \ No newline at end of file