javascript 如何删除节点调度中的所有调度?

fv2wmkja  于 2023-01-01  发布在  Java
关注(0)|答案(6)|浏览(208)

我看到从docs您可以按名称逐个删除,例如...

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

 // sample announcement

 var rule = new schedule.RecurrenceRule();
 rule.dayOfWeek = [1, 2, 3, 4, 5];
 rule.minute = 50;
 rule.hour = 12;

 var message = schedule.scheduleJob("AnnouncementOne", rule, function() {
    // make my announcement
})

AnnouncementOne.cancel();

但我想代码得到所有的调度作业,然后循环通过他们删除每一个。我认为以下得到所有的作业...

var jobList = schedule.scheduledJobs;

它将以下内容输出到控制台...

{ 'AnnouncementOne':
   Job {
     job: [Function],
     callback: false,
     name: 'AnnouncementOne',
     trackInvocation: [Function],
     stopTrackingInvocation: [Function],
     triggeredJobs: [Function],
     setTriggeredJobs: [Function],
     cancel: [Function],
     cancelNext: [Function],
     reschedule: [Function],
     nextInvocation: [Function],
     pendingInvocations: [Function] },
  'AnnouncementTwo':
   Job {
     job: [Function],
     callback: false,
     name: 'AnnouncementTwo',
     trackInvocation: [Function],
     stopTrackingInvocation: [Function],
     triggeredJobs: [Function],
     setTriggeredJobs: [Function],
     cancel: [Function],
     cancelNext: [Function],
     reschedule: [Function],
     nextInvocation: [Function],
     pendingInvocations: [Function] } }

如何循环jobList以删除每个作业?
替代代码:

var jobList = schedule.scheduledJobs;

for(jobName in jobList){
  var job = 'jobList.' + jobName;
  eval(job+'.cancel()');
}
6tdlim6h

6tdlim6h1#

最好远离eval

const schedule = require('node-schedule');
const _ = require('lodash');
const j = schedule.scheduleJob('SomeDirtyWork', '42 * * * *', () => {
  // Something is going on
});

const jobNames = _.keys(schedule.scheduledJobs);
for(let name of jobNames) schedule.cancelJob(name);
swvgeqrz

swvgeqrz2#

这可能不是最优雅的方法,但由于jobList缺少长度字段,下面是一种实现方法:

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

// sample announcement

var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [1, 2, 3, 4, 5];
rule.minute = 50;
rule.hour = 12;

var message = schedule.scheduleJob("Announcement0", rule, function() {
    // make my announcement
})

var message = schedule.scheduleJob("Announcement1", rule, function() {
    // make my announcement
})

var jobList = schedule.scheduledJobs;

for (var i = 0; i < 2; i++) {
   eval('jobList.Announcement'+i+'.cancel()');
}

这将从列表中删除计划的作业。
要在删除作业之前检查作业是否存在:

for(var i = 0; i < 2; i++){
  var job = 'jobList.Announcement'+i;
  if(eval(job) != undefined) {
    eval(job+'.cancel()');
  }
}

当然,你也可以使用for-in。只要记住,如果你使用for-in,结果是不排序的,而结果在常规的for循环中是排序的,所以你可能会遇到一些意想不到的行为:

var jobList = schedule.scheduledJobs;

for(jobName in jobList){
  var job = 'jobList.' + jobName;
  eval(job+'.cancel()');
}
wh6knrhe

wh6knrhe3#

在一行中:

for (const job in schedule.scheduledJobs) schedule.cancelJob(job);
rkkpypqq

rkkpypqq4#

var jobList = cron.scheduledJobs; //Get All scheduled jobs

Object.values(jobList).map(job => {
  console.log("Each Job", job.name);

    cron.cancelJob(job.name);
  })
1tuwyuhd

1tuwyuhd5#

你好,我有同样的问题,但你可以使用下面的代码.

var jobList = schedule.scheduledJobs;
for(jobName in jobList){
    // Here inside **jobName** you are getting name of each Schedule.
    var EachJobObject = cron.scheduledJobs[jobName];
    EachJobObject.cancel();
}

以上代码将取消所有节点计划.它为我工作.

doinxwow

doinxwow6#

您还可以执行以下操作:

for (const job in schedule.scheduledJobs) schedule.scheduledJobs[job].cancel();

相关问题