@onlineapps/conn-infra-mq 1.1.50 → 1.1.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlineapps/conn-infra-mq",
3
- "version": "1.1.50",
3
+ "version": "1.1.51",
4
4
  "description": "A promise-based, broker-agnostic client for sending and receiving messages via RabbitMQ",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -337,9 +337,43 @@ class RabbitMQClient extends EventEmitter {
337
337
  console.log(`[RabbitMQClient] [CONSUMER] Asserting queue ${queue} before consume() at ${new Date().toISOString()}`);
338
338
  console.log(`[RabbitMQClient] [CONSUMER] Queue options:`, JSON.stringify(queueOptions, null, 2));
339
339
 
340
+ // CRITICAL: Ensure _queueChannel is open before using it
341
+ // If it's closed (e.g., due to 406 error), recreate it
342
+ if (!this._queueChannel || this._queueChannel.closed) {
343
+ console.warn(`[RabbitMQClient] [CONSUMER] _queueChannel is closed or null, recreating...`);
344
+ try {
345
+ this._queueChannel = await this._connection.createChannel();
346
+ this._queueChannel._createdAt = new Date().toISOString();
347
+ this._queueChannel._closeReason = null;
348
+ this._queueChannel._lastOperation = null;
349
+
350
+ // Re-attach event listeners
351
+ this._queueChannel.on('error', (err) => {
352
+ console.error('[RabbitMQClient] Queue channel error:', err.message);
353
+ console.error('[RabbitMQClient] Error code:', err.code);
354
+ console.error('[RabbitMQClient] Channel created at:', this._queueChannel._createdAt);
355
+ console.error('[RabbitMQClient] Last operation:', this._queueChannel._lastOperation);
356
+ this._queueChannel._closeReason = `Error: ${err.message} (code: ${err.code})`;
357
+ });
358
+ this._queueChannel.on('close', () => {
359
+ console.error('[RabbitMQClient] Queue channel closed');
360
+ console.error('[RabbitMQClient] Channel created at:', this._queueChannel._createdAt);
361
+ console.error('[RabbitMQClient] Close reason:', this._queueChannel._closeReason || 'Unknown');
362
+ console.error('[RabbitMQClient] Last operation:', this._queueChannel._lastOperation);
363
+ });
364
+
365
+ console.log(`[RabbitMQClient] [CONSUMER] ✓ _queueChannel recreated`);
366
+ } catch (recreateErr) {
367
+ console.error(`[RabbitMQClient] [CONSUMER] ✗ Failed to recreate _queueChannel:`, recreateErr.message);
368
+ throw new Error(`Cannot assertQueue: _queueChannel is closed and recreation failed: ${recreateErr.message}`);
369
+ }
370
+ }
371
+
340
372
  // Use queueChannel for assertQueue to avoid RPC reply queue issues
341
- const channelForAssert = this._queueChannel || this._channel;
373
+ const channelForAssert = this._queueChannel;
374
+ this._queueChannel._lastOperation = `About to assertQueue ${queue}`;
342
375
  await channelForAssert.assertQueue(queue, queueOptions);
376
+ this._queueChannel._lastOperation = `assertQueue ${queue} succeeded`;
343
377
 
344
378
  const assertEndTime = Date.now();
345
379
  console.log(`[RabbitMQClient] [CONSUMER] ✓ Queue ${queue} asserted (took ${assertEndTime - assertStartTime}ms)`);
@@ -354,6 +388,15 @@ class RabbitMQClient extends EventEmitter {
354
388
  console.log(`[RabbitMQClient] [CONSUMER] Options: prefetch=${prefetch}, noAck=${noAck}, durable=${durable}`);
355
389
  console.log(`[RabbitMQClient] [CONSUMER] Channel state before consume: closed=${this._channel.closed}`);
356
390
 
391
+ // CRITICAL WARNING: amqplib's channel.consume() may internally call assertQueue() WITHOUT parameters
392
+ // This happens if the queue doesn't exist or if amqplib needs to verify queue existence
393
+ // If queue exists with different arguments (e.g., x-message-ttl), this will cause 406 PRECONDITION-FAILED
394
+ // and close the channel. That's why we assertQueue() with correct parameters BEFORE consume() above.
395
+ console.log(`[RabbitMQClient] [CONSUMER] ⚠ WARNING: About to call amqplib's channel.consume() for queue: ${queue}`);
396
+ console.log(`[RabbitMQClient] [CONSUMER] ⚠ WARNING: amqplib may internally call assertQueue() WITHOUT parameters if queue doesn't exist`);
397
+ console.log(`[RabbitMQClient] [CONSUMER] ⚠ WARNING: If queue exists with different arguments, this will cause 406 PRECONDITION-FAILED and close channel`);
398
+ console.log(`[RabbitMQClient] [CONSUMER] ⚠ WARNING: We already asserted queue with correct parameters above - this should prevent 406`);
399
+
357
400
  const consumeResult = await this._channel.consume(
358
401
  queue,
359
402
  async (msg) => {