Fixed health checker.

This commit is contained in:
BurnyLlama 2022-05-28 16:29:45 +02:00
parent dffd4b0507
commit 5d4a3baef3

View File

@ -5,6 +5,7 @@ import https from "https"
const log = getLogger("Health Checker", settings.checkers["health-checker"].color) const log = getLogger("Health Checker", settings.checkers["health-checker"].color)
// This function is a mess -- I know.
function checkService(serviceDomain) { function checkService(serviceDomain) {
log(`Checking service '${serviceDomain}'`) log(`Checking service '${serviceDomain}'`)
return new Promise( return new Promise(
@ -15,10 +16,12 @@ function checkService(serviceDomain) {
path: "/", path: "/",
method: "GET" method: "GET"
}, res => { }, res => {
if (res.statusCode === 200) resolve(`Service '${serviceDomain}' seems to be running!`) res.on("error", () => reject())
res.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`)) 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() req.end()
} }
) )
@ -31,11 +34,12 @@ export function start(sendMessage) {
function checkServices(serviceDomains) { function checkServices(serviceDomains) {
for (const serviceDomain of serviceDomains) { for (const serviceDomain of serviceDomains) {
checkService(serviceDomain).then( checkService(serviceDomain).then(
status => log(status) () => log(`Service '${serviceDomain}' seems to be running!`)
).catch( ).catch(
status => { () => {
log(status) const message = `**IMPORTANT!** Service '${serviceDomain}' seems to have stopped!`
sendMessage(status) log(message)
sendMessage(message)
} }
) )
} }