UNPKG

@goparrot/pubsub-event-bus

Version:
65 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DelayedMessageExchangeRetryStrategy = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const interface_1 = require("../../interface"); const provider_1 = require("../../provider"); const configuration_1 = require("../../utils/configuration"); const retry_constants_1 = require("../../utils/retry-constants"); const utils_1 = require("../../utils"); let DelayedMessageExchangeRetryStrategy = class DelayedMessageExchangeRetryStrategy { constructor(rootRetryOptions, assertExchangeOptions) { this.rootRetryOptions = rootRetryOptions; this.assertExchangeOptions = assertExchangeOptions; this.strategy = interface_1.RetryStrategyEnum.DELAYED_MESSAGE_EXCHANGE; } get logger() { return provider_1.LoggerProvider.logger; } async setupInfrastructure(channelWrapper, wrappersWithRetryStrategy) { const delayedExchangeOptions = { ...this.assertExchangeOptions, arguments: { ...this.assertExchangeOptions.arguments, 'x-delayed-type': 'direct' }, }; await channelWrapper.addSetup(async (channel) => { await Promise.all([ channel.assertExchange(retry_constants_1.DEFAULT_RETRY_DELAYED_MESSAGE_EXCHANGE_NAME, 'x-delayed-message', delayedExchangeOptions), ...wrappersWithRetryStrategy.map(async (handlerWrapper) => { const { queue } = handlerWrapper; return channel.bindQueue(queue, retry_constants_1.DEFAULT_RETRY_DELAYED_MESSAGE_EXCHANGE_NAME, queue); }), ]); this.logger.log(`Delayed message auto retry exchange "${retry_constants_1.DEFAULT_RETRY_DELAYED_MESSAGE_EXCHANGE_NAME}" asserted`); }); } async requeue(channelWrapper, handlerWrapper, event) { const { handler, queue, options: { retryOptions }, } = handlerWrapper; const message = event.message(); if (!message) { return; } const { delay = this.rootRetryOptions.delay } = retryOptions !== null && retryOptions !== void 0 ? retryOptions : {}; const retryCount = event.retryCount + 1; const delayValue = (0, utils_1.calculateDelay)(delay, retryCount); await channelWrapper.publish(retry_constants_1.DEFAULT_RETRY_DELAYED_MESSAGE_EXCHANGE_NAME, queue, event.payload, { ...message.properties, type: message.properties.type, headers: { ...message.properties.headers, 'x-delay': delayValue, [retry_constants_1.RETRY_COUNT_HEADER]: retryCount, [retry_constants_1.ORIGIN_EXCHANGE_HEADER]: (0, utils_1.getMessageExchange)(message), }, }); this.logger.log(`Event ${event.constructor.name} was republished to "${queue}" queue with ${delayValue} ms delay`, handler.name); } }; exports.DelayedMessageExchangeRetryStrategy = DelayedMessageExchangeRetryStrategy; exports.DelayedMessageExchangeRetryStrategy = DelayedMessageExchangeRetryStrategy = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, common_1.Inject)(configuration_1.CQRS_RETRY_OPTIONS)), tslib_1.__param(1, (0, common_1.Inject)(configuration_1.CQRS_EXCHANGE_CONFIG)), tslib_1.__metadata("design:paramtypes", [Object, Object]) ], DelayedMessageExchangeRetryStrategy); //# sourceMappingURL=DelayedMessageExchangeRetryStrategy.js.map