From 5d4a3baef35c8dcde2a78214c4dee836a9275726 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 28 May 2022 16:29:45 +0200 Subject: [PATCH] Fixed health checker. --- modules/checkers/health-checker.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/checkers/health-checker.js b/modules/checkers/health-checker.js index fc97522..151c67d 100644 --- a/modules/checkers/health-checker.js +++ b/modules/checkers/health-checker.js @@ -5,6 +5,7 @@ import https from "https" const log = getLogger("Health Checker", settings.checkers["health-checker"].color) +// This function is a mess -- I know. function checkService(serviceDomain) { log(`Checking service '${serviceDomain}'`) return new Promise( @@ -15,10 +16,12 @@ function checkService(serviceDomain) { path: "/", method: "GET" }, res => { - if (res.statusCode === 200) resolve(`Service '${serviceDomain}' seems to be running!`) - res.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`)) + res.on("error", () => reject()) + res.on("end", () => res.statusCode === 200 ? resolve() : reject()) + res.on("close", () => res.statusCode === 200 ? resolve() : reject()) + return res.statusCode === 200 ? resolve() : reject() }) - req.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`)) + req.on("error", () => reject()) req.end() } ) @@ -31,11 +34,12 @@ export function start(sendMessage) { function checkServices(serviceDomains) { for (const serviceDomain of serviceDomains) { checkService(serviceDomain).then( - status => log(status) + () => log(`Service '${serviceDomain}' seems to be running!`) ).catch( - status => { - log(status) - sendMessage(status) + () => { + const message = `**IMPORTANT!** Service '${serviceDomain}' seems to have stopped!` + log(message) + sendMessage(message) } ) }