UNPKG

event-message-broker

Version:

A Message Bus that uses AWS Stack and RabbitMQ

118 lines (117 loc) 5.39 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RabbitMQBus = void 0; const RabbitMqInfrastructure_1 = require("../infrastructure/RabbitMqInfrastructure"); class RabbitMQBus { constructor(channel) { this.channel = channel; } publish(topicName, message) { return __awaiter(this, void 0, void 0, function* () { yield this.publishInExchange(topicName, "*", message, {}); }); } send(queueName, message, _) { return __awaiter(this, void 0, void 0, function* () { yield this.sendInQueue(queueName, message); }); } redelivery(params) { return __awaiter(this, void 0, void 0, function* () { const infra = new RabbitMqInfrastructure_1.RabbitMqInfrastructure(this.channel); const temporaryTopic = yield infra.bindTemporaryTopic(params.QueueName); yield this.publishInExchange(temporaryTopic, "*", params.Message.Object, { 'x-delay': params.DelaySeconds * 1000 }); }); } schedule(params) { return __awaiter(this, void 0, void 0, function* () { yield this.publishInExchange(params.TopicName, "*", params.Message, { 'x-delay': (params.ScheduleDate.getTime() - (new Date()).getTime()) }); }); } consume(params) { return __awaiter(this, void 0, void 0, function* () { const confirmChannel = yield this.channel.connection.createConfirmChannel(); confirmChannel.consume(params.Endpoint, (message) => __awaiter(this, void 0, void 0, function* () { if (!message) { return; } let messageContext = message; messageContext.Object = JSON.parse(message.content.toString('utf-8')); try { yield params.handle(messageContext); } catch (error) { yield this.secondLevelResilience({ DelaySeconds: params.DelaySeconds, MaxRetryCount: params.MaxRetryCount, QueueName: params.Endpoint, Message: messageContext }); } })); }); } secondLevelResilience(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const redeliveryAttempt = parseInt((_b = (_a = params.Message.properties.headers) === null || _a === void 0 ? void 0 : _a['x-redelivery-attempt']) !== null && _b !== void 0 ? _b : 0) + 1; if (redeliveryAttempt > params.MaxRetryCount) { yield this.sendInQueue(params.QueueName + '-dlq', params.Message.Object); } else { const infra = new RabbitMqInfrastructure_1.RabbitMqInfrastructure(this.channel); const temporaryTopic = yield infra.bindTemporaryTopic(params.QueueName); yield this.publishInExchange(temporaryTopic, "*", params.Message.Object, { 'x-delay': params.DelaySeconds * 1000, 'x-redelivery-attempt': redeliveryAttempt }); } }); } publishInExchange(topicName_1) { return __awaiter(this, arguments, void 0, function* (topicName, routingKey = "*", message, headers) { const confirmChannel = yield this.channel.connection.createConfirmChannel(); return new Promise((resolve, reject) => { confirmChannel.publish(topicName, routingKey, Buffer.from(JSON.stringify(message)), Object.assign(Object.assign({}, headers), { correlationId: message.CorrelationId }), (error, _) => { if (error) { reject(error); } else { resolve(); } }); }); }); } sendInQueue(queueName, message) { return __awaiter(this, void 0, void 0, function* () { const confirmChannel = yield this.channel.connection.createConfirmChannel(); return new Promise((resolve, reject) => { confirmChannel.sendToQueue(queueName, Buffer.from(JSON.stringify(message)), { correlationId: message.CorrelationId }, (error, _) => { if (error) { reject(error); } else { resolve(); } }); }); }); } } exports.RabbitMQBus = RabbitMQBus;