@qrvey/health-checker
Version:
 
94 lines • 4.31 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-explicit-any */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RabbitMQHealthChecker = void 0;
exports.getRabbitHttpHost = getRabbitHttpHost;
const requireEnv_1 = require("../../utils/requireEnv");
const logger_1 = __importDefault(require("../../utils/logger"));
const fetch_1 = require("@qrvey/fetch");
const constants_1 = require("../../utils/constants");
function getRabbitCredentials() {
const user = (0, requireEnv_1.requireEnv)('RABBITMQ_USER');
const pass = (0, requireEnv_1.requireEnv)('RABBITMQ_PASSWORD');
return { user, pass };
}
function getRabbitHttpHost() {
const raw = (0, requireEnv_1.requireEnv)('RABBITMQ_HTTP_HOST');
const url = new URL(raw);
const basePath = url.pathname.replace(/\/$/, '');
return `${url.protocol}//${url.host}${basePath}`;
}
function buildRabbitHttpConfig() {
const { user, pass } = getRabbitCredentials();
const basicAuth = Buffer.from(`${user}:${pass}`).toString('base64');
const authHeader = `Basic ${basicAuth}`;
return {
baseDomain: getRabbitHttpHost(),
authHeader,
};
}
async function ping() {
const { baseDomain, authHeader } = buildRabbitHttpConfig();
const res = await fetch(`${baseDomain}/api/overview`, {
headers: { Authorization: authHeader },
});
if (!res.ok) {
throw new Error(`[RabbitMQHealthChecker] Management API ping failed: ${res.statusText}`);
}
}
function listConsumers() {
const { baseDomain, authHeader } = buildRabbitHttpConfig();
return fetch_1.FetchService.get('/api/consumers', {
baseDomain,
headers: { Authorization: authHeader },
});
}
async function getConsumersSet() {
return new Set((await listConsumers()).map((c) => { var _a, _b, _c; return `${(_a = c === null || c === void 0 ? void 0 : c.consumer_tag) !== null && _a !== void 0 ? _a : ''}::${(_c = (_b = c === null || c === void 0 ? void 0 : c.queue) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ''}`; }));
}
async function validateQueues(queues, consumerTag, consumersSet) {
const missingQueues = queues.filter((queue) => !consumersSet.has(`${consumerTag}::${queue}`));
if (missingQueues.length > 0) {
throw new Error(`[RabbitMQHealthChecker] Missing subscriptions for queues: ${missingQueues.join(', ')} for consumerTag ${consumerTag}`);
}
}
async function handleConsumersCheck(param, metadata) {
var _a, _b, _c, _d;
const shouldLoadConsumers = ((_a = param === null || param === void 0 ? void 0 : param.queues) === null || _a === void 0 ? void 0 : _a.length) || (param === null || param === void 0 ? void 0 : param.returnConsumersSet);
if (!shouldLoadConsumers)
return;
const consumersSet = (_b = param === null || param === void 0 ? void 0 : param.consumersSet) !== null && _b !== void 0 ? _b : (await getConsumersSet());
if ((_c = param === null || param === void 0 ? void 0 : param.queues) === null || _c === void 0 ? void 0 : _c.length) {
const consumerTag = (_d = param.hostName) !== null && _d !== void 0 ? _d : (0, requireEnv_1.requireEnv)('HOSTNAME');
await validateQueues(param.queues, consumerTag, consumersSet);
}
if (param === null || param === void 0 ? void 0 : param.returnConsumersSet) {
metadata.consumersSet = consumersSet;
}
}
async function performCheck(param) {
const metadata = {};
if (!(param === null || param === void 0 ? void 0 : param.omitPing))
await ping();
await handleConsumersCheck(param, metadata);
if (process.env.NODE_ENV === 'test') {
logger_1.default.info('[RabbitMQHealthChecker] check executed successfully');
}
return { status: constants_1.OK, metadata };
}
exports.RabbitMQHealthChecker = {
dependency: 'eventBroker',
async check(param) {
try {
return await performCheck(param);
}
catch (error) {
logger_1.default.error('[RabbitMQHealthChecker] Health check failed', error);
throw error;
}
},
};
//# sourceMappingURL=rabbitmqHealthChecker.service.js.map