maintenance-bot/modules/checkers/health-checker.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-05-28 15:03:43 +02:00
import getLogger from "../../libs/logger.js"
2022-05-28 16:10:37 +02:00
import settings from "../../libs/settings.js"
import https from "https"
2022-05-28 15:03:43 +02:00
const log = getLogger("Health Checker", settings.checkers["health-checker"].color)
2022-05-28 16:29:45 +02:00
// This function is a mess -- I know.
2022-05-28 16:10:37 +02:00
function checkService(serviceDomain) {
log(`Checking service '${serviceDomain}'`)
return new Promise(
(resolve, reject) => {
const req = https.request({
hostname: serviceDomain,
port: 443,
path: "/",
method: "GET"
}, res => {
2022-05-28 16:29:45 +02:00
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()
2022-05-28 16:10:37 +02:00
})
2022-05-28 16:29:45 +02:00
req.on("error", () => reject())
2022-05-28 16:10:37 +02:00
req.end()
}
)
}
export function start(sendMessage) {
2022-05-28 15:03:43 +02:00
log("Starting up the health checker...")
2022-05-28 16:10:37 +02:00
log("Services to watch:", settings.checkers["health-checker"].services)
function checkServices(serviceDomains) {
for (const serviceDomain of serviceDomains) {
checkService(serviceDomain).then(
2022-05-28 16:29:45 +02:00
() => log(`Service '${serviceDomain}' seems to be running!`)
2022-05-28 16:10:37 +02:00
).catch(
2022-05-28 16:29:45 +02:00
() => {
const message = `**IMPORTANT!** Service '${serviceDomain}' seems to have stopped!`
log(message)
sendMessage(message)
2022-05-28 16:10:37 +02:00
}
)
}
}
checkServices(settings.checkers["health-checker"].services)
setInterval(
() => checkServices(settings.checkers["health-checker"].services),
(settings.checkers["health-checker"].interval ?? 300) * 1000
)
return
2022-05-28 15:03:43 +02:00
}