Am using node-cron to run a cron job every 12 Oclock Midnight, the job am running is to increment some data on the user table, the issue now is that the job will run twice.
e.g I wanna increment 15, after 12 o'clock, it will turn to 17 instead of 16.
have tried my possible best to detect the issue, am unable.
When I stop the Nodejs Application, the MySQL database keeps updating, whereas have already off the nodejs application here is the job am running
const CronJob = require('cron').CronJob;
const logger = require("../helpers/logger");
const { updateUser } = require("../helpers/user");
const db = require("../models/db");
const job = new CronJob('* * * * *', async function() {
db.query("UPDATE sponsored_posts SET post_status = 0", (err, data) => {
if (err) logger.debug(err)
});
db.query("DELETE FROM sponsored_posts", (err, data) => {
if (err) logger.debug(err)
});
//EXPIRING USER
db.query("SELECT * FROM all_users WHERE membership_expired > 0", async (err, data) => {
if (data.length > 0) {
//FIlter Through All User
//Deduct While They Havent Expired
await data.map(async user => {
await updateUser({
uid: parseInt(user.uid),
membership_expired: user.membership_expired - 1
})
})
}
})
db.query("SELECT * FROM all_users WHERE membership_expired = 0", async (err, data) => {
if (data.length > 0) {
//FIlter Through All User
//Set Can Purchase To 1
await data.map(async user => {
await updateUser({
uid: parseInt(user.uid),
can_purchase: 1
})
})
}
});
}, null, false, "Africa/Lagos");
job.start();