How to implement scheduled tasks in node
Node's method of implementing scheduled tasks: 1. Use setTimeOut and event events for management; 2. Sort all added events and calculate the time interval between the current time and the most recent event occurrence time; 3. Call Just set the callback with setTimeOut.
The operating environment of this tutorial: linux7.3 system, node18.4.0 version, Dell G3 computer.
How does node implement scheduled tasks?
node scheduled tasks (node-schedule module)
Realize a scheduled task every day at ten o'clock in the morning The function of updating the git code on the server at two points
app.js file under the koa2 framework
import schedule from 'node-schedule'; import updateCode from './controllers/hcpLan/fetch' const app = new Koa(); const router = new Router(); router.get( '/', (ctx, next) => { ctx.body = 'hello' }); app.use(router.routes()).use(router.allowedMethods()); let rule = new schedule.RecurrenceRule() /**每天的凌晨12点更新代码*/ rule.hour = 0 rule.minute = 0 rule.second = 0 /**启动任务*/ schedule.scheduleJob(rule, () => { updateCode.cloneRepo(); console.log('代码更新了!'); }) app.listen(3000)
node-schedule principle: Use setTimeOut and event events to manage and manage all added events Sort, and calculate the time interval between the current time and the most recent event occurrence time, and then call setTimeOut to set the callback. Generally speaking, there are two types of events, one is one-time and the other is periodic. The one-time task will end after being called, and the periodic task will be called continuously. When a periodic event is called, it will Generate the next periodic task based on the period, add it to the task list, and reorder it. At the end of each task call, the next task is calculated and prepared.
1. Setting the timer
node-schedule allows a variety of rules to implement timing
1. Cron style timer
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ 一周的星期 (0 - 7) (0 or 7 is Sun) │ │ │ │ └───── 月份 (1 - 12) │ │ │ └────────── 月份中的日子 (1 - 31) │ │ └─────────────── 小时 (0 - 23) │ └──────────────────── 分钟 (0 - 59) └───────────────────────── 秒 (0 - 59, OPTIONAL) var schedule = require('node-schedule'); //当分钟为42时,执行一个cron任务 var j = schedule.scheduleJob('42 * * * *', function(){ console.log('执行了!'); });
2.Date object Rule timer
var schedule = require('node-schedule'); var date = new Date('2017-09-26 22:00:00'); var j = schedule.scheduleJob(date, function(){ console.log('执行了!'); });
3.RecurrenceRule instance rule timer
var schedule = require('node-schedule'); var rule = new schedule.RecurrenceRule(); rule.minute = 42; var j = schedule.scheduleJob(rule, function(){ console.log('执行了!'); });
For specific usage, please view the github documentation https://github.com/node-schedule/node-schedule
Recommended learning: "nodejs video tutorial"
The above is the detailed content of How to implement scheduled tasks in node. 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.

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!

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

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
