diff --git a/changelog/items/other/blocker-health-check.md b/changelog/items/other/blocker-health-check.md new file mode 100644 index 00000000..2e45834c --- /dev/null +++ b/changelog/items/other/blocker-health-check.md @@ -0,0 +1 @@ +- Add health check for the blocker container diff --git a/packages/health-check/src/checks/critical.js b/packages/health-check/src/checks/critical.js index dd614beb..22051bda 100644 --- a/packages/health-check/src/checks/critical.js +++ b/packages/health-check/src/checks/critical.js @@ -1,9 +1,11 @@ const got = require("got"); const FormData = require("form-data"); const { isEqual } = require("lodash"); -const { calculateElapsedTime, getResponseContent, getAuthCookie } = require("../utils"); +const { calculateElapsedTime, getResponseContent, getAuthCookie, isPortalModuleEnabled } = require("../utils"); const { SkynetClient, stringToUint8ArrayUtf8, genKeyPairAndSeed } = require("skynet-js"); +const MODULE_BLOCKER = "b"; + const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API); const exampleSkylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q"; @@ -173,6 +175,33 @@ async function accountHealthCheck(done) { done({ name: "accounts", time: calculateElapsedTime(time), ...data }); } +// blockerHealthCheck returns the result of blocker container health endpoint +async function blockerHealthCheck(done) { + const time = process.hrtime(); + const data = { up: false }; + + try { + const response = await got(`http://${process.env.BLOCKER_HOST}:${process.env.BLOCKER_PORT}/health`, { + responseType: "json", + }); + + data.statusCode = response.statusCode; + data.response = response.body; + data.up = response.body.dbAlive === true; + } catch (error) { + data.statusCode = error?.response?.statusCode || error.statusCode || error.status; + data.errorMessage = error.message; + data.errorResponseContent = getResponseContent(error.response); + } + + // this is a no-op but it's added to explicitly document the ip property + // should not be set on the data object to prevent the IP from being compared + // to the server's IP - this is not required for this check and will fail + delete data.ip; + + done({ name: "blocker", time: calculateElapsedTime(time), ...data }); +} + async function genericAccessCheck(name, url) { const authCookie = await getAuthCookie(); const time = process.hrtime(); @@ -210,4 +239,8 @@ if (process.env.ACCOUNTS_ENABLED === "true") { checks.push(accountHealthCheck, accountWebsiteCheck); } +if (isPortalModuleEnabled(MODULE_BLOCKER)) { + checks.push(blockerHealthCheck); +} + module.exports = checks; diff --git a/packages/health-check/src/utils.js b/packages/health-check/src/utils.js index 7ff84bec..6e35f75e 100644 --- a/packages/health-check/src/utils.js +++ b/packages/health-check/src/utils.js @@ -100,10 +100,18 @@ function getAuthCookie() { return (getAuthCookie.cache = authenticate()); } +/** + * isPortalModuleEnabled returns true if the given module is enabled + */ +function isPortalModuleEnabled(module) { + return process.env.PORTAL_MODULES && process.env.PORTAL_MODULES.indexOf(module) !== -1; +} + module.exports = { calculateElapsedTime, getYesterdayISOString, getResponseContent, ensureValidJSON, getAuthCookie, + isPortalModuleEnabled, };