UNPKG

@goparrot/pubsub-event-bus

Version:
57 lines 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoRetryStrategy = 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 AbstractHandleWrapperStrategy_1 = require("./AbstractHandleWrapperStrategy"); let AutoRetryStrategy = class AutoRetryStrategy extends AbstractHandleWrapperStrategy_1.AbstractHandleWrapperStrategy { constructor(retryStrategies, rootRetryOptions, assertExchangeOptions) { super(); this.retryStrategies = retryStrategies; this.rootRetryOptions = rootRetryOptions; this.assertExchangeOptions = assertExchangeOptions; this.strategy = interface_1.AutoAckEnum.AUTO_RETRY; } process(handlerWrapper, channelWrapper) { const { handler, options: { retryOptions }, } = handlerWrapper; const { maxRetryAttempts = this.rootRetryOptions.maxRetryAttempts, strategy = this.rootRetryOptions.strategy } = retryOptions !== null && retryOptions !== void 0 ? retryOptions : {}; const originalMethod = handler.prototype.handle; const retryStrategy = this.retryStrategies[strategy]; const logger = this.logger; Reflect.defineProperty(handler.prototype, 'handle', { ...Reflect.getOwnPropertyDescriptor(handler.prototype, 'handle'), async value(event) { var _a, _b; try { await originalMethod.apply(this, [event]); } catch (error) { if (event.retryCount >= maxRetryAttempts) { await ((_b = (_a = this).onRetryAttemptsExceeded) === null || _b === void 0 ? void 0 : _b.call(_a, event, error)); logger.warn(`Maximum number of retry attempts (${maxRetryAttempts}) exceeded. Discarded message: ${JSON.stringify(event)}`, handler.name); return; } await retryStrategy.requeue(channelWrapper, handlerWrapper, event); } finally { const message = event.message(); if (message) { channelWrapper.ack(message); } } }, }); } }; exports.AutoRetryStrategy = AutoRetryStrategy; exports.AutoRetryStrategy = AutoRetryStrategy = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, common_1.Inject)(provider_1.CQRS_RETRY_STRATEGIES)), tslib_1.__param(1, (0, common_1.Inject)(configuration_1.CQRS_RETRY_OPTIONS)), tslib_1.__param(2, (0, common_1.Inject)(configuration_1.CQRS_EXCHANGE_CONFIG)), tslib_1.__metadata("design:paramtypes", [Object, Object, Object]) ], AutoRetryStrategy); //# sourceMappingURL=AutoRetryStrategy.js.map