Last active
January 31, 2019 11:49
-
-
Save buddies2705/d14c7864799a19e7e2fa4e1961b7c1f8 to your computer and use it in GitHub Desktop.
Full repo https://github.com/buddies2705/Eth-Ping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3') | |
const rpcURL = "Your_node_url" | |
var web3 = new Web3(new Web3.providers.HttpProvider(rpcURL)); | |
var nodemailer = require('nodemailer'); | |
var count = 0 | |
var isMailSend = false; | |
var numberOfRetries = 3; | |
var cron = require('node-cron'); | |
//adjust cron timing | |
cron.schedule('*/10 * * * * *', () => { | |
console.log('running a task in every 10 sec'); | |
ping(); | |
}); | |
// create reusable transporter object using SMTP transport | |
var transporter = nodemailer.createTransport({ | |
service: 'Gmail', | |
auth: { | |
user: 'your_email', | |
pass: 'Your_email_password' | |
} | |
}); | |
//You need to allow less secure app if you want to use Gmail mail service. | |
//Follow this doc (https://support.google.com/accounts/answer/6010255?hl=en) | |
// and Change account access for less secure apps | |
var mailOptions = { | |
from: 'gaurav@coinmonks.com', // sender address | |
to: 'gaurav@coinmonks.com', // list of receivers | |
subject: 'Fire In the Hole ✔ , Your node is not responding', // Subject line | |
text: 'You node is not responding', // plaintext body | |
html: 'your node is not responding' // html body | |
}; | |
// send mail with defined transport object | |
function send(){ | |
transporter.sendMail(mailOptions, function(error, info){ | |
if(error){ | |
console.log(error); | |
}else{ | |
console.log('Message sent: ' + info.response); | |
} | |
}); | |
} | |
function ping(){ | |
web3.eth.getBlockNumber(function(err, result){ | |
if(err){ | |
count++; | |
if(count > numberOfRetries && isMailSend == false){ | |
send(); | |
isMailSend = true; | |
} | |
} | |
else { | |
count = 0; | |
if(isMailSend){ | |
isMailSend = false; | |
} | |
console.log(result); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment