@goparrot/pubsub-event-bus
Version:
NestJS EventBus extension for RabbitMQ PubSub
86 lines • 4.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeadLetterTtlRetryStrategy = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const lodash_1 = require("lodash");
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 DeadLetterTtlRetryStrategy = class DeadLetterTtlRetryStrategy {
constructor(rootRetryOptions, assertExchangeOptions, bindingQueueOptions) {
this.rootRetryOptions = rootRetryOptions;
this.assertExchangeOptions = assertExchangeOptions;
this.bindingQueueOptions = bindingQueueOptions;
this.strategy = interface_1.RetryStrategyEnum.DEAD_LETTER_TTL;
}
get logger() {
return provider_1.LoggerProvider.logger;
}
async setupInfrastructure(channelWrapper, wrappersWithRetryStrategy) {
const delays = (0, lodash_1.chain)(wrappersWithRetryStrategy)
.flatMap((wrapper) => {
var _a;
const { delay = this.rootRetryOptions.delay, maxRetryAttempts = this.rootRetryOptions.maxRetryAttempts } = (_a = wrapper.options.retryOptions) !== null && _a !== void 0 ? _a : {};
return (0, lodash_1.times)(maxRetryAttempts, (retryCount) => (0, utils_1.calculateDelay)(delay, retryCount + 1));
})
.uniq()
.value();
await channelWrapper.addSetup(async (channel) => {
await Promise.all([
channel
.assertExchange(retry_constants_1.DEFAULT_RETRY_DELAY_EXCHANGE_NAME, 'topic', this.assertExchangeOptions)
.then(() => this.logger.log(`Delay auto retry exchange "${retry_constants_1.DEFAULT_RETRY_DELAY_EXCHANGE_NAME}" asserted`)),
channel
.assertExchange(retry_constants_1.DEFAULT_RETRY_REQUEUE_EXCHANGE_NAME, 'topic', this.assertExchangeOptions)
.then(() => this.logger.log(`Requeue auto retry exchange "${retry_constants_1.DEFAULT_RETRY_REQUEUE_EXCHANGE_NAME}" asserted`)),
...delays.map(async (delay) => {
const queue = `${retry_constants_1.DEFAULT_DELAY_QUEUE_NAME_PREFIX}.${delay}`;
await channel.assertQueue(queue, {
...this.bindingQueueOptions,
messageTtl: delay,
deadLetterExchange: retry_constants_1.DEFAULT_RETRY_REQUEUE_EXCHANGE_NAME,
});
await channel.bindQueue(queue, retry_constants_1.DEFAULT_RETRY_DELAY_EXCHANGE_NAME, `#.retry.${delay}`);
this.logger.log(`Delay queue asserted "${queue}" asserted`);
}),
...wrappersWithRetryStrategy.map(async (handlerWrapper) => {
const { queue } = handlerWrapper;
await channel.bindQueue(queue, retry_constants_1.DEFAULT_RETRY_REQUEUE_EXCHANGE_NAME, `${queue}.#`);
}),
]);
});
}
async requeue(channelWrapper, handlerWrapper, event) {
const { queue, handler, 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);
const routingKey = `${queue}.retry.${delayValue}`;
await channelWrapper.publish(retry_constants_1.DEFAULT_RETRY_DELAY_EXCHANGE_NAME, routingKey, event.payload, {
...message.properties,
type: message.properties.type,
headers: {
...message.properties.headers,
[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.DeadLetterTtlRetryStrategy = DeadLetterTtlRetryStrategy;
exports.DeadLetterTtlRetryStrategy = DeadLetterTtlRetryStrategy = 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.__param(2, (0, common_1.Inject)(configuration_1.CQRS_BINDING_QUEUE_CONFIG)),
tslib_1.__metadata("design:paramtypes", [Object, Object, Object])
], DeadLetterTtlRetryStrategy);
//# sourceMappingURL=DeadLetterTtlRetryStrategy.js.map