Skip to content

Commit

Permalink
add endorsement worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Dec 7, 2023
1 parent a39259c commit a4a5303
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
CMD ["entry.sh"]
7 changes: 7 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

# start cron
/usr/sbin/crond -f -l 8

# Run Node
cd opt/trainingcenter_backend && npm run run
32 changes: 32 additions & 0 deletions misc/CheckEndorsements.js
Original file line number Diff line number Diff line change
@@ -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()
});
14 changes: 0 additions & 14 deletions misc/Permissions.txt

This file was deleted.

1 change: 1 addition & 0 deletions misc/crontab.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * cd /opt/trainingcenter_backend && node /opt/trainingcenter_backend/misc/CheckEndorsements.js >> /var/log/check_endorsement.log

0 comments on commit a4a5303

Please sign in to comment.