@nestjstools/messaging-rabbitmq-extension
Version:
Extension to handle messages and dispatch them over AMQP protocol
65 lines • 3.27 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RabbitmqMigrator = void 0;
const common_1 = require("@nestjs/common");
let RabbitmqMigrator = class RabbitmqMigrator {
async run(channel) {
if (channel.config.autoCreate === false) {
return Promise.resolve();
}
if (!channel.connection) {
throw new Error('Brak aktywnego połączenia AMQP');
}
this.channelWrapper = channel.createChannelWrapper();
await this.channelWrapper.addSetup(async (ch) => {
await ch.assertExchange(channel.config.exchangeName, channel.config.exchangeType, { durable: true });
await ch.assertQueue(channel.config.queue, {
durable: true,
});
if (channel.config.deadLetterQueueFeature) {
const dlxExchange = 'dead_letter.exchange';
const dlq = `${channel.config.queue}_dead_letter`;
await ch.assertExchange(dlxExchange, 'direct', { durable: true });
await ch.assertQueue(dlq, { durable: true });
await ch.bindQueue(dlq, dlxExchange, dlq);
}
if (channel.config.retryMessage) {
const retryExchange = `${channel.config.exchangeName}_retry.exchange`;
const retryQueue = `${channel.config.queue}_retry`;
await ch.assertExchange(retryExchange, channel.config.exchangeType, {
durable: true,
});
if (channel.config.forceRecreateRetryQueue) {
if (await ch.checkQueue(channel.config.queue)) {
await ch.deleteQueue(retryQueue);
}
}
await ch.assertQueue(retryQueue, {
durable: true,
arguments: {
'x-message-ttl': channel.config.retryMessageTtl ?? 1000,
'x-dead-letter-exchange': channel.config.exchangeName,
},
});
for (const key of channel.config.bindingKeys ?? []) {
await ch.bindQueue(retryQueue, retryExchange, key);
}
}
for (const key of channel.config.bindingKeys ?? []) {
await ch.bindQueue(channel.config.queue, channel.config.exchangeName, key);
}
});
return await this.channelWrapper.waitForConnect();
}
};
exports.RabbitmqMigrator = RabbitmqMigrator;
exports.RabbitmqMigrator = RabbitmqMigrator = __decorate([
(0, common_1.Injectable)()
], RabbitmqMigrator);
//# sourceMappingURL=rabbitmq.migrator.js.map