-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
57 lines (48 loc) · 1.35 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
55
56
const config = require('./config.json')
const mailer = require('./sendMail')
var retry = require('retry');
var request = require('request');
var cron = require('node-cron');
process.on('uncaughtException', function (err) {
console.error(err);
console.log("Node NOT Exiting...");
});
const rpcURL = config.rpcURL;
var numberOfRetries = config.numberOfRetries;
var count = 0
var isMailSend = false;
var getBlock = {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1};
cron.schedule('*/15 * * * * *', () => {
console.log('Pinging in every 15 sec');
pingRequest(rpcURL,function(err, result) {
if (err || result == "" || result == undefined) { // TODO ask for failure condition or check for positve case
manageCount(true);
}else{
manageCount(false);
}
});
});
function pingRequest(rpcURL , cb){
request.post({
headers: {'content-type' : 'application/json'},
url: rpcURL,
body: JSON.stringify(getBlock)
}, function(error, response, body){
cb(error , body);
});
}
function manageCount(isError){
if(isError){
count++;
if(count > numberOfRetries && isMailSend == false){
mailer.send();
isMailSend = true;
}
}
else {
count = 0;
if(isMailSend){
isMailSend = false;
}
}
}