Implementation of task scheduling and execution in node. JS is not allowed to be reproduced without CHSZS. Permission to reprint should be marked by the author and blog home: Http://blog.csdn.net/chszs
Batch processing is a common requirement in business development, such as bank statement processing, such as the push of ad mail.
node. JS also has some excellent libraries for batch processing, and Node-schedule is one of them.
Node-schedule is a lightweight, node. JS-based, cron-like scheduler tool. Its introduction can be viewed: https://www.npmjs.com/package/node-schedule
Node-schedule allows the execution of arbitrary tasks (arbitrary functions) at a specified date time, and also supports circular rules. Only a single timer is used inside the node-schedule.
Node-schedule is a time-based scheduler, not a time-interval-based scheduler. If you have the need to "run a function every 5 minutes", you will find it more appropriate to use time-interval-based scheduling, such as SetInterval (), which is very handy. If your needs are "to run a function once in the 5th and 50th minutes of every hour", you will find the time-based scheduling method more appropriate.
Also note that, unlike Cron, node-schedule can be perfectly supported on Windows systems.
It is important to note that the Node-schedule is designed to be used for inter-process scheduling, that is, the scheduled job will run the relevant script at the time of dispatch, and the dispatch will disappear when the script executes. If you want to keep scheduling jobs when the script is not running, it is best to use cron.
In Node-schedule, each scheduled job is described using the Job object. You can create the job manually, and then execute the schedule () method to apply the schedule, or use Schedulejob ().
The Job object is Eventemitter, issuing a run event each time the dispatch executes. When the scheduled time arrives, it also issues the scheduled event. The cancellation event can be canceled before the dispatch starts. These events all receive the JavaScript date object as a parameter.
Note that the job is executed immediately when it is first dispatched, so if you use Schedulejob () to create the job, you lose the event that was dispatched immediately for the first time.
Also note that the cancellation event is canceled, and the expression is used in American English.
To install Node-schedule is simple, execute:
npm install node-schedule
If you've ever used cron to understand the format of a cron expression, it's easier to use it.
Cron-style scheduling
The cron format consists of the following:
* * * * * *┬ ┬ ┬ ┬ ┬ ┬│ │ │ │ │ |│ │ │ │ │ └ 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)
Let's look at an example:
var scheduler = require(‘node-schedule‘);var montlyJob = scheduler.scheduleJob(‘0 0 1 * *‘, function(){ console.log(‘I run the first day of the month‘);});
There are also ways to use JavaScript objects:
var scheduler = require(‘node-schedule‘);var rule = new scheduler.RecurrenceRule();rule.hour = 7rule.dayOfWeek = new scheduler.Range(0,6)var dailyJob = scheduler.scheduleJob(date, function(){ console.log(‘I run on days at 7:00‘);});scheduler.scheduleJob(rule,task);
You can also schedule a task by skipping the specified date:
var scheduler = require(‘node-schedule‘);var date = new Date(2017, 1, 1, 0, 0, 0);var newYearJob = scheduler.scheduleJob(date, function() { console.log("Happy new year");});
The job to cancel the dispatch is also very simple, such as:
newYearJob.cancel();
Implementing task scheduling and execution in node. js