Trying to run the tasks based on schedule using node-cron 'https://github.com/merencia/node-cron'.
Task creation and starting it:
var cron = require('node-cron'),
task = cron.schedule('* * * * * *', function () {
console.log('task running...',JSON.stringify(task));
}, false);
task.start();
To stop the task:
task.stop();
To destroy the task:
task.destroy();
The code works fine when tasks are executed within the scope of where they are created. But as per the requirement how can i access the 'task' later from a different function. Can the task be stored in the backend to perform 'stop()' or 'destroy()' functions on it later. If not possible with the node-cron what else can be used. Working with node.js and mongoDb.
I faced the same Issue. Using node-schedule
solved the issue:
start the custom Job:
const campaignId = "MY_CUSTOM_ID"
let job = schedule.scheduleJob(campaignId, '* * * * * *', function () {
console.log('running campaign: ' + campaignId)
})
stop the custom Job:
const campaignId = "MY_CUSTOM_ID"
let current_job = schedule.scheduledJobs[campaignId]
current_job.cancel()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With