Skip to content

Commit

Permalink
Add monthly dummy data refresh cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogoncalves03 committed Feb 12, 2024
1 parent 364b348 commit ff24cc7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN npm install

# Bundle app source
COPY .env* .
COPY init-demo.sql .

# Setup pdfmake VFS
RUN mkdir node_modules/pdfmake/report
Expand Down
30 changes: 30 additions & 0 deletions backend/src/cron/monthlyDummyData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const { CronJob } = require("cron");
const { logError } = require("../modules/logging");

new CronJob(
"0 4 1 * *", // 4 am on the 1st day of every month
async () => {
try {
const bashCommand = `PGPASSWORD=${process.env.DB_PASSWORD} psql -h ${process.env.DB_DEMO_HOST} -U ${process.env.DB_USER} -d ${process.env.DB_NAME} -a -f ../../init-demo.sql`;
const options = {
cwd: __dirname,
};

const { stderr } = await exec(bashCommand, options);

if (stderr) {
throw new Error(stderr);
}
} catch (error) {
logError(
"cron/monthlyDummyData",
`a problem occurred while running the monthlyDummyData job\n${error.stack}`,
);
}
},
null,
true,
"Europe/Lisbon",
);
1 change: 1 addition & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ app.listen(process.env.PORT, () => {
});

require("./cron/weeklyBackup");
require("./cron/monthlyDummyData");
require("./cron/dailyReminders");

0 comments on commit ff24cc7

Please sign in to comment.