From dffd4b050782e07c630c5ddc33fe92012b21b846 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 28 May 2022 16:10:37 +0200 Subject: [PATCH] Added health checker. --- modules/checkers/health-checker.js | 47 ++++++++++++++++++++++++++++-- modules/checkers/ip-changed.js | 10 +++++-- modules/notifiers/xmpp.js | 2 +- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/modules/checkers/health-checker.js b/modules/checkers/health-checker.js index 189a2f6..fc97522 100644 --- a/modules/checkers/health-checker.js +++ b/modules/checkers/health-checker.js @@ -1,9 +1,52 @@ import getLogger from "../../libs/logger.js" -import settings from "../../libs/settings.js" +import settings from "../../libs/settings.js" +import https from "https" const log = getLogger("Health Checker", settings.checkers["health-checker"].color) -export function start() { +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 } \ No newline at end of file diff --git a/modules/checkers/ip-changed.js b/modules/checkers/ip-changed.js index 686045c..3ce5fd3 100644 --- a/modules/checkers/ip-changed.js +++ b/modules/checkers/ip-changed.js @@ -12,7 +12,7 @@ const currentIp = await getIp() const cachedOldIp = fs.readFileSync("./ip.cache").toString() let oldIp = cachedOldIp ? oldIp : currentIp -async function checkIp() { +async function checkIp(sendMessage) { log("Checking for IP changes...") const nowIp = await getIp() if (nowIp !== oldIp) { @@ -27,7 +27,11 @@ async function checkIp() { export async function start(sendMessage) { log("Starting up the IP watcher...") - checkIp() - setInterval(checkIp, (settings.checkers["ip-changed"].interval ?? 3600) * 1000) + checkIp(sendMessage) + setInterval( + () => checkIp(sendMessage), + (settings.checkers["ip-changed"].interval ?? 3600) * 1000 + ) + return } \ No newline at end of file diff --git a/modules/notifiers/xmpp.js b/modules/notifiers/xmpp.js index 7368f0a..1188d5c 100644 --- a/modules/notifiers/xmpp.js +++ b/modules/notifiers/xmpp.js @@ -13,7 +13,7 @@ export async function start() { password: settings.notifiers.xmpp.sender.pass }) - xmpp.on("error", err => console.error(err)) + xmpp.on("error", err => console.error(err)) xmpp.on("offline", () => console.log("OFFLINE!")) async function sendMessage(msg) {