maintenance-bot/modules/checkers/health-checker.js
2022-05-28 16:29:45 +02:00

56 lines
1.8 KiB
JavaScript

import getLogger from "../../libs/logger.js"
import settings from "../../libs/settings.js"
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(
(resolve, reject) => {
const req = https.request({
hostname: serviceDomain,
port: 443,
path: "/",
method: "GET"
}, res => {
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())
req.end()
}
)
}
export function start(sendMessage) {
log("Starting up the health checker...")
log("Services to watch:", settings.checkers["health-checker"].services)
function checkServices(serviceDomains) {
for (const serviceDomain of serviceDomains) {
checkService(serviceDomain).then(
() => log(`Service '${serviceDomain}' seems to be running!`)
).catch(
() => {
const message = `**IMPORTANT!** Service '${serviceDomain}' seems to have stopped!`
log(message)
sendMessage(message)
}
)
}
}
checkServices(settings.checkers["health-checker"].services)
setInterval(
() => checkServices(settings.checkers["health-checker"].services),
(settings.checkers["health-checker"].interval ?? 300) * 1000
)
return
}