UNPKG

@nam088/nestjs-rabbitmq

Version:

A comprehensive RabbitMQ module for NestJS with decorator-based message handling

69 lines 3.16 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RabbitMQHealthIndicator = void 0; const common_1 = require("@nestjs/common"); const terminus_1 = require("@nestjs/terminus"); const constants_1 = require("../constants"); const inject_rabbitmq_decorator_1 = require("../decorators/inject-rabbitmq.decorator"); const rabbitmq_service_1 = require("../services/rabbitmq.service"); let RabbitMQHealthIndicator = class RabbitMQHealthIndicator extends terminus_1.HealthIndicator { constructor(rabbitmqService) { super(); this.rabbitmqService = rabbitmqService; } async checkConnection(timeout) { return new Promise((resolve) => { const timer = setTimeout(() => { resolve(false); }, timeout); try { const isConnected = this.rabbitmqService.isConnected(); clearTimeout(timer); resolve(isConnected); } catch { clearTimeout(timer); resolve(false); } }); } async isHealthy(connectionName = constants_1.DEFAULT_CONNECTION_NAME, timeout = 5000) { const key = `rabbitmq_${connectionName}`; try { const isConnected = await this.checkConnection(timeout); if (isConnected) { return this.getStatus(key, true, { status: 'up', connection: connectionName, }); } throw new Error('RabbitMQ connection is not established'); } catch (error) { throw new terminus_1.HealthCheckError('RabbitMQ health check failed', this.getStatus(key, false, { status: 'down', connection: connectionName, error: error.message, })); } } }; exports.RabbitMQHealthIndicator = RabbitMQHealthIndicator; exports.RabbitMQHealthIndicator = RabbitMQHealthIndicator = __decorate([ (0, common_1.Injectable)(), __param(0, (0, inject_rabbitmq_decorator_1.InjectRabbitMQ)()), __metadata("design:paramtypes", [rabbitmq_service_1.RabbitMQService]) ], RabbitMQHealthIndicator); //# sourceMappingURL=rabbitmq.health.js.map