Skip to content

Commit 5c8cc8f

Browse files
committed
Switch setInterval > node-schduler for better relaibility
1 parent ccf83f9 commit 5c8cc8f

File tree

4 files changed

+148
-16
lines changed

4 files changed

+148
-16
lines changed

package-lock.json

+137-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"express": "^4.17.1",
2929
"jsdoc": "^3.6.10",
3030
"jsonwebtoken": "^8.5.1",
31+
"node-schedule": "^2.1.0",
3132
"nodemailer": "^6.7.2",
3233
"pg": "^8.7.1",
3334
"sequelize": "^6.3.5",

src/background_jobs/unconfirmed_emails.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
module.exports.remove_unconfirmed_email_users = async () => {
16+
console.log("job executed !")
1617

1718

1819
// find and retreive all the users with an unconfirmed email address

src/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const express = require('express');
55
const { verify } = require('jsonwebtoken');
66
const { Sequelize } = require('sequelize');
77
const models = require('./models');
8+
const schedule = require('node-schedule');
89
const { remove_unconfirmed_email_users } = require("./background_jobs/unconfirmed_emails");
910

1011
// Configure Local Variables
@@ -127,9 +128,14 @@ app.get("/confirmEmail/:token", require("./routes/User/Account/confirmEmail"));
127128
// await the database creation process, so as we can access the data on our jobs
128129
await sequelize.sync().then(() => { app.emit('database-started'); });
129130

130-
// execute the job once a while, 20 days in this example
131-
// note that there is a limit for the interval since it is an integer
132-
setInterval(remove_unconfirmed_email_users, 20 * 24 * 60 * 60 * 1000)
131+
// this will run the job every first day of each month at 00:00;
132+
schedule.scheduleJob(
133+
"remove user accounts with unconfirmed email addresses",
134+
"0 0 1 * *",
135+
() => {
136+
remove_unconfirmed_email_users();
137+
},
138+
)
133139
})()
134140

135141
// Start Server

0 commit comments

Comments
 (0)