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

Avoid crash and notice not found contacts to admins

parent eb05384a
Branches
No related tags found
No related merge requests found
...@@ -73,7 +73,7 @@ const logger = require('./logger'); ...@@ -73,7 +73,7 @@ const logger = require('./logger');
*/ */
exports.getContactDetails = async (offlineValidatorsArray) => { exports.getContactDetails = async (offlineValidatorsArray) => {
logger.log('Offline validators are: ', offlineValidatorsArray); logger.log('Offline validators are: ', offlineValidatorsArray);
let result = []; let offlineContacts = [];
// Prepare data // Prepare data
let techies = await readCSVtoJson(TECHIE_FILE); let techies = await readCSVtoJson(TECHIE_FILE);
...@@ -83,7 +83,7 @@ exports.getContactDetails = async (offlineValidatorsArray) => { ...@@ -83,7 +83,7 @@ exports.getContactDetails = async (offlineValidatorsArray) => {
let groupedConsortium = groupContactsByInstitution(consortium); let groupedConsortium = groupContactsByInstitution(consortium);
console.log(groupedTechies) console.log(groupedTechies)
let noContacts = [] let notFoundContacts = []
for (validator of offlineValidatorsArray) { for (validator of offlineValidatorsArray) {
let address = validator.address; let address = validator.address;
let institution = addressInstitutionMap[address]; let institution = addressInstitutionMap[address];
...@@ -96,22 +96,22 @@ exports.getContactDetails = async (offlineValidatorsArray) => { ...@@ -96,22 +96,22 @@ exports.getContactDetails = async (offlineValidatorsArray) => {
} else if (groupedConsortium[institution]) { // all other contacts we have } else if (groupedConsortium[institution]) { // all other contacts we have
contacts = groupedConsortium[institution]; contacts = groupedConsortium[institution];
} else { // no contacts } else { // no contacts
noContacts.push(address); notFoundContacts.push({ institution, address });
} }
// Add address field to contact object // Add address field to contact object
let contactsWithAddress = contacts && contacts.map(contact => { // if contacts undefined, throw below. if (contacts) {
let contactsWithAddress = contacts.map(contact => { // if contacts undefined, throw below.
return { return {
address: address, address: address,
lastOnline: validator.lastOnline, lastOnline: validator.lastOnline,
...contact ...contact
} }
}) })
result.push(contactsWithAddress); offlineContacts.push(contactsWithAddress);
} }
if (noContacts.length > 0) }
throw new Error('Couldnt find contacts for addresses ' + noContacts.join()) return { offlineContacts, notFoundContacts };
return result;
} }
/** /**
... ...
......
...@@ -41,6 +41,7 @@ exports.sendNoticeMails = (contactsArray) => { ...@@ -41,6 +41,7 @@ exports.sendNoticeMails = (contactsArray) => {
* @param {Error} error - the thrown Error object * @param {Error} error - the thrown Error object
*/ */
exports.sendErrorEmails = (emails, error) => { exports.sendErrorEmails = (emails, error) => {
logger.log('Sending error emails to ' + emails.join() + 'about: ' + error)
const message = { const message = {
from: `bloxberg Validator Monitoring <monitoring@bloxberg.org>`, from: `bloxberg Validator Monitoring <monitoring@bloxberg.org>`,
to: emails, to: emails,
...@@ -54,6 +55,33 @@ exports.sendErrorEmails = (emails, error) => { ...@@ -54,6 +55,33 @@ exports.sendErrorEmails = (emails, error) => {
return transport.sendMail(message); return transport.sendMail(message);
} }
/**
* 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.sendNotFoundEmails = (emails, notFoundContacts) => {
logger.log('Sending not found emails to ' + emails.join() + ' for the addresses: ' + notFoundContacts.map(contact => contact.address).join())
const message = {
from: `bloxberg Validator Monitoring <monitoring@bloxberg.org>`,
to: emails,
subject: '❗ Contact Not Found: bloxberg Validator Offline',
text: `The following validators do not have an assigned contact and
hence were not able to be contacted. If the institution name is undefined,
it means an associated institution for the address couldn't be found in Cobra input.
Otherwise the institution has an associated address but is missing any contacts. \n\n` +
notFoundContacts.map(contact => {
return `Institution: ${contact.institution} \n\n
Address: ${contact.address}\n\n`
}).join()
};
// 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. * @function to send a notice email to the validator input.
* *
... ...
......
const { getValidatorArray } = require('./validators'); const { getValidatorArray } = require('./validators');
const { sendNoticeMails, sendErrorEmails } = require('./email'); const { sendNoticeMails, sendErrorEmails, sendNotFoundEmails } = require('./email');
const { getContactDetails } = require('./contacts'); const { getContactDetails } = require('./contacts');
const schedule = require('node-schedule'); const schedule = require('node-schedule');
const logger = require('./logger'); const logger = require('./logger');
...@@ -28,12 +28,15 @@ function checkValidatorsAndSendEmails() { ...@@ -28,12 +28,15 @@ function checkValidatorsAndSendEmails() {
logger.log("Got the online status for validators: "); logger.log("Got the online status for validators: ");
return getContactDetails(offlineValidatorsArray); return getContactDetails(offlineValidatorsArray);
}) })
.then(offlineContacts => { .then(({ offlineContacts, notFoundContacts }) => {
return sendNoticeMails(offlineContacts); return Promise.all([
sendNoticeMails(offlineContacts),
sendNotFoundEmails(ERROR_CONTACTS, notFoundContacts)
]);
}) })
.catch(err => { .catch(err => {
logger.error("SOMETHING WENT WRONG") logger.error("SOMETHING WENT WRONG")
logger.error(err) logger.error(err.stack)
sendErrorEmails(ERROR_CONTACTS, err); sendErrorEmails(ERROR_CONTACTS, err);
}) })
} }
...@@ -19,7 +19,7 @@ const fileLogOptions = { ...@@ -19,7 +19,7 @@ const fileLogOptions = {
const mainLoggers = logger.createLogger({ const mainLoggers = logger.createLogger({
level: 'info', level: 'info',
format: format.combine( format: format.combine(
format.metadata({ fillExcept: ['timestamp', 'level', 'message'] }) format.metadata({ fillExcept: ['timestamp', 'level', 'message', 'stack'] })
), ),
transports: [ transports: [
new transports.DailyRotateFile({ new transports.DailyRotateFile({
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment