UNPKG

event-message-broker

Version:

A Message Bus that uses AWS Stack and RabbitMQ

112 lines (111 loc) 6.09 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.SQSService = void 0; const client_sqs_1 = require("@aws-sdk/client-sqs"); const constants_1 = require("../utils/constants"); const sqs_consumer_1 = require("sqs-consumer"); const o11y_1 = require("../../application/utils/o11y"); const api_1 = require("@opentelemetry/api"); const configurations_1 = require("../../application/utils/configurations"); class SQSService { constructor() { this.client = new client_sqs_1.SQSClient(); } send(queueName, message, params) { return __awaiter(this, void 0, void 0, function* () { const command = new client_sqs_1.SendMessageCommand(Object.assign({ MessageBody: JSON.stringify(message), QueueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(queueName) }, params)); const output = yield this.client.send(command); return output.MessageId; }); } redelivery(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const command = new client_sqs_1.SendMessageCommand(Object.assign({ QueueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(params.QueueName), MessageBody: params.Message.Body, DelaySeconds: params.DelaySeconds, MessageAttributes: { RedeliveryStartsAt: { DataType: 'String', StringValue: (new Date((_d = (_c = (_b = (_a = params.Message) === null || _a === void 0 ? void 0 : _a.MessageAttributes) === null || _b === void 0 ? void 0 : _b['RedeliveryStartsAt']) === null || _c === void 0 ? void 0 : _c.StringValue) !== null && _d !== void 0 ? _d : new Date())).toString() }, RedeliveryCurrentAt: { DataType: 'String', StringValue: new Date().toString() }, RedeliveryAttempt: { DataType: 'Number', StringValue: (parseInt((_h = (_g = (_f = (_e = params.Message) === null || _e === void 0 ? void 0 : _e.MessageAttributes) === null || _f === void 0 ? void 0 : _f['RedeliveryAttempt']) === null || _g === void 0 ? void 0 : _g.StringValue) !== null && _h !== void 0 ? _h : '0') + 1).toString() } } }, params.Params)); const output = yield this.client.send(command); return { messageId: output.MessageId, startsAt: (_j = command.input.MessageAttributes) === null || _j === void 0 ? void 0 : _j['RedeliveryStartsAt'].StringValue, attempt: (_k = command.input.MessageAttributes) === null || _k === void 0 ? void 0 : _k['RedeliveryAttempt'].StringValue }; }); } consume(params) { const consumer = sqs_consumer_1.Consumer.create({ messageAttributeNames: ['All'], queueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(params.Endpoint), batchSize: configurations_1.Configuration.prefetch, sqs: this.client, handleMessage: (message) => __awaiter(this, void 0, void 0, function* () { const span = (0, o11y_1.startSpan)('bus', api_1.SpanKind.CONSUMER); try { span.setAttribute('queue', params.Endpoint); this.bindMessage(message); yield params.handle(message); span.addEvent('message-consumed', { correlationId: message.Object.CorrelationId }); } catch (error) { yield this.secondLevelResilience({ DelaySeconds: params.DelaySeconds, MaxRetryCount: params.MaxRetryCount, Message: message, QueueName: params.Endpoint }); } finally { span.end(); } }) }); consumer.start(); } bindMessage(message) { if (message.Body) { let messageContent = JSON.parse(message.Body); if (messageContent.Type === 'Notification') { messageContent = JSON.parse(messageContent.Message); } message.Object = messageContent; } } secondLevelResilience(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let retryCount = parseInt((_d = (_c = (_b = (_a = params.Message) === null || _a === void 0 ? void 0 : _a.MessageAttributes) === null || _b === void 0 ? void 0 : _b['RetryCount']) === null || _c === void 0 ? void 0 : _c.StringValue) !== null && _d !== void 0 ? _d : '0'); yield this.send(retryCount <= params.MaxRetryCount ? params.QueueName : `${params.QueueName}-dlq`, params.Message.Object, { DelaySeconds: retryCount <= params.MaxRetryCount ? params.DelaySeconds : 0, MessageAttributes: { 'RetryCount': { DataType: 'Number', StringValue: (retryCount <= params.MaxRetryCount ? (retryCount + 1) : params.MaxRetryCount).toString() } } }); }); } } exports.SQSService = SQSService;