UNPKG

coffee-core

Version:

Coffee IT API core library

100 lines 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RabbitMQPublisher = void 0; const amqplib_1 = require("amqplib"); const common_1 = require("@nestjs/common"); const custom_logger_1 = require("../../logger/custom-logger"); class RabbitMQPublisher { config; logger = new custom_logger_1.CustomLogger(RabbitMQPublisher.name); channel; server; constructor(config) { this.config = config; } async publishRetry(message, routingKey, maxRetries = 10, logEvent = true) { await this.publishAndRetry(message, routingKey, 0, maxRetries, logEvent); } async publishAndRetry(message, routingKey, retries, maxRetries, logEvent) { try { await this.publish(message, routingKey, logEvent); } catch (e) { const timeOut = setTimeout(async () => { const newRetries = retries + 1; this.logger.error(`Got error when publishing to ${routingKey}. Retrying for the ${newRetries}th time. ${e}`); try { await this.publish(message, routingKey, logEvent); } catch (err) { await this.publishRetry(message, routingKey, newRetries, logEvent); } }, this.config.PUBLISH_RETRY_TIMEOUT_MS * (2 ** retries)); if (retries >= maxRetries) { clearTimeout(timeOut); common_1.Logger.error(`Stopped retrying to publish to ${routingKey} after ${retries} times.`); } } } async publish(message, routingKey, logEvent = true) { const channel = await this.getChannel(); const createdExchange = await channel.assertExchange(this.config.EXCHANGE_NAME, this.config.EXCHANGE_TYPE, { durable: this.config.DURABLE_EXCHANGE }); const byteMessage = Buffer.from(JSON.stringify(message)); const options = { persistent: this.config.PERSISTENT_MESSAGES }; channel.publish(createdExchange.exchange, routingKey, byteMessage, options); if (logEvent) { this.logger.debug(`Published event to ${routingKey} with payload ${JSON.stringify(message, null, 2)}`); } } async close() { if (this.channel) { await this.channel.close(); } } async hasActiveConnection() { try { const server = await this.getServer(); const channel = await this.getChannel(); return server !== undefined && channel !== undefined; } catch (err) { return false; } } async getChannel() { if (this.channel) { return this.channel; } const server = await this.getServer(); this.channel = await server.createChannel(); this.channel.on('close', () => { this.logger.debug('Channel closed'); this.channel = undefined; }); this.channel.on('err', () => { this.logger.debug('Channel got an error'); this.channel = undefined; }); return this.channel; } async getServer() { if (this.server) { return this.server; } this.server = await this.connect(); this.server.on('close', () => { this.server = undefined; this.logger.debug('RabbitMQ server closed'); }); this.server.on('err', () => { this.logger.debug('RabbitMQ server got an error'); this.server = undefined; }); return this.server; } async connect() { return (0, amqplib_1.connect)(this.config.AMQP_CONNECTION_URI); } } exports.RabbitMQPublisher = RabbitMQPublisher; //# sourceMappingURL=rabbitmq-publisher.js.map