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) 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) { 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( 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 }