Skip to content Skip to sidebar Skip to footer

Start/stop Cronjob On Button Click In Nodejs Express App

I have been working on a project which requires the start and stop of cron scheduler when a user clicks on a button on the front end. Basically when a user clicks on a button, the

Solution 1:

Every time you hit the scheduler api a new instance of cron-job is made and you are stopping the newly defined instance of cron-job not the previous one.

Solution is to define the cron-job out of the scope of router so that whenever you hit the scheduler api the instance won't change

Like this:

const cron = require('node-cron');

// creates the cronjob instance with startScheduler const task = cron.schedule('*/10 * * * * *', () =>  {
    console.log('test cronjob running every 10secs');
}, {
    scheduled: false
});

router.post('/scheduler', async (req, res) => {

    // gets the id from the buttonconst id = req.body.id;

    try{
         // finds the scheduler data from the MongoDBconst scheduler = awaitScheduler.find({ _id: id });

         // checks whether there is a scheduler or notif ( !scheduler  ) {
             return res.json({
                  error: 'No scheduler found.'
             });
         }

         // checks if the scheduler is already running or not. If it is then it stops the schedulerif ( scheduler.isRunning ) {

             // scheduler stopped
             task.stop();

             return res.json({
                  message: 'Scheduler stopped!'
             });
         }

         // starts the scheduler
         task.start();

         res.json({
              message: 'Scheduler started!'
         });

    }catch(e) {
         console.log(e)
    }
});

Solution 2:

The problem might come from the line:

const task = cron.schedule('*/10 * * * * *', () =>  {

which, actually, creates a new task and uses a new Scheduler if you read the source code of node-cron: https://github.com/node-cron/node-cron/blob/fbc403930ab3165ffef7d53387a29af92670dfea/src/node-cron.js#L29

functionschedule(expression, func, options) {
        let task = createTask(expression, func, options);
        storage.save(task);
        return task;
    }

(which, internally, uses: https://github.com/node-cron/node-cron/blob/fbc403930ab3165ffef7d53387a29af92670dfea/src/scheduled-task.js#L7:

let task = new Task(func);
    let scheduler = new Scheduler(cronExpression, options.timezone, options.recoverMissedExecutions);

So, when you call:

    task.stop();

As far as I understand, what you do is calling the method "stop" of a brand new task, not the method stop of the task you launched the first time you clicked the button.

Judging by your code, the problem is that you are not actually using your scheduler while using the task.

PS: The module also exposes a function that lets you retrieve tasks from its storage: https://github.com/node-cron/node-cron/blob/fbc403930ab3165ffef7d53387a29af92670dfea/src/node-cron.js#L58

But as I haven't found any documentation about it, I do not recommend using it.

Post a Comment for "Start/stop Cronjob On Button Click In Nodejs Express App"