@onlineapps/conn-infra-mq 1.1.44 → 1.1.45

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.44",
3
+ "version": "1.1.45",
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,96 +365,8 @@ class QueueManager {
365
365
  }
366
366
  }
367
367
 
368
- // CRITICAL: Check if queue exists with different arguments BEFORE assertQueue
369
- // If queue exists with wrong args, delete it and create with correct args
370
- // This prevents 406 errors and channel closure
371
- let queueNeedsRecreation = false;
372
- let existingQueueInfo = null;
373
-
374
- try {
375
- // Use checkQueue to get existing queue info (this may close channel on 404, but that's OK)
376
- // We use a separate channel for this check to avoid closing the main channel
377
- let checkChannel = channel;
378
- try {
379
- existingQueueInfo = await checkChannel.checkQueue(queueName);
380
- console.log(`[QueueManager] DEBUG: Queue ${queueName} exists, checking arguments...`);
381
-
382
- // Compare existing queue arguments with expected arguments
383
- const existingArgs = existingQueueInfo.arguments || {};
384
- const expectedArgs = queueOptions.arguments || {};
385
-
386
- // Check if arguments match (deep comparison of key values)
387
- const argsMatch = Object.keys(expectedArgs).every(key => {
388
- const existing = existingArgs[key];
389
- const expected = expectedArgs[key];
390
-
391
- // Handle special case: x-dead-letter-routing-key may have placeholder replaced
392
- if (key === 'x-dead-letter-routing-key') {
393
- const expectedRoutingKey = expected.replace('{service}', serviceName);
394
- return existing === expectedRoutingKey || existing === expected;
395
- }
396
-
397
- return existing === expected;
398
- }) && Object.keys(existingArgs).length === Object.keys(expectedArgs).length;
399
-
400
- if (!argsMatch) {
401
- console.warn(`[QueueManager] Queue ${queueName} exists with different arguments, will delete and recreate`);
402
- console.warn(`[QueueManager] Existing args:`, JSON.stringify(existingArgs));
403
- console.warn(`[QueueManager] Expected args:`, JSON.stringify(expectedArgs));
404
- queueNeedsRecreation = true;
405
- } else {
406
- console.log(`[QueueManager] DEBUG: Queue ${queueName} exists with correct arguments`);
407
- }
408
- } catch (checkErr) {
409
- if (checkErr.code === 404) {
410
- // Queue doesn't exist - will be created
411
- console.log(`[QueueManager] DEBUG: Queue ${queueName} does not exist, will create`);
412
- existingQueueInfo = null;
413
- } else {
414
- // Other error - log but continue (will try assertQueue anyway)
415
- console.warn(`[QueueManager] checkQueue failed for ${queueName}:`, checkErr.message);
416
- // Channel may be closed, but we'll recreate it before assertQueue
417
- }
418
- }
419
- } catch (checkError) {
420
- // If checkQueue fails (e.g., channel closed), continue anyway
421
- // assertQueue will handle it
422
- console.warn(`[QueueManager] Failed to check queue ${queueName}, will try assertQueue:`, checkError.message);
423
- }
424
-
425
- // If queue exists with wrong args, delete it first
426
- if (queueNeedsRecreation && existingQueueInfo) {
427
- console.log(`[QueueManager] Deleting queue ${queueName} to recreate with correct arguments...`);
428
- try {
429
- // Recreate channel if needed (checkQueue may have closed it)
430
- if (!channel || channel.closed) {
431
- channel = await transport._connection.createChannel();
432
- channel._createdAt = new Date().toISOString();
433
- channel._closeReason = null;
434
- channel._lastOperation = `Recreated before deleteQueue ${queueName}`;
435
-
436
- channel.on('error', (err) => {
437
- console.error('[RabbitMQClient] Recreated queue channel error:', err.message);
438
- channel._closeReason = `Error: ${err.message} (code: ${err.code})`;
439
- });
440
- channel.on('close', () => {
441
- console.error('[RabbitMQClient] Recreated queue channel closed');
442
- console.error('[RabbitMQClient] Close reason:', channel._closeReason || 'Unknown');
443
- });
444
-
445
- transport._queueChannel = channel;
446
- }
447
-
448
- await channel.deleteQueue(queueName);
449
- console.log(`[QueueManager] ✓ Deleted queue ${queueName} for recreation`);
450
- } catch (deleteErr) {
451
- // If delete fails, log warning but continue - assertQueue will handle it
452
- console.warn(`[QueueManager] Failed to delete queue ${queueName}, will try assertQueue anyway:`, deleteErr.message);
453
- }
454
- }
455
-
456
368
  // CRITICAL: Ensure channel is usable before assertQueue
457
- // Recreate if closed (may have been closed by checkQueue or deleteQueue)
369
+ // Recreate if closed (may have been closed by previous operations)
458
370
  if (!channel || channel.closed) {
459
371
  console.log(`[QueueManager] DEBUG: Channel is closed before assertQueue ${queueName}, recreating...`);
460
372
  channel = await transport._connection.createChannel();
@@ -505,13 +417,13 @@ class QueueManager {
505
417
  console.log(`[QueueManager] DEBUG: assertQueue failed for ${queueName}:`, assertErr.message);
506
418
  console.log(`[QueueManager] DEBUG: Error code: ${assertErr.code}`);
507
419
 
508
- // If 406 PRECONDITION-FAILED, this should not happen if we deleted the queue
509
- // But handle it anyway - queue exists with different args
420
+ // If 406 PRECONDITION-FAILED, queue exists with different args
421
+ // Delete it and recreate with correct args
510
422
  if (assertErr.code === 406) {
511
- console.error(`[QueueManager] ERROR: Queue ${queueName} still has different arguments after deletion attempt!`);
512
- console.error(`[QueueManager] This should not happen - queue may have been recreated by another process`);
423
+ console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406), deleting and recreating...`);
424
+ console.warn(`[QueueManager] Expected args:`, JSON.stringify(queueOptions.arguments));
513
425
 
514
- // Try to delete and recreate one more time
426
+ // Delete queue and recreate with correct args
515
427
  try {
516
428
  // Recreate channel (closed by 406)
517
429
  channel = await transport._connection.createChannel();