UNPKG

@goparrot/pubsub-event-bus

Version:
53 lines 2.55 kB
import { __decorate, __metadata, __param } from "tslib"; import { Inject, Injectable } from '@nestjs/common'; import { AutoAckEnum } from '../../interface'; import { CQRS_RETRY_STRATEGIES } from '../../provider'; import { CQRS_EXCHANGE_CONFIG, CQRS_RETRY_OPTIONS } from '../../utils/configuration'; import { AbstractHandleWrapperStrategy } from './AbstractHandleWrapperStrategy'; let AutoRetryStrategy = class AutoRetryStrategy extends AbstractHandleWrapperStrategy { constructor(retryStrategies, rootRetryOptions, assertExchangeOptions) { super(); this.retryStrategies = retryStrategies; this.rootRetryOptions = rootRetryOptions; this.assertExchangeOptions = assertExchangeOptions; this.strategy = AutoAckEnum.AUTO_RETRY; } process(handlerWrapper, channelWrapper) { const { handler, options: { retryOptions }, } = handlerWrapper; const { maxRetryAttempts = this.rootRetryOptions.maxRetryAttempts, strategy = this.rootRetryOptions.strategy } = 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) { try { await originalMethod.apply(this, [event]); } catch (error) { if (event.retryCount >= maxRetryAttempts) { await this.onRetryAttemptsExceeded?.(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); } } }, }); } }; AutoRetryStrategy = __decorate([ Injectable(), __param(0, Inject(CQRS_RETRY_STRATEGIES)), __param(1, Inject(CQRS_RETRY_OPTIONS)), __param(2, Inject(CQRS_EXCHANGE_CONFIG)), __metadata("design:paramtypes", [Object, Object, Object]) ], AutoRetryStrategy); export { AutoRetryStrategy }; //# sourceMappingURL=AutoRetryStrategy.js.map