Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do scheduled tasks in Nodejs

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to do scheduled tasks in Nodejs". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to do scheduled tasks in Nodejs.

At present, we have encountered such a demand that each user can customize the timing push, and the time configuration of the timing task is configured by the user and modified at any time, instead of the traditional server setting the time to run the timing task.

If the cycle of the timing task is known, then the Linux Crontab timing task can be realized. There is also a relatively mature scheme in node. The following will be my own understanding of the Node timed task program to do an introduction.

Schduler scheduled tasks provided by egg framework

Egg is Ali's Node enterprise application framework, it provides a mechanism for developers to write scheduled tasks elegantly, refer to the official document: https://eggjs.org/zh-cn/basics/schedule.html, here do not elaborate on the specific usage, just briefly talk about its configuration and usage.

1. Static timing task, that is, timing cycle, processing logic is written to death.

two。 Dynamic configuration of scheduled tasks, which is relatively flexible, developers can control the scheduled task cycle through the configuration file.

3. By manually triggering the timing task by the code, we can judge whether to trigger the timing task according to some logic.

From the above three points, the timing tasks provided by egg can not meet the needs of users to configure their own timing cycle, so we can only look for other solutions. Through the query, we found that the library of node-schedule also provides the function of timing tasks.

Node-schedule

After browsing node-schudule 's document https://www.npmjs.com/package/node-schedule, his usage is as follows:

Const schedule = require ('node-schedule'); const job = schedule.scheduleJob (' 42 *', function () {console.log ('The answer to life, the universe, and everything');})

Yes, he supports custom timing cycles, which can meet our needs and can be used as our choice, Nice!

Continue to browse the usage articles of other developers and find that there is a record of problems related to node-schedule, that is, the timer was executed twice at the scheduled time. For specific problems, see the original text: https://cnodejs.org/topic/5b58414b8d66b90b2b7384a7. This is a low probability problem, we did not do verification, because our requirements, timely timer executed twice, does not affect our results, so this is not an obstacle to our use of it, interested students can test bug.

Write at the end

Finally, here is a summary of our own project timing task solution, which can be skipped by students who are not interested.

Our backend uses the egg framework, so our final solution is to add node-schdule timers to the agent process, and users' timers will be executed in synchronous agent processes. At present, we only have a small number of users, and we only think of this solution for the time being. Later, we may deploy the background in a distributed manner.

This can be a problem, so can a large number of single-process runs, and egg itself does not recommend doing too much business-related stuff in the agent process. Another important thing is that agent is a helper process. Some of the common work of work is actually done in agent. Uncaught errors in agent will not quit and restart, so fault tolerance must be done and done well.

At this point, I believe you have a deeper understanding of "how to do scheduled tasks in Nodejs". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report