How to do the Node scheduled task

background

At present, we have met such a requirement that each user can customize the timing push. The time configuration of the scheduled task can be configured by the user and modified at any time, instead of the traditional time running scheduled task set by the server.

If the scheduled task cycle is known, the Linux Crontab scheduled task can be implemented. There are already relatively mature solutions in Node. The following is an introduction to the Node scheduled task scheme that I have learned.

Schduler timed tasks provided by the Egg framework

Egg is alibaba’s Node enterprise application framework, which provides a set of mechanisms for developers to gracefully write scheduled tasks. For details, please refer to the official documentation: eggjs.org/zh-cn/basic… , here do not elaborate on the specific usage, just a brief description of its configuration.

1. Static scheduled tasks, that is, periodic tasks, process logical write failures.

2. Dynamically configure scheduled tasks, which is relatively flexible. Developers can control the duration of scheduled tasks through configuration files.

3. You can determine whether to trigger the scheduled task based on some logic by manually triggering the scheduled task through codes.

From the above three points, the scheduled task provided by Egg cannot meet our requirement that users configure the scheduled period by themselves, so we have to seek other solutions. Through data query, we find that the library of Node-Schedule also provides the function of scheduled task.

node-schedule

In the browse node – schudule documents www.npmjs.com/package/nod… After that, he used it as follows:

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

Yes, it supports custom timing periods that can meet our needs and can be used as our option, Nice!

There is a problem with node-schedule. The timer executes twice at a predetermined time. For details, see cnodejs.org/topic/5b584… . This is a problem of low probability, we did not do verification, because our requirement that the timely timer be executed twice has no impact on our results, so this is not an obstacle to our use of it, interested students can go to test the bug.

Write in the last

Finally, here is a summary of our own project timing task solutions, students who are not interested can skip.

Therefore, our final solution is to add the Timer of Node-schdule to the Agent process, and all the timers of users will be executed synchronously in the Agent process. At present, our number of users is not large, so we only think of this solution for the time being, and we may deploy the background distributed after the volume is increased. This can be a problem, as can large CDR runs, and egg itself does not recommend doing much business in the Agent process. Another important thing is that agent is an auxiliary process, and some public work of work is done in agent. Agent will not exit and restart when uncaught errors occur, so fault tolerance processing must be done and done well.

If you have a better plan, feel free to leave a comment and let us know.