diff --git a/app.js b/app.js index b303bd9..214caf4 100644 --- a/app.js +++ b/app.js @@ -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); + }); } }); } @@ -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); } } @@ -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(); diff --git a/package-lock.json b/package-lock.json index 924a74d..336020c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,11 +13,6 @@ "is-buffer": "^2.0.2" } }, - "bluebird": { - "version": "3.7.2", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", - "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" - }, "debug": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",