Nodejs Scheduled Tasks

Source: Internet
Author: User
Tags install node npm install node

In a real-world development project, you will encounter many tasks that are scheduled to work. Such as: the timing of the export of certain data, scheduled to send messages or messages to users, scheduled backup of what type of files, etc.

It is generally possible to write a timer to complete the corresponding requirements, which is very easy to implement in node. js, and the next step is to Node-schedule to complete the scheduled tasks.

Here is an example to illustrate the use of node-schedule.

Node-schedule:https://github.com/node-schedule/node-schedule

Installation:

NPM Install Node-schedule

Cron Style Timer

var schedule = require (' Node-schedule '); Functionschedulecronstyle () {

Schedule.schedulejob (' * * * * * * ', function () {

Console.log (' Schedulecronstyle: ' +new Date ());

});

}

Schedulecronstyle ();

Write the task code to execute in the callback function of Schedule.schedulejob, and a timer is finished!

Now let's talk about what the cron-style timer's incoming parameters represent, and first look at the results above, such as

From the output can be seen, the incoming ' 30 * * * * * ' result is 30 seconds per minute will be executed, the following to see this incoming parameter code what

Wildcard interpretation

*  *  *  *  *  *

┬┬┬┬┬┬

│││││|

│││││└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)

6 placeholders are represented from left to right: seconds, minutes, hours, days, months, weeks

' * ' indicates a wildcard, matches any, when the second is ' * ', indicates that any number of seconds are triggered, other analogy

Below you can see the meaning of the following incoming parameters, respectively

Trigger at 30th seconds per minute: ' 30 * * * * * '

1 minutes 30 seconds per hour trigger: ' 30 1 * * * * '

Daily 1:1 A.M. 30 seconds Trigger: ' 30 1 1 * * * '

Every 1st 1:1 30 seconds Trigger: ' 30 1 1 1 * * '

2016 January 1 1:1 30 seconds Trigger: ' 30 1 1 1 2016 * '

1 per week, 1 points, 1 minutes, 30 seconds, Trigger: ' 30 1 1 * * 1 '

This is easy to implement with a short code based on your own needs.

Cron Style timer-range trigger

You can also pass in the scope in the preceding parameter placeholders, such as the following example

var schedule = require (' Node-schedule ');

function Schedulecronstyle () {

Schedule.schedulejob (' * * * * * * ', function () {

Console.log (' Schedulecronstyle: ' + new Date ());

});

}

Schedulecronstyle ();

Results such as:

From the output, it can be seen that 1-10 seconds per minute will trigger,

Other occupiers use the same method, and the input range can be seen with reference to the preceding "wildcard interpretation"

Recursive rule Timers

And look at another style. Write timer

var schedule = require (' Node-schedule ');

function Schedulerecurrencerule () {

var rule = new schedule. Recurrencerule ();

Rule.dayofweek = 2;

Rule.month = 3;

Rule.dayofmonth = 1;

Rule.hour = 1;

Rule.minute = 42;

Rule.second = 0;

Schedule.schedulejob (rule, function () {

Console.log (' Schedulerecurrencerule: ' + new Date ());

});

}

Schedulerecurrencerule ();

Results such as:

From the results can be seen, the 60th seconds per minute will be triggered, other rules can see my comments in the code, of course, can also be combined to achieve the demand effect!

Object Text Grammar Timer

See Using examples directly

var schedule = require (' Node-schedule ');

function Scheduleobjectliteralsyntax () {

DayOfWeek

Month

DayOfMonth

Hour

Minute

Second

Schedule.schedulejob ({hour:16, minute:11, dayofweek:1}, function () {

Console.log (' Scheduleobjectliteralsyntax: ' + new Date ());

});

}

Scheduleobjectliteralsyntax ();

Results such as:

The code implements a 16:11-minute trigger every Monday, and other combinations can be freely combined according to the annotation parameter names in my code.

Cancel Timer

example, the Cancl method of the timer object can be

var schedule = require (' Node-schedule ');

function Schedulecancel () {

var counter = 1;

var j = schedule.schedulejob (' * * * * * * ', function () {

Console.log (' Timer trigger number: ' + counter ');

counter++;

});

SetTimeout (function () {

Console.log (' timer cancellation ')

J.cancel ();

}, 5000);

}

Schedulecancel ();

The results are as follows:

After the writing

Most of the requirements of the timer function can be completed with the help of Node-schedule, using it in the project is also a good effect, a variety of requirements can meet the ^_^!

This article was reproduced from: http://www.cnblogs.com/zhongweiv/p/node_schedule.html

Nodejs Scheduled Tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.