Home > Web Front-end > JS Tutorial > How to use Node Schedule to create scheduled tasks in node projects

How to use Node Schedule to create scheduled tasks in node projects

青灯夜游
Release: 2021-11-17 19:29:17
forward
3452 people have browsed it

nodeHow to create a scheduled task in the project? The following article will introduce to you how to use Node Schedule to develop scheduled task scripts in node projects. I hope it will be helpful to you! The backend of

How to use Node Schedule to create scheduled tasks in node projects

nodejs also needs scheduled tasks to be processed, such as backup, scheduled email sending, settlement and other operations, so the npm Node Schedule is used Help us develop scheduled task scripts.

npm install node-schedule
Copy after login

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.

Job The object is an `EventEmitter and emits the following events:

  • runEvents after each execution.
  • scheduledEvents each time the plan is run.
  • acanceled, when a canceled event is called before it is executed.
  • A error is thrown when a scheduled job call is triggered or the exit event rejects the Promise.

(Both the scheduled and canceled events receive a JavaScript date object as a parameter). Note that the task is executed immediately for the first time, so if you use the scheduleJob() method to create a task, you will miss the first scheduled event trigger, but you can manually query the call .

Cron format

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

每分钟的第30秒触发: '30 * * * * *'

每小时的1分30秒触发 :'30 1 * * * *'

每天的凌晨1点1分30秒触发 :'30 1 1 * * *'

每月的1日1点1分30秒触发 :'30 1 1 1 * *'

2016年的1月1日1点1分30秒触发 :'30 1 1 1 2016 *'

每周1的1点1分30秒触发 :'30 1 1 * * 1'
Copy after login

Here is my personal use of the node-schedule encapsulation class to add, delete, modify and check scheduled tasks

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

exports.Interval = class Interval {
  constructor({ unit_name, maintain_time, last_alarm }) {
    this.unit_name = unit_name          // 任务名字
    this.maintain_time = maintain_time  // 定时时间
    this.last_alarm = last_alarm || ""        // 上一次定时任务名字
  }

  // 生成新的定时任务
  async create(callback) {
    // 终止之前的定时任务
    if (this.last_alarm !== "") {
      this.delete(this.last_alarm)
    }
    schedule.scheduleJob(`${this.unit_name}`, `${this.maintain_time}`, callback);
  }

  // 删除定时任务
  delete() {
    if (schedule.scheduledJobs[this.unit_name]) {
      schedule.scheduledJobs[this.unit_name].cancel();
      return true
    }
    return false
  }

  // 找到一个定时任务
  findOne(name) {
    if (schedule.scheduledJobs[name]) {
      return schedule.scheduledJobs[name]
    } else {
      throw new Error("未找到任务名")
    }
  }

  // 查看所有的定时任务
  findAll() {
    return schedule.scheduledJobs
  }
}
Copy after login

Here is the scheduled task Interval instance when calling

// 定时任务
new Util.Interval({
  unit_name: '自动分发任务 0 0 12 * * *',
  maintain_time: '0 0 12 * * *',
  last_alarm: '自动分发任务 0 0 12 * * *'
}).create(async () => {
  // 写入你自己想在定时任务触发的时候,想要执行的函数
})
Copy after login

For more node-related knowledge, please visit: nodejs tutorial! !

The above is the detailed content of How to use Node Schedule to create scheduled tasks in node projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template