Merge pull request #1564 from SkynetLabs/pj/add-blocker-health-check

Blocker Health Check
This commit is contained in:
Christopher Schinnerl 2022-01-17 13:52:33 +00:00 committed by GitHub
commit ed16602085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1 @@
- Add health check for the blocker container

View File

@ -1,9 +1,11 @@
const got = require("got"); const got = require("got");
const FormData = require("form-data"); const FormData = require("form-data");
const { isEqual } = require("lodash"); 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 { SkynetClient, stringToUint8ArrayUtf8, genKeyPairAndSeed } = require("skynet-js");
const MODULE_BLOCKER = "b";
const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API); const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API);
const exampleSkylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q"; const exampleSkylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q";
@ -173,6 +175,33 @@ async function accountHealthCheck(done) {
done({ name: "accounts", time: calculateElapsedTime(time), ...data }); 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) { async function genericAccessCheck(name, url) {
const authCookie = await getAuthCookie(); const authCookie = await getAuthCookie();
const time = process.hrtime(); const time = process.hrtime();
@ -210,4 +239,8 @@ if (process.env.ACCOUNTS_ENABLED === "true") {
checks.push(accountHealthCheck, accountWebsiteCheck); checks.push(accountHealthCheck, accountWebsiteCheck);
} }
if (isPortalModuleEnabled(MODULE_BLOCKER)) {
checks.push(blockerHealthCheck);
}
module.exports = checks; module.exports = checks;

View File

@ -100,10 +100,18 @@ function getAuthCookie() {
return (getAuthCookie.cache = authenticate()); 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 = { module.exports = {
calculateElapsedTime, calculateElapsedTime,
getYesterdayISOString, getYesterdayISOString,
getResponseContent, getResponseContent,
ensureValidJSON, ensureValidJSON,
getAuthCookie, getAuthCookie,
isPortalModuleEnabled,
}; };