-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.js
91 lines (73 loc) · 2.37 KB
/
scheduler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// --------------------------------------------------------------------------------------------
// Job Schedule (uses node-cron)
// --------------------------------------------------------------------------------------------
// '* * * * * *' - runs every second
// '*/5 * * * * *' - runs every 5 seconds
// '10,20,30 * * * * *' - run at 10th, 20th and 30th second of every minute
// '0 * * * * *' - runs every minute
// '0 0 * * * *' - runs every hour (at 0 minutes and 0 seconds)
// But also more complex schedules e.g.
// '00 30 11 * * 1-5' - Runs every weekday (Monday through Friday) at 11:30:00 AM. It does not run on Saturday or Sunday.
// Sample code: running job every 10 minutes:
// var cron = require('cron');
// var cronJob = cron.job("0 */10 * * * *", function(){
// // perform operation e.g. GET request http.get() etc.
// console.info('cron job completed');
// });
// cronJob.start();
var request = require('request')
, _ = require('lodash')
, moment = require('moment')
, time = require('time')
, cronJob = require('cron').CronJob
var delay = false;
function sendTestJob(){
console.log('sendTestJob....');
var testJob = {
method: 'POST'
, url: 'http://127.0.0.1:3600/job'
, json: {
"type": "testJob",
"data": { title: "send test job from scheduler" },
"options" : {
"attempts": 1
, "delay": 10000
}
}
};
// if(delay) {
// testJob.json.data.title = "send delayed testJob from scheduler";
// testJob.json.options.delay = 30000;
// }
request(testJob, function(error, response, body) {
if (error) {
console.log(error);
}
else {
console.log(body);
}
});
};
var heartbeat = new cronJob({
cronTime: '*/5 * * * * *', // send testjob every thirty seconds
onTick: function() {
console.log('');
console.log('****************************************');
console.log('heartbeat');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
console.log('****************************************');
sendTestJob();
// if(delay){
// delay = false;
// }
// else {
// delay = true;
// }
},
start: true
});
console.log('');
console.log('****************************************');
console.log('scheduler2.js started');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
console.log('****************************************');