From d2f4ee6f4f58be2331675edc9fb1dcec578cc911 Mon Sep 17 00:00:00 2001 From: Adrian Winterstein Date: Thu, 3 Jul 2025 16:56:47 +0200 Subject: [PATCH] Fix Group dailies reset not working. (#7) Fix group dailies not reset #2 --------- Co-authored-by: Adrian Winterstein Co-authored-by: Saik0Shinigami --- Dockerfile | 14 ++++++- scripts/team-cron.js | 4 +- scripts/team-cron/habiticateamcron | 1 + scripts/team-cron/run-team-cron.js | 64 ++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 scripts/team-cron/habiticateamcron create mode 100644 scripts/team-cron/run-team-cron.js diff --git a/Dockerfile b/Dockerfile index 15ac44f2b7..4fccc3b49f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -72,8 +72,18 @@ COPY --from=build /usr/src/habitica/website/ /var/lib/habitica/website/ COPY --from=build /usr/src/habitica/package.json /var/lib/habitica/package.json COPY --from=build /usr/src/habitica/config.json /var/lib/habitica/config.json -CMD ["node", "/var/lib/habitica/website/transpiled-babel/index.js"] +# Copy the scripts and cron job for resetting group dailies on a daily basis +RUN mkdir -p /var/lib/habitica/scripts/ +COPY --from=build /usr/src/habitica/scripts/team-cron.js /var/lib/habitica/scripts/team-cron.js +COPY --from=build /usr/src/habitica/scripts/team-cron/run-team-cron.js /var/lib/habitica/ +RUN apt-get update && apt-get -y install cron +RUN mkdir -p /etc/cron.d +COPY --from=build /usr/src/habitica/scripts/team-cron/habiticateamcron /etc/cron.d/habiticateamcron +RUN chmod 0644 /etc/cron.d/habiticateamcron +RUN crontab /etc/cron.d/habiticateamcron + +CMD sh -c 'printenv | grep -v "no_proxy" >> /etc/environment && /etc/init.d/cron start && node /var/lib/habitica/website/transpiled-babel/index.js' # Container for providing the build web component of Habitica @@ -98,4 +108,4 @@ RUN echo -e ":80 {\n\ root * /var/www\n\ reverse_proxy @backend server:3000\n\ file_server\n\ -}" > /etc/caddy/Caddyfile \ No newline at end of file +}" > /etc/caddy/Caddyfile diff --git a/scripts/team-cron.js b/scripts/team-cron.js index 6e84acaa5f..e621aa54e1 100644 --- a/scripts/team-cron.js +++ b/scripts/team-cron.js @@ -100,11 +100,11 @@ async function updateTeamTasks (team) { return Promise.all(toSave); } -export default async function processTeamsCron () { +module.exports = async function processTeamsCron() { const activeTeams = await Group.find({ 'purchased.plan.customerId': { $exists: true }, }, { cron: 1, leader: 1, purchased: 1 }).exec(); const cronPromises = activeTeams.map(updateTeamTasks); return Promise.all(cronPromises); -} +}; diff --git a/scripts/team-cron/habiticateamcron b/scripts/team-cron/habiticateamcron new file mode 100644 index 0000000000..8336a3bfde --- /dev/null +++ b/scripts/team-cron/habiticateamcron @@ -0,0 +1 @@ +2 * * * * . /etc/environment; cd /var/lib/habitica && /usr/local/bin/node ./run-team-cron.js >> /var/log/cron.log 2>&1 diff --git a/scripts/team-cron/run-team-cron.js b/scripts/team-cron/run-team-cron.js new file mode 100644 index 0000000000..f43a322f88 --- /dev/null +++ b/scripts/team-cron/run-team-cron.js @@ -0,0 +1,64 @@ +const mongoose = require("mongoose"); +const nconf = require("nconf"); + +// Initialize nconf with hierarchical configuration + +nconf + .argv() // Command-line arguments first + .env() // Environment variables second + .file({ + // Configuration file third + file: "config.json", + }) + .defaults({ + // Default values last + SESSION_SECRET_KEY: null, + SESSION_SECRET_IV: null, + }); + +// Get database URI +const dbUri = nconf.get("NODE_DB_URI"); + +async function main() { + try { + // Connect to MongoDB and wait for connection + await mongoose.connect(dbUri, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); + + console.log("Connected to MongoDB"); + + // Register babel after MongoDB connection + require("@babel/register")({ + extensions: [".js"], + presets: ["@babel/preset-env"], + cache: false, + }); + + console.log("Babel registered"); + + const processTeamsCron = require("./scripts/team-cron.js"); + + if (typeof processTeamsCron !== "function") { + throw new Error("processTeamsCron is not properly exported"); + } + + // Run the cron job + console.log("Starting team cron processing..."); + await processTeamsCron(); + console.log("Team cron processing completed"); + + // Close the DB connection + await mongoose.connection.close(); + process.exit(0); + } catch (error) { + console.error("Error:", error); + if (mongoose.connection) { + await mongoose.connection.close(); + } + process.exit(1); + } +} + +main().catch(console.error);