UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

75 lines 1.84 kB
import debug from 'debug'; /** * @file Handle health check services. * @copyright Piggly Lab 2024 */ export class HealthCheckService { /** * Handlers to check. * * @type {Map<string, () => Promise<boolean>>} * @protected * @memberof HealthCheckService * @since 4.0.0 * @author Caique Araujo <caique@piggly.com.br> */ handlers; /** * Constructor. * * @public * @constructor * @memberof HealthCheckService * @since 4.0.0 * @author Caique Araujo <caique@piggly.com.br> */ constructor() { this.handlers = new Map(); } /** * Check all handlers. * * @returns {Promise<{ * success: boolean; * services: Record<string, boolean>; * }>} * @public * @memberof HealthCheckService * @since 4.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async check() { let success = true; const services = {}; for (const [name, handler] of this.handlers) { try { services[name] = await handler(); } catch (error) { services[name] = false; debug('app:healthcheck:error')(`${name} failed to health check`, error); } finally { success = success && services[name]; } } return { services, success, }; } /** * Register a new handler. * * @param {string} name * @param {() => Promise<boolean>} handler * @public * @memberof HealthCheckService * @since 4.0.0 * @author Caique Araujo <caique@piggly.com.br> */ register(name, handler) { this.handlers.set(name, handler); } } //# sourceMappingURL=HealthCheckService.js.map