UNPKG

@visulima/health-check

Version:

A library built to provide support for defining service health for node services. It allows you to register async health checks for your dependencies and the service itself, provides a health endpoint that exposes their status, and health metrics.

56 lines (54 loc) 1.64 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class Healthcheck { static { __name(this, "Healthcheck"); } /** * A copy of registered checkers */ healthCheckers = {}; addChecker(service, checker) { this.healthCheckers[service] = checker; } /** * Returns the health check reports. The health checks are performed when * this method is invoked. */ async getReport() { const report = {}; await Promise.all(Object.keys(this.healthCheckers).map(async (service) => await this.invokeChecker(service, report))); const unhealthyService = Object.keys(report).find((service) => !report[service].health.healthy); return { healthy: !unhealthyService, report }; } async isLive() { const { healthy } = await this.getReport(); return healthy; } /** * Returns an array of registered services names */ get servicesList() { return Object.keys(this.healthCheckers); } /** * Invokes a given checker to collect the report metrics. */ async invokeChecker(service, reportSheet) { const checker = this.healthCheckers[service]; let report; try { report = await checker(); report.displayName = report.displayName || service; } catch (error) { report = { displayName: service, health: { healthy: false, message: error.message, timestamp: (/* @__PURE__ */ new Date()).toISOString() }, meta: { fatal: true } }; } reportSheet[service] = report; return report.health.healthy; } } export { Healthcheck as default };