如何停止节点 cron 作业 [英] How to stop a node cron job

查看:161
本文介绍了如何停止节点 cron 作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些条件下停止节点 cron 作业.

I want to stop a node cron job on certain conditions.

这就是我开始每 1 秒运行一次任务的方式.

This is how I started the task to run every 1 second.

const runScheduler = (url)=>{
    cron.schedule('*/1 * * * * *', () => {
        console.log('running a task every one second');
        async.waterfall([ getUrlDelay(url), dbMethods.updateUrlResponses(url) ],function(err, success){
            if(err) return console.log('Url Not Found');
        })
      });
}

我想停止这项工作,但无法完成.

I want to stop this job but unable to do it.

推荐答案

由于对这些包所做的更改,以下答案现在可能不相关.scheduleJob 本来是用来创建作业的伪代码.目的是展示如何阻止他们.

The below answer may not be relevant now because of the changes done to these packages. The scheduleJob was meant to be a pseudocode to just create a job. The objective was to show how to stop them.

以下是三个 cron 模块(cron, node-cron & node-schedule):

Here's a comprehensive summary for instantiating, starting and stopping sheduled cron-jobs for three cron-modules (cron, node-cron & node-schedule):

1.CRON

为了取消作业,您可以创建一个具有唯一名称的作业.

In order to cancel the job you can create a job with unique name.

var cron = require('cron').CronJob;

var j = cron.scheduleJob(unique_name, '*/1 * * * * *',()=>{
    //Do some work
});
// for some condition in some code
let my_job = cron.scheduledJobs[unique_name];
my_job.stop();

它应该取消作业.

2.NODE-CRON

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

const url_taskMap = {};
const task = cron.schedule('*/1 * * * * *',()=>{
    //Foo the bar..
});
url_taskMap[url] = task;
// for some condition in some code
let my_job = url_taskMap[url];
my_job.stop();

3.节点时间表

var schedule = require('node-schedule');

let uniqueJobName = specificURL;
// Shedule job according to timed according to cron expression
var job = schedule.scheduleJob(uniqueJobName,'*/10 * * * * *', function(){
     //Bar the foo..
});
// Inspect the job object (i.E.: job.name etc.)
console.log(`************** JOB: ******************`);
console.log(job);

// To cancel the job on a certain condition (uniqueJobName must be known) 
if (<someCondition>) {
    let current_job = schedule.scheduledJobs[uniqueJobName];
    current_job.cancel();
}

Aritra Chakraborty 总结 &ILuvLogix

Summarized by Aritra Chakraborty & ILuvLogix

这篇关于如何停止节点 cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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