Skip to content
Snippets Groups Projects
Commit eb05384a authored by kuzdogan's avatar kuzdogan
Browse files

Send error email if fails

parent 29c6fa4a
Branches
No related tags found
No related merge requests found
......@@ -2,3 +2,6 @@ SMTP_HOST=
SMTP_PORT=
SMTP_USER=
SMTP_PASS=
# set to development if developing.
# NODE_ENV=development
\ No newline at end of file
......@@ -82,28 +82,35 @@ exports.getContactDetails = async (offlineValidatorsArray) => {
let groupedTechies = groupContactsByInstitution(techies);
let groupedConsortium = groupContactsByInstitution(consortium);
console.log(groupedTechies)
let noContacts = []
for (validator of offlineValidatorsArray) {
let address = validator.address;
let institution = addressInstitutionMap[address];
logger.info('Processing contacts for institution with address ' + validator.address + ' and name ' + institution)
let contacts; // of this validator
// If there's a techie for the institution add them as contact
if (groupedTechies[institution]) {
contacts = groupedTechies[institution]; // ref not copy but OK
} else { // all other contacts we have
contacts = groupedConsortium[institution]; // ref not copy but OK
contacts = groupedTechies[institution];
} else if (groupedConsortium[institution]) { // all other contacts we have
contacts = groupedConsortium[institution];
} else { // no contacts
noContacts.push(address);
}
// Add address field to contact object
let contactsWithAddress = contacts.map(contact => {
let contactsWithAddress = contacts && contacts.map(contact => { // if contacts undefined, throw below.
return {
address: address,
lastOnline: validator.lastOnline,
...contact
}
})
result.push(contactsWithAddress);
}
if (noContacts.length > 0)
throw new Error('Couldnt find contacts for addresses ' + noContacts.join())
return result;
}
......@@ -171,7 +178,7 @@ function mapAddressToInsitutions(contactsArray) {
for (contact of contactsArray) {
if (contact.haupt === 'H' && ethereumRegex().test(contact.comments)) {
let addr = contact.comments.match(ethereumRegex());
result[addr] = contact.institution;
result[addr] = contact.institution; // 0xEafe556569895f555755815131D21D49AFdb2Efe": "Universität Zürich"
}
}
return result;
......
......@@ -34,6 +34,26 @@ exports.sendNoticeMails = (contactsArray) => {
return Promise.all(promises);
}
/**
* Function to send error emails to admins when the script fails.
*
* @param {Array} emails - array of email strings as receivers
* @param {Error} error - the thrown Error object
*/
exports.sendErrorEmails = (emails, error) => {
const message = {
from: `bloxberg Validator Monitoring <monitoring@bloxberg.org>`,
to: emails,
subject: '❗ ERROR: bloxberg Validator Offline',
text: `When running the script the following error is encountered\n\n
${error.message}\n\n
${error.stack}`
};
// return setTimeout(() => Promise.resolve(`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
return transport.sendMail(message);
}
/**
* @function to send a notice email to the validator input.
*
......
const { getValidatorArray } = require('./validators');
const { sendNoticeMails } = require('./email');
const { sendNoticeMails, sendErrorEmails } = require('./email');
const { getContactDetails } = require('./contacts');
const schedule = require('node-schedule');
const logger = require('./logger');
const ERROR_CONTACTS = ['lawton@mpdl.mpg.de', 'uzdogan@mpdl.mpg.de']
// Set the schedule to run in cron format see helper https://crontab.guru/
// Format:
// minute hour day month day-of-week
......@@ -13,9 +14,12 @@ const logger = require('./logger');
// 0 13 * * 1 ==> every Monday at 13:00
// 0 13 */5 * * ==> every fifth day of the month at 13:00 5th, 10th, 15th...
//
schedule.scheduleJob('0 13 * * 1', checkValidatorsAndSendEmails);
if (process.env.NODE_ENV === 'development') {
// schedule.scheduleJob('*/5 * * * * *', () => logger.log('Hi'));
checkValidatorsAndSendEmails()
} else {
schedule.scheduleJob('0 13 * * 1', checkValidatorsAndSendEmails);
}
function checkValidatorsAndSendEmails() {
getValidatorArray()
......@@ -30,5 +34,6 @@ function checkValidatorsAndSendEmails() {
.catch(err => {
logger.error("SOMETHING WENT WRONG")
logger.error(err)
sendErrorEmails(ERROR_CONTACTS, err);
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment