UNPKG

@nestjs-redis/health-indicator

Version:

A comprehensive Redis health indicator for NestJS applications using the Terminus health check library

54 lines (53 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisHealthIndicator = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const nestjs_terminus_1 = require("./nestjs-terminus"); let RedisHealthIndicator = (() => { let _classDecorators = [(0, common_1.Injectable)()]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; var RedisHealthIndicator = _classThis = class { constructor() { /** * TODO * * This is workaround, this should be DI but for some reason * HealthIndicatorService is not injected after building the package. * * Reference (how it should be): https://docs.nestjs.com/recipes/terminus#custom-health-indicator * * ToDo: Fix this issue in the future. */ this.healthIndicatorService = new nestjs_terminus_1.HealthIndicatorService(); } async isHealthy(key, { client }) { const indicator = this.healthIndicatorService.check(key); try { const result = await client.ping(); const isHealthy = result === 'PONG'; if (!isHealthy) { return indicator.down({ message: `Redis ping failed: ${result}` }); } return indicator.up(); } catch (error) { return indicator.down({ message: error instanceof Error ? error.message : 'Redis connection failed', }); } } }; tslib_1.__setFunctionName(_classThis, "RedisHealthIndicator"); (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; tslib_1.__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); RedisHealthIndicator = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); tslib_1.__runInitializers(_classThis, _classExtraInitializers); })(); return RedisHealthIndicator = _classThis; })(); exports.RedisHealthIndicator = RedisHealthIndicator;