简体   繁体   English

无法使用 node-cron 停止 cron 作业

[英]can't stop cron job using node-cron

Currently I'm using node-cron package to schedule the task.目前我正在使用 node-cron package 来安排任务。 I can make the task running but I can't make it stop.我可以让任务运行,但我不能让它停止。

My POST body is:我的 POST 正文是:

{
command: start/stop/destroy, 
}

The code is:代码是:

export const schedule = async (req, res) => {
    const cmd = req.body.command
    const url_taskMap = {}

    // run every midnight GMT+7
    const task = await cron.schedule(process.env.SCHEDULE_TIME, () => {
        console.log('doing task');
    }, {
        scheduled: true,
        timezone: "Asia/Bangkok"
    })

    url_taskMap['url'] = task;
    let my_job = url_taskMap['url'];


    if (cmd === 'start') {
        my_job.start()
        res.status(200).send('Task started!')
    } else if (cmd === 'stop') {
        my_job.stop()
        res.status(200).send('Task stoped!')
    } else {
        my_job.destroy()
        res.status(200).send('Task destroyed!')
    }
}

I follow the answer here https://stackoverflow.com/a/53684854/15088319 but the task still running.我按照这里的答案https://stackoverflow.com/a/53684854/15088319但任务仍在运行。 I don't know how to make it stop.我不知道如何让它停下来。 Can you help me?你能帮助我吗?

I'm not very familiar with node-cron but it looks like there are a couple of problems with this code where the main one is that every request creates a new job, this means that, for example, the first request (cmd: 'start') started a job, then the next request (cmd: 'stop') creates another job and then stops that other job while the first job will keep running...我对node-cron不是很熟悉,但看起来这段代码有几个问题,主要问题是每个请求都会创建一个新作业,这意味着,例如,第一个请求(cmd:' start') 开始一个作业,然后下一个请求 (cmd: 'stop') 创建另一个作业,然后停止另一个作业,而第一个作业将继续运行...

In order to overcome this problem we need to make sure that only a single instance (at most) of the job will run, and when we want to stop it - we're actually stopping that instance.为了克服这个问题,我们需要确保作业的一个实例(最多)将运行,并且当我们想要停止它时 - 我们实际上正在停止该实例。

This can be achieved by persisting the job that runs, and before running a new job first check if we don't have a job that is already running.这可以通过持久化正在运行的作业来实现,并且在运行新作业之前首先检查我们是否没有正在运行的作业。

The second problem is that 'stop', if I understand correctly, will stop the scheduling of a task, so for example, if a task was scheduled to run every minute, and task.stop() was called - the task will not be called again in the next minute.第二个问题是,如果我理解正确,“停止”将停止任务的调度,例如,如果一个任务被安排为每分钟运行一次,并且task.stop()被调用 - 该任务将不会下一分钟又打来电话。 This does not mean that your code from a previously running task will stop executing, if your code has an infinite loop, for example, stopping the task will not create more instances of you code running.这并不意味着之前运行的任务中的代码将停止执行,例如,如果您的代码有无限循环,停止任务不会创建更多正在运行的代码实例。 but the instances that already started to run will run forever.但是已经开始运行的实例将永远运行。 This means that you have the responsibility to make sure you code completes running!这意味着您有责任确保您的代码完成运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM