@onlineapps/conn-infra-mq 1.1.39 → 1.1.41

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.39",
3
+ "version": "1.1.41",
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": {
@@ -365,94 +365,73 @@ class QueueManager {
365
365
  }
366
366
  }
367
367
 
368
- // CRITICAL: Use the SAME pattern as ensureQueue() - checkQueue() FIRST, then assertQueue() only if 404
369
- // This prevents channel closure on 406 PRECONDITION-FAILED
370
- // Pattern: checkQueue() -> if 404, assertQueue() -> if 406, use existing queue (channel stays open)
368
+ // CRITICAL: Use assertQueue() DIRECTLY - it's idempotent and safer than checkQueue() + assertQueue()
369
+ // assertQueue() creates queue if it doesn't exist, or does nothing if it exists with same params
370
+ // If queue exists with different params (406), channel closes but that's OK - we accept the queue
371
371
  let queueCreated = false;
372
372
 
373
373
  // Track operation on channel for debugging
374
374
  if (channel && channel._lastOperation !== undefined) {
375
- channel._lastOperation = `About to checkQueue ${queueName}`;
375
+ channel._lastOperation = `About to assertQueue ${queueName}`;
376
376
  }
377
377
 
378
378
  try {
379
- console.log(`[QueueManager] DEBUG: About to checkQueue ${queueName} (same pattern as ensureQueue)`);
379
+ console.log(`[QueueManager] DEBUG: About to assertQueue ${queueName} directly (idempotent)`);
380
380
  console.log(`[QueueManager] DEBUG: Channel state: exists=${!!channel}, closed=${channel ? (channel.closed === true ? 'true' : channel.closed === false ? 'false' : 'undefined') : 'N/A'}`);
381
381
 
382
- // CRITICAL: Use checkQueue() FIRST (same as ensureQueue() pattern)
383
- // This prevents channel closure on 406 - if queue exists with different args, checkQueue() returns 406 but channel stays open
384
- await channel.checkQueue(queueName);
382
+ // CRITICAL: Use assertQueue() DIRECTLY - it's idempotent
383
+ // If queue doesn't exist creates it
384
+ // If queue exists with same params → does nothing (OK)
385
+ // If queue exists with different params → 406, channel closes (but we accept the queue)
386
+ await channel.assertQueue(queueName, queueOptions);
385
387
 
386
- // Queue exists - use it
387
- console.log(`[QueueManager] DEBUG: checkQueue succeeded - queue ${queueName} exists`);
388
+ // Queue created or already exists with same params
389
+ console.log(`[QueueManager] DEBUG: assertQueue succeeded for ${queueName}`);
388
390
  if (channel && channel._lastOperation !== undefined) {
389
- channel._lastOperation = `checkQueue ${queueName} succeeded`;
391
+ channel._lastOperation = `assertQueue ${queueName} succeeded`;
390
392
  }
391
393
  queues[queueInfo.type] = queueName;
392
394
  queueCreated = true;
393
- console.info(`[QueueManager] ✓ Business queue exists: ${queueName}`);
394
- } catch (checkErr) {
395
- console.log(`[QueueManager] DEBUG: checkQueue failed for ${queueName}:`, checkErr.message);
396
- console.log(`[QueueManager] DEBUG: Error code: ${checkErr.code}`);
395
+ console.info(`[QueueManager] ✓ Created/verified business queue: ${queueName}`);
396
+ } catch (assertErr) {
397
+ console.log(`[QueueManager] DEBUG: assertQueue failed for ${queueName}:`, assertErr.message);
398
+ console.log(`[QueueManager] DEBUG: Error code: ${assertErr.code}`);
397
399
 
398
- // Check if channel is still open
399
- if (!channel || channel.closed) {
400
- throw new Error('Channel closed during checkQueue - cannot verify queue');
401
- }
402
-
403
- // If queue doesn't exist (404), create it using assertQueue
404
- if (checkErr.code === 404) {
405
- console.log(`[QueueManager] DEBUG: Queue ${queueName} doesn't exist (404), creating with assertQueue...`);
406
-
407
- if (channel && channel._lastOperation !== undefined) {
408
- channel._lastOperation = `About to assertQueue ${queueName} (queue doesn't exist)`;
409
- }
400
+ // If 406 PRECONDITION-FAILED, queue exists with different args - accept it
401
+ // Channel is closed by server, but that's OK - queue exists and we can use it
402
+ if (assertErr.code === 406) {
403
+ console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406), accepting as-is:`, assertErr.message);
404
+ queues[queueInfo.type] = queueName;
405
+ queueCreated = true;
406
+ console.info(`[QueueManager] ✓ Business queue exists (different args, accepted): ${queueName}`);
410
407
 
408
+ // Channel is closed, but we need to recreate it for next queue
409
+ // Don't throw error - queue exists, we just need new channel
411
410
  try {
412
- await channel.assertQueue(queueName, queueOptions);
413
- console.log(`[QueueManager] DEBUG: assertQueue succeeded for ${queueName}`);
414
- if (channel && channel._lastOperation !== undefined) {
415
- channel._lastOperation = `assertQueue ${queueName} succeeded`;
416
- }
417
- queues[queueInfo.type] = queueName;
418
- queueCreated = true;
419
- console.info(`[QueueManager] ✓ Created business queue: ${queueName}`);
420
- } catch (assertErr) {
421
- // If channel closed during assertQueue, it's a real error
422
- if (!channel || channel.closed) {
423
- throw new Error(`Channel closed during assertQueue for ${queueName} - check channel management`);
424
- }
411
+ const newChannel = await transport._connection.createChannel();
412
+ newChannel._createdAt = new Date().toISOString();
413
+ newChannel._closeReason = null;
414
+ newChannel._lastOperation = `Recreated after 406 for ${queueName}`;
425
415
 
426
- // If 406 PRECONDITION-FAILED, queue exists with different args (race condition?)
427
- if (assertErr.code === 406) {
428
- console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406 from assertQueue):`, assertErr.message);
429
- // Channel should still be open - try checkQueue to verify
430
- if (!channel || channel.closed) {
431
- throw new Error('Channel closed - cannot check existing queue');
432
- }
433
- try {
434
- await channel.checkQueue(queueName);
435
- queues[queueInfo.type] = queueName;
436
- queueCreated = true;
437
- console.info(`[QueueManager] Business queue exists (verified): ${queueName}`);
438
- } catch (verifyErr) {
439
- throw new Error(`Failed to verify existing queue ${queueName}: ${verifyErr.message}`);
440
- }
441
- } else {
442
- // Other errors - rethrow
443
- throw assertErr;
444
- }
416
+ newChannel.on('error', (err) => {
417
+ console.error('[RabbitMQClient] Recreated queue channel error:', err.message);
418
+ newChannel._closeReason = `Error: ${err.message} (code: ${err.code})`;
419
+ });
420
+ newChannel.on('close', () => {
421
+ console.error('[RabbitMQClient] Recreated queue channel closed');
422
+ console.error('[RabbitMQClient] Close reason:', newChannel._closeReason || 'Unknown');
423
+ });
424
+
425
+ channel = newChannel;
426
+ transport._queueChannel = channel;
427
+ console.log(`[QueueManager] DEBUG: Channel recreated after 406 for ${queueName}`);
428
+ } catch (recreateErr) {
429
+ // Even if channel recreation fails, queue exists - log warning but continue
430
+ console.warn(`[QueueManager] Failed to recreate channel after 406 for ${queueName}, but queue exists:`, recreateErr.message);
445
431
  }
446
- } else if (checkErr.code === 406) {
447
- // Queue exists with different args (406 from checkQueue) - use it as-is
448
- // CRITICAL: checkQueue() on 406 does NOT close channel (unlike assertQueue() on 406)
449
- console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406 from checkQueue), using as-is:`, checkErr.message);
450
- queues[queueInfo.type] = queueName;
451
- queueCreated = true;
452
- console.info(`[QueueManager] ✓ Business queue exists (different args): ${queueName}`);
453
432
  } else {
454
- // Other error - rethrow
455
- throw checkErr;
433
+ // Other error - critical, cannot continue
434
+ throw new Error(`Failed to create business queue ${queueName}: ${assertErr.message}`);
456
435
  }
457
436
  }
458
437