nodejs 定時任務 node-schedule 庫
node-schedule 是一個基於時間的排程,而不是基於區間的排程。你可以很容易的讓他按照你的意思來幹活,比如,你說“每五分鐘來執行這個函式",你將發現setInterval
要更容易使用,也是更適合的。但是如果你想說"執行這個函式在每個月的第三個星期二每個小時的20分和50分",你會發現你更想要Node Schedule元件。此外,Node Schedule 支援windows系統,不像cron並不支援。
注意 Node Schedule 是被設計來進行程序內排程,也就是說排程任務只能在你的指令碼執行時才能有效以及排程將在執行成功後消失。如果你需要在你腳步不執行的時候排程任務,那就需要考慮使用cron.
任務和排程
每個在Node Schedule的計劃任務都會被一個Job
物件所代表,你可手動建立任務,然後執行schedule()
方法來應用一個計劃,或者使用一個方便的方法ScheduleJob()
就像下面要說的。
Job
物件是事件觸發器
,觸發一個run
事件在每次執行之後。
他們也觸發一個scheduled
事件,在每次他們排程執行的時候,canceled
事件可以讓一個呼叫在它執行之前被取消(這兩個事件都接受一個JavaScript日期物件作為一個引數). 注意這個任務會第一時間被排程,所以如果你使用scheduleJob()
這個方便的方法來建立一個任務,你將錯過第一個scheduled
事件,但是你能手動查詢呼叫(下面會有)。也要注意canceled
Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute).
https://github.com/node-schedule/node-schedule
安裝:
npm install node-schedule --save
或者yarn add node-schedule
用法
1、Cron風格定時器
const schedule = require('node-schedule');
const scheduleCronstyle = ()=>{
//每分鐘的第30秒定時執行一次:
schedule.scheduleJob('30 * * * * *',()=>{
console.log('scheduleCronstyle:' + new Date());
});
}
scheduleCronstyle();
schedule.scheduleJob的回撥函式中寫入要執行的任務程式碼,一個定時器就完成了!
規則引數講解 *代表萬用字元
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ 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個佔位符從左到右分別代表:秒、分、時、日、月、周幾
*
表示萬用字元,匹配任意,當秒是*
時,表示任意秒數都觸發,其它類推
下面可以看看以下傳入引數分別代表的意思
每分鐘的第30秒觸發: '30 * * * * *'
每小時的1分30秒觸發 :'30 1 * * * *'
每天的凌晨1點1分30秒觸發 :'30 1 1 * * *'
每月的1日1點1分30秒觸發 :'30 1 1 1 * *'
2016年的1月1日1點1分30秒觸發 :'30 1 1 1 2016 *'
每週1的1點1分30秒觸發 :'30 1 1 * * 1'
每個引數還可以傳入數值範圍:
const task1 = ()=>{
//每分鐘的1-10秒都會觸發,其它萬用字元依次類推
schedule.scheduleJob('1-10 * * * * *', ()=>{
console.log('scheduleCronstyle:'+ new Date());
})
}
task1()
Recurrence Rule Scheduling
You can build recurrence rules to specify when a job should recur. For instance, consider this rule, which executes the function every hour at 42 minutes after the hour:
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = 42;
var j = schedule.scheduleJob(rule, function(){
console.log('The answer to life, the universe, and everything!');
});
You can also use arrays to specify a list of acceptable values, and theRange
object to specify a range of start and end values, with an optional step parameter. For instance, this will print a message on Thursday, Friday, Saturday, and Sunday at 5pm:
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
console.log('Today is recognized by Rebecca Black!');
});
RecurrenceRule properties
second (0-59)
minute (0-59)
hour (0-23)
date (1-31)
month (0-11)
year
dayOfWeek (0-6) Starting with Sunday
Note: It's worth noting that the default value of a component of a recurrence rule is
null
(except for second, which is 0 for familiarity with cron).If we did not explicitly setminute
to 0 above, the message would have instead been logged at 5:00pm, 5:01pm, 5:02pm, ..., 5:59pm.Probably not what you want.
使用方法
1:確定時間
例如:2014年2月14日,15:40執行
var schedule = require("node-schedule");
var date = new Date(2014,2,14,15,40,0);
var j = schedule.scheduleJob(date, function(){
console.log("執行任務");
});
取消任務
j.cancel();
2:每小時的固定時間
例如:每小時的40分鐘執行
var rule = new schedule.RecurrenceRule();
rule.minute = 40;
var j = schedule.scheduleJob(rule, function(){
console.log("執行任務");
});
3:一個星期中的某些天的某個時刻執行,
例如:週一到週日的20點執行
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(1, 6)];
rule.hour = 20;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
console.log("執行任務");
});
4:每秒執行
var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=1; i<60; i++){
times.push(i);
}
rule.second = times;
var c=0;
var j = schedule.scheduleJob(rule, function(){
c++;
console.log(c);
});