@onlineapps/conn-infra-mq 1.1.30 → 1.1.31

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.30",
3
+ "version": "1.1.31",
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": {
@@ -294,12 +294,16 @@ class QueueManager {
294
294
  }
295
295
 
296
296
  // CRITICAL: Use assertQueue directly - if queue exists with different args (406), that's OK
297
- // We'll handle it gracefully, but channel should NOT close
297
+ // BUT: 406 PRECONDITION-FAILED closes the channel in amqplib!
298
+ // Solution: Catch 406, recreate channel, and continue
298
299
  try {
299
300
  // Check if channel is still open before each operation
300
301
  if (!channel || channel.closed) {
301
- console.error(`[QueueManager] DEBUG: Channel closed before creating ${queueName}`);
302
- throw new Error(`Channel closed before creating ${queueName} - cannot continue`);
302
+ console.error(`[QueueManager] DEBUG: Channel closed before creating ${queueName}, recreating...`);
303
+ // Recreate channel if closed
304
+ channel = await transport._connection.createChannel();
305
+ transport.queueChannel = channel;
306
+ console.log(`[QueueManager] DEBUG: Channel recreated for ${queueName}`);
303
307
  }
304
308
 
305
309
  console.log(`[QueueManager] DEBUG: About to assertQueue ${queueName} with options:`, JSON.stringify(queueOptions, null, 2));
@@ -313,22 +317,36 @@ class QueueManager {
313
317
  } catch (assertErr) {
314
318
  console.error(`[QueueManager] DEBUG: assertQueue failed for ${queueName}:`, assertErr.message);
315
319
  console.error(`[QueueManager] DEBUG: Error code: ${assertErr.code}, channel closed: ${channel ? channel.closed : 'N/A'}`);
316
- console.error(`[QueueManager] DEBUG: Stack trace:`, assertErr.stack);
317
320
 
318
- // If channel closed, this is a critical error
319
- if (!channel || channel.closed) {
320
- console.error(`[QueueManager] DEBUG: Channel was closed during assertQueue for ${queueName}`);
321
- throw new Error(`Channel closed during assertQueue for ${queueName} - cannot create remaining queues`);
322
- }
323
-
324
- // If 406 PRECONDITION-FAILED, queue exists with different args - use it as-is
325
- // CRITICAL: Do NOT use checkQueue() here - it closes channel on 404!
326
- // If assertQueue() returned 406, queue exists (just with different args) - that's OK
321
+ // If 406 PRECONDITION-FAILED, queue exists with different args - channel is closed, recreate it
322
+ // CRITICAL: amqplib closes channel on 406, so we MUST recreate it to continue
327
323
  if (assertErr.code === 406) {
328
- console.warn(`[QueueManager] Queue ${queueName} exists with different arguments, using as-is:`, assertErr.message);
324
+ console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406), channel closed. Recreating channel...`);
325
+
326
+ // Recreate channel - 406 closes it
327
+ try {
328
+ channel = await transport._connection.createChannel();
329
+ transport.queueChannel = channel;
330
+ console.log(`[QueueManager] DEBUG: Channel recreated after 406 for ${queueName}, channel closed=${channel.closed}`);
331
+ } catch (recreateErr) {
332
+ throw new Error(`Failed to recreate channel after 406 for ${queueName}: ${recreateErr.message}`);
333
+ }
334
+
329
335
  // Queue exists (just with different args) - accept it
330
336
  queues[queueInfo.type] = queueName;
331
337
  } else {
338
+ // Other error - check if channel closed
339
+ if (!channel || channel.closed) {
340
+ console.error(`[QueueManager] DEBUG: Channel was closed during assertQueue for ${queueName}`);
341
+ // Try to recreate channel
342
+ try {
343
+ channel = await transport._connection.createChannel();
344
+ transport.queueChannel = channel;
345
+ console.log(`[QueueManager] DEBUG: Channel recreated after error for ${queueName}`);
346
+ } catch (recreateErr) {
347
+ throw new Error(`Channel closed during assertQueue for ${queueName} and failed to recreate: ${recreateErr.message}`);
348
+ }
349
+ }
332
350
  // Other error - critical, cannot continue
333
351
  throw new Error(`Failed to create business queue ${queueName}: ${assertErr.message}`);
334
352
  }