admin管理员组

文章数量:1244424

I see from docs you can delete one by one by name for example ...

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();

but I would like code to get all scheduled jobs and then loop through them to delete each one. I think the following gets all the jobs ...

var jobList = schedule.scheduledJobs;

which outputs following to console ...

{ '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] } }

how do I loop through jobList to delete each job?

Alternative Code:

var jobList = schedule.scheduledJobs;

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

I see from docs you can delete one by one by name for example ...

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();

but I would like code to get all scheduled jobs and then loop through them to delete each one. I think the following gets all the jobs ...

var jobList = schedule.scheduledJobs;

which outputs following to console ...

{ '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] } }

how do I loop through jobList to delete each job?

Alternative Code:

var jobList = schedule.scheduledJobs;

for(jobName in jobList){
  var job = 'jobList.' + jobName;
  eval(job+'.cancel()');
}
Share Improve this question edited Oct 8, 2016 at 20:37 MorningSleeper asked Oct 7, 2016 at 20:57 MorningSleeperMorningSleeper 4151 gold badge5 silver badges13 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

It is better to keep away from 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);

In one line:

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

Probably not the most elegant approach, but since jobList lacks a length field, here is a way to achieve this:

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()');
}

This removes the scheduled jobs from the list.

To check whether a job exists prior to deleting it:

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

Of course, you can also use for-in. Just remember that if you use for-in the results are not ordered while the results are ordered in the regular for loop, so you could potentially experience some unexpected behavior:

var jobList = schedule.scheduledJobs;

for(jobName in jobList){
  var job = 'jobList.' + jobName;
  eval(job+'.cancel()');
}
var jobList = cron.scheduledJobs; //Get All scheduled jobs

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

    cron.cancelJob(job.name);
  })

Hello I had same problem, But you can use below code.

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();
}

Above Code will cancel all Node Schedule. it worked for me.

you also can do this:

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

本文标签: javascriptHow to delete all schedules in nodescheduleStack Overflow