Issue
I am building a full stack project, the frontend can send a date to the server (Nodejs) and right now the server creates a cron job that edits a column called isActive
in the database which is of type boolean
.
I am using the Node schedule npm package in order to schedule the cron jobs and I am wondering if having a cron job that edits the isActive
column is a good idea.
The main problem that I am thinking could happen is:
What happens if a user wants to change the isActive
column on a specific record in a week from now but then they changed their mind and they want to cancel that. Is there a way to cancel that cron job or is there a way to get some kind of reference to that particular cron job?
I am very new to cron jobs so I am not sure how to work with them yet.
Thank you in advance for your help
The cron job is working as expected but I am not sure how to handle canceling that cron job for a specific user.
Solution
It is possible to cancel a specific user.
Demo, Two users (user1 and user2) scheduled at the first time on weekend.
But user2 want to cancel before weekend, so only user1 update isActive column.
Code as time.js
file.
const schedule = require('node-schedule');
const moment = require('moment')
// All users want to cancel for editIsActive()
let users = [
{
user : 'user1',
isCancel : false
},
{
user : 'user2',
isCancel : false
}
];
// Your database to update here except canceled user
function editIsActive(item) {
if (!item.isCancel) {
console.log(`${item.user} is edited a isActive column`);
}
}
// user 2 canceled for editIsActive()
const some_user_cancel_job = schedule.scheduleJob('5 * * * * *', () => {
if (!users[1].isCancel) {
console.log('user 2 want to cancel to edit on xx:xx:5 (week day)' + moment().format("MM/DD/YYYY HH:mm:ss"));
users[1].isCancel = true
}
});
// Main cron job scheduler
const cron_job = schedule.scheduleJob('10 * * * * *', () => {
console.log('cron job called on every xx:xx:10 (week end)' + moment().format("MM/DD/YYYY HH:mm:ss"));
users.forEach((item) => {
editIsActive(item)
})
});
// All scheduled cron jobs clear in here
const clear_job = schedule.scheduleJob('6 * * * *', () => {
console.log('Canceled all cron jobs on next Monday' + moment().format("MM/DD/YYYY HH:mm:ss"));
some_user_cancel_job.cancel()
cron_job.cancel()
clear_job.cancel()
});
Result
$ node time.js
user 2 want to cancel to edit on xx:xx:5 (week day)12/28/2022 16:02:05
cron job called on every xx:xx:10 (week end)12/28/2022 16:02:10
user1 is edited a isActive column
cron job called on every xx:xx:10 (week end)12/28/2022 16:03:10
user1 is edited a isActive column
cron job called on every xx:xx:10 (week end)12/28/2022 16:04:10
user1 is edited a isActive column
cron job called on every xx:xx:10 (week end)12/28/2022 16:05:10
user1 is edited a isActive column
Canceled all cron jobs on next Monday12/28/2022 16:06:00
All schedule time is not real data. You need to see manual
Example) every Sunday at 2:30pm:
const job = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
console.log('Time for tea!');
});
Answered By - Bench Vue Answer Checked By - Senaida (WPSolving Volunteer)