Skip to content

Commit

Permalink
Fixed unhandled exception in the refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberinferno committed Dec 24, 2019
1 parent bda4545 commit 16c3c90
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
39 changes: 19 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ function startMonitoring() {

function checkAndNotifyAllServices() {
config['services'].forEach(service => {
let result = {};
if(service.host && service.port && service.name) {
if (service.host && service.port && service.name) {
console.log(`Checking ${service.name} connectivity...`);
try {
result = checkConnection(service);
console.log(`${result.name} connectivity check succeeded!`);
if(notificationLog[result.name]) {
delete notificationLog [result.name];
}

} catch (err) {
console.log(`${result.name} connectivity check failed!`);
notifyFailure(result);
}
checkConnection(service.name, service.host, service.port)
.then(result => {
console.log(`${result.name} connectivity check succeeded!`);
if (notificationLog[result.name]) {
delete notificationLog[result.name];
}
}).catch(result => {
console.log(`${result.name} connectivity check failed!`);
notifyFailure(result);
});
}
});
}
Expand All @@ -70,22 +68,23 @@ function notifyFailure(data) {
}
notificationLog[data.name] = currentTime[0];

const notificationMsg = `@here ${data.name} service with host ${data.host} \
and port ${data.port} has gone down!`;
const notificationMsg = `@here ${data.name} service with host ${data.host}\
and port ${data.port} has gone down!`;
if (config['discord_webhooks'] && config['discord_webhooks'].length) {
config['discord_webhooks'].forEach(webhook => discordNotification(webhook, notificationMsg));
}

if (config['notify_emails'] && config['notify_emails'].length) {
config['notify_emails'].forEach(email => emailNotification(email, notificationMsg));
config['notify_emails'].forEach(email => emailNotification(email, notificationMsg, notificationMsg));
}
}

async function discordNotification(webhookUrl, content) {
try {
await axios.post(webhookUrl, { content: content });
}
catch(err) {
await axios.post(webhookUrl, {
content: content
});
} catch (err) {
console.log(err);
}
}
Expand All @@ -106,7 +105,7 @@ function checkConnection(serviceName, host, port) {
return new Promise((resolve, reject) => {
const connectionTimeout = config['connection_timeout'];
const timeout = connectionTimeout ? connectionTimeout * MILISECONDS_TO_SECONDS_FACTOR :
DEFAULT_TIMEOUT_IN_SECONDS;
DEFAULT_TIMEOUT_IN_SECONDS;
const timer = setTimeout(() => {
reject('timeout');
socket.end();
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 16c3c90

Please sign in to comment.