rabbitmq-enterprise-toolkit
Version:
🚀 Enterprise-grade RabbitMQ wrapper for Node.js & TypeScript - High-throughput messaging with automatic retry, DLQ, batch processing & graceful shutdown. Production-ready AMQP client with zero message loss guarantee.
84 lines • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueueManager = void 0;
const errors_1 = require("../utils/errors");
const helpers_1 = require("../utils/helpers");
class QueueManager {
constructor(getChannel) {
this.getChannel = getChannel;
}
async setupQueue(options) {
const channel = this.getChannel();
const queueNameNormalized = (0, helpers_1.normalizeQueueName)(options.queueName);
const dlqName = this.getDLQName(options, queueNameNormalized);
try {
await this.createDeadLetterQueue(channel, dlqName, options);
await this.createMainQueue(channel, queueNameNormalized, dlqName, options);
await this.setPrefetchCount(channel, options);
console.log(`Queue registered: ${queueNameNormalized}`);
}
catch (error) {
// If queue already exists with different parameters, try to delete and recreate
if (error instanceof Error && error.message.includes('PRECONDITION_FAILED')) {
console.warn(`Queue ${queueNameNormalized} exists with different parameters. Attempting to recreate...`);
try {
// Try to delete the existing queue
await channel.deleteQueue(queueNameNormalized);
await channel.deleteQueue(dlqName);
// Wait a bit for deletion to propagate
await new Promise(resolve => setTimeout(resolve, 1000));
// Recreate the queues
await this.createDeadLetterQueue(channel, dlqName, options);
await this.createMainQueue(channel, queueNameNormalized, dlqName, options);
await this.setPrefetchCount(channel, options);
console.log(`Queue recreated: ${queueNameNormalized}`);
}
catch (recreateError) {
throw new errors_1.ConsumerError('Failed to recreate queue after parameter conflict', {
queueName: options.queueName,
error: (0, helpers_1.formatError)(recreateError)
});
}
}
else {
throw new errors_1.ConsumerError('Failed to setup queue', {
queueName: options.queueName,
error: (0, helpers_1.formatError)(error)
});
}
}
}
async createDeadLetterQueue(channel, dlqName, options) {
await channel.assertQueue(dlqName, {
durable: options.durable !== false
});
}
async createMainQueue(channel, queueName, dlqName, options) {
// High-throughput için optimize edilmiş queue arguments
const queueArguments = {
'x-dead-letter-exchange': '',
'x-dead-letter-routing-key': dlqName,
'x-queue-mode': 'lazy', // Lazy mode - çok veri için optimal
'x-max-length': 1000000, // Max 1M mesaj (memory protection)
'x-overflow': 'reject-publish' // Limit aşınca yeni mesajları reddet
};
await channel.assertQueue(queueName, {
durable: options.durable !== false,
exclusive: options.exclusive || false,
autoDelete: options.autoDelete || false,
arguments: queueArguments
});
}
async setPrefetchCount(channel, options) {
const prefetchCount = options.prefetchCount || 10;
await channel.prefetch(prefetchCount);
}
getDLQName(options, queueNameNormalized) {
if (options.deadLetterExchange) {
return options.deadLetterRoutingKey || (0, helpers_1.createDLQName)(queueNameNormalized);
}
return (0, helpers_1.createDLQName)(queueNameNormalized);
}
}
exports.QueueManager = QueueManager;
//# sourceMappingURL=queue-manager.js.map