-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (47 loc) · 1.87 KB
/
index.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
var schedule = require('node-schedule');
var request = require('request');
function sendEvent () {
getUnprocessedEvents();
}
var getUnprocessedEvents = function(){
var eventsURL = process.env.EVENTS_URL;
request.get({
url: eventsURL,
json: true
},
function(error, response, events) {
if (!error && response.statusCode == 200) {
if (events.length > 0) {
events.forEach(function(event){
request.post({
url: event.url,
json: true
}, function(error, res){
if (!error && res.statusCode == 200){
console.log('ok to delete...');
request.delete({
url: eventsURL + '/' + event._id
}, function(err, res){
if (!err && res.statusCode == 204){
console.log('Event Delivered');
} else {
console.error('Problem to delete the event');
}
})
} else {
console.log('not ok to delete ...');
}
});
});
} else {
console.log('There are no events to process');
}
} else {
console.error(error);
}
}
);
};
//Initialze the schedule
schedule.scheduleJob('0 0 * * * *', sendEvent);
console.log('Schedule task to retry to send the event');