由于对这些软件包所做的更改,以下答案现在可能不相关。这scheduleJob
本来是一个伪代码来创建一个工作。目的是展示如何阻止他们。
这是为三个 cron 模块( cron、node-cron和node-schedule )实例化、启动和停止调度的 cron 作业的综合摘要:
1. CRON
为了cancel
工作,您可以创建一个具有唯一名称的工作。
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.节点-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总结