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

52 lines
1.6 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: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 => {
if (res.statusCode === 200) resolve(`Service '${serviceDomain}' seems to be running!`)
res.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`))
})
req.on("error", () => reject(`Service '${serviceDomain}' seems to have stopped!`))
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(
status => log(status)
).catch(
status => {
log(status)
sendMessage(status)
}
)
}
}
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
}