This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The NodeJS backend also needs scheduled tasks for processing, such as backup, scheduled mail sending, settlement and other operations, so the Node Schedule NPM is used to help us formulate the script of scheduled tasks.

npm install node-schedule
Copy the code

Each scheduled task in Node Schedule is represented by a Job object. You can create it manually and then execute the schedule() method to apply the task, or use scheduleJob() as follows.

The Job object is’ EventEmitter ‘and emits the following events:

  • runEvents after each execution.
  • scheduledEvents at each schedule run time.
  • onecanceledWhen it calls the canceled event before execution.
  • aerrorThe throw or exit event is rejected when the scheduling job is invokedPromise.

(Scheduled and Canceled events both accept a JavaScript date object as an argument.) Note that the task is executed immediately for the first time, so if you create the task using the scheduleJob() method, the first scheduled event will be missed, but you can query the call manually.

Cron format

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7├ ─ ├ ─ month (1 - 12│ ├ ─ ├ ─ dayof month (1 - 31│ ├ ─── hour (0 - 23├ ─── minute (0 - 59) └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ the second (0 - 59OPTIONAL) number one per minute30A second trigger:'30 * * * * *'Per hour1points30A second trigger:30 1 * * * *Every morning in the wee hours1point1points30A second trigger:'30 1 1 * * *The monthly1day1point1points30A second trigger:'30 1 1 1 * *'

2016Years of1month1day1point1points30A second trigger:'30 1 1 1 2016 *'Once a week1the1point1points30A second trigger:1 1 * * 1
Copy the code

Here is an individual using the Node-schedule wrapper class to add, delete, change and view scheduled tasks

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

exports.Interval = class Interval {
  constructor({ unit_name, maintain_time, last_alarm }) {
    this.unit_name = unit_name          // Task name
    this.maintain_time = maintain_time  // Timing time
    this.last_alarm = last_alarm || ""        // Name of the last scheduled task
  }

  Generates a new scheduled task
  async create(callback) {
    // Terminates the previous scheduled task
    if (this.last_alarm ! = ="") {
      this.delete(this.last_alarm)
    }
    schedule.scheduleJob(`The ${this.unit_name}`.`The ${this.maintain_time}`, callback);
  }

  // Delete a scheduled task
  delete() {
    if (schedule.scheduledJobs[this.unit_name]) {
      schedule.scheduledJobs[this.unit_name].cancel();
      return true
    }
    return false
  }

  // Find a scheduled task
  findOne(name) {
    if (schedule.scheduledJobs[name]) {
      return schedule.scheduledJobs[name]
    } else {
      throw new Error("Task name not found")}}// View all scheduled tasks
  findAll() {
    return schedule.scheduledJobs
  }
}
Copy the code

Here is an instance of the call-time timed task Interval

// Scheduled task
new Util.Interval({
  unit_name: 'Automatic dispatch task 0 0 12 * * *'.maintain_time: '0 0 12 * * *'.last_alarm: 'Automatic dispatch task 0 0 12 * * *'
}).create(async() = > {// Write the function you want to execute when the scheduled task is triggered
})
Copy the code