Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit the tasks created using node-cron

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.

like image 265
Arvind Avatar asked Jul 19 '18 11:07

Arvind


1 Answers

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()
like image 133
Alan Avatar answered Oct 04 '22 08:10

Alan