在Nodejs Express应用中单击按钮即可启动/停止cronjob [英] Start/stop cronjob on button click in Nodejs Express app

查看:122
本文介绍了在Nodejs Express应用中单击按钮即可启动/停止cronjob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个项目,当用户单击前端的一个按钮时,该项目需要cron计划程序的启动和停止.基本上,当用户单击按钮时,cron作业将开始.然后单击停止按钮将停止计时器.就这么简单.

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 cron job will start. And clicking the stop button will stop the timer. It is as simple as that.

要实现这一点,我要在按钮单击时向Nodejs/Express后端发出发布请求,这将触发调度程序的启动/停止功能.这是端点的样子:

To achieve that, I am making post requests to the Nodejs/Express backend on button click which triggers start/stop function of the scheduler. This is how the endpoint looks like:

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

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

    // gets the id from the button
    const id = req.body.id;

    try{
         // finds the scheduler data from the MongoDB
         const scheduler = await Scheduler.find({ _id: id });

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

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

         // checks if the scheduler is already running or not. If it is then it stops the scheduler
         if ( 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)
    }
});

现在,调度程序可以完美运行,但不会在第二次单击按钮时停止.它继续运行.我觉得我没有在正确的位置调用 task.start() task.stop().而且我不知道正确的地方在哪里.我实际上是cronjobs的新手.

Right now the scheduler runs perfectly but it doesn't stop on second button click. It keeps on running. I feel like I'm not calling task.start() and task.stop() at correct places where it would work. And I don't know where the correct places are. I'm actually new to cronjobs.

如果有人告诉我我做错了,那太好了.

It would be great if someone tells me what I am doing wrong.

谢谢.

推荐答案

每次您按下 scheduler api 时,都会创建一个 cron-job的新实例,而您停止新定义的cron-job实例,而不是上一个实例.

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.

解决方案是在路由器的范围之外定义 cron-job ,以便每当您按下 scheduler api 时实例都不会改变

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

赞:

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 button
    const id = req.body.id;

    try{
         // finds the scheduler data from the MongoDB
         const scheduler = await Scheduler.find({ _id: id });

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

         // checks if the scheduler is already running or not. If it is then it stops the scheduler
         if ( 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)
    }
});

这篇关于在Nodejs Express应用中单击按钮即可启动/停止cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆