node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

Pass job instance as onTick callback parameter

Open antoine-pous opened this issue 5 years ago • 1 comments

Actually we can't manipulate the job from it's callback.

It would be really cool to be able to pause it and update it depending the task itself.

For this use case the simplest way is to pass the CronJob instance as parameter for onTick and onComplete.

Here's a small use case:

const exec = async (job: CronJob) => {
  job.stop()
  // Do the job...
  job.start()
}
new CronJob('* * * * * *', exec, () => {}, true)

antoine-pous avatar Dec 08 '20 17:12 antoine-pous

Cant you do this by just passing in the instance like this?

var runCron = async function (job: CronJob) {
   console.log("CRON running", job.running)
}

var cronJob = new CronJob({
    cronTime: '0 */5 * * * *',
    onTick: () => runCron(cronJob),
    start: true
})

mayerlench avatar Jun 07 '21 16:06 mayerlench