How to use Node Schedule to create scheduled tasks in node projects
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
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
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:
-
run
Events after each execution. -
scheduled
Events each time the plan is run. - a
canceled
, 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 thePromise
.
(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'
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 } }
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 () => { // 写入你自己想在定时任务触发的时候,想要执行的函数 })
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

ThinkPHP6 scheduled task scheduling: scheduled task execution 1. Introduction In the process of web application development, we often encounter situations where certain repetitive tasks need to be executed regularly. ThinkPHP6 provides a powerful scheduled task scheduling function, which can easily meet the needs of scheduled tasks. This article will introduce how to use scheduled task scheduling in ThinkPHP6, and provide some code examples to help understand. 2. Configure scheduled tasks, create scheduled task files, and create a comman in the app directory of the project.

In web development, many websites and applications need to perform certain tasks regularly, such as cleaning up junk data, sending emails, etc. In order to automate these tasks, developers need to implement task scheduling and timed task functions. This article will introduce how to implement task scheduling and timed tasks in PHP, as well as some commonly used third-party libraries and tools. 1. Task Scheduling Task scheduling refers to executing certain tasks according to specified times or events. In PHP, cron timer or similar mechanism can be used to implement task scheduling. Typically, task scheduling

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Python implements automatic page refresh and scheduled task function analysis for headless browser collection applications. With the rapid development of the network and the popularization of applications, the collection of web page data has become more and more important. The headless browser is one of the effective tools for collecting web page data. This article will introduce how to use Python to implement the automatic page refresh and scheduled task functions of a headless browser. The headless browser adopts a browser operation mode without a graphical interface, which can simulate human operation behavior in an automated way, thereby enabling the user to access web pages, click buttons, and fill in information.

How to use scheduled tasks in FastAPI to perform background work. With the rapid development of Internet applications, many applications have some background tasks that need to be executed regularly, such as data cleaning, email sending, backup, etc. In order to solve this problem, we can use scheduled tasks to automatically execute background work. In this article, we will introduce how to use scheduled tasks in the FastAPI framework to perform background work. FastAPI is a modern, fast (high-performance) web framework mainly used for building APIs. it has
