@onlineapps/conn-infra-mq 1.1.20 → 1.1.21

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.20",
3
+ "version": "1.1.21",
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": {
@@ -103,17 +103,40 @@ class QueueManager {
103
103
  throw new Error('Channel closed during checkQueue - cannot verify queue');
104
104
  }
105
105
 
106
- // If queue doesn't exist (404), we'll let it be created lazily on first publish
107
- // This avoids RPC reply queue issues with assertQueue()
106
+ // If queue doesn't exist (404), create it using assertQueue with proper error handling
107
+ // Business queues MUST be created with specific arguments (TTL, DLQ, max-length) from queueConfig
108
+ // Lazy creation via sendToQueue() is NOT suitable - it creates queue with default arguments
108
109
  if (checkErr.code === 404) {
109
- // Queue doesn't exist - will be created on first sendToQueue()
110
- // Return a mock queue info to indicate queue will be created lazily
111
- console.info(`[QueueManager] Queue ${queueName} doesn't exist - will be created on first publish`);
112
- return {
113
- queue: queueName,
114
- messageCount: 0,
115
- consumerCount: 0
116
- };
110
+ // CRITICAL: Business queues MUST be created explicitly with correct arguments
111
+ // Use assertQueue() on regular channel (queueChannel) - this should work without RPC issues
112
+ // If it fails, it's a real problem that needs to be fixed, not worked around
113
+ try {
114
+ if (!channel || channel.closed) {
115
+ throw new Error('Channel closed - cannot create queue');
116
+ }
117
+
118
+ // For business queues: use assertQueue with queueOptions from queueConfig
119
+ // This ensures correct TTL, DLQ, max-length arguments
120
+ return await channel.assertQueue(queueName, queueOptions);
121
+ } catch (assertErr) {
122
+ // If channel closed during assertQueue, it's a real error
123
+ if (!channel || channel.closed) {
124
+ throw new Error(`Channel closed during assertQueue for ${queueName} - check channel management`);
125
+ }
126
+
127
+ // If 406 PRECONDITION-FAILED, queue exists with different args
128
+ if (assertErr.code === 406) {
129
+ console.warn(`[QueueManager] Queue ${queueName} exists with different arguments:`, assertErr.message);
130
+ // Try to get queue info anyway
131
+ if (!channel || channel.closed) {
132
+ throw new Error('Channel closed - cannot check existing queue');
133
+ }
134
+ return await channel.checkQueue(queueName);
135
+ }
136
+
137
+ // Other errors - rethrow
138
+ throw assertErr;
139
+ }
117
140
  } else {
118
141
  // Other error (including 406) - queue exists with different args
119
142
  // Log warning and return queue info without asserting
@@ -222,23 +222,24 @@ class RabbitMQClient extends EventEmitter {
222
222
  throw checkErr;
223
223
  }
224
224
  } else {
225
- // Business queue - may need to be created, but use unified config
225
+ // Business queue - should already be created by setupServiceQueues() BEFORE consume is called
226
226
  // Use queueChannel (regular channel) for queue operations to avoid RPC reply queue issues
227
- const queueOptions = this._getQueueOptions(queue, { durable });
228
-
229
- // Try to assert queue with our config
230
- // If it fails with 406 (PRECONDITION-FAILED), queue exists with different args - use it as-is
231
- // IMPORTANT: Don't try to re-assert after 406, as it will close the channel
227
+ // Only check if it exists - don't try to create it here (that's setupServiceQueues() responsibility)
232
228
  try {
233
- await this._queueChannel.assertQueue(queue, queueOptions);
234
- } catch (assertErr) {
235
- // If queue exists with different arguments (406), use it as-is without re-asserting
236
- if (assertErr.code === 406) {
237
- console.warn(`[RabbitMQClient] Queue ${queue} exists with different arguments, using as-is (skipping assert to avoid channel close):`, assertErr.message);
238
- // Don't try to re-assert - just proceed to consume
229
+ await this._queueChannel.checkQueue(queue);
230
+ // Queue exists - proceed to consume
231
+ } catch (checkErr) {
232
+ if (checkErr.code === 404) {
233
+ // Queue doesn't exist - this is an error, queue should have been created by setupServiceQueues()
234
+ throw new Error(`Business queue '${queue}' not found. Queue should be created by setupServiceQueues() before consuming.`);
235
+ }
236
+ // Other error (including 406) - queue exists with different args, use it as-is
237
+ if (checkErr.code === 406) {
238
+ console.warn(`[RabbitMQClient] Queue ${queue} exists with different arguments, using as-is:`, checkErr.message);
239
+ // Proceed to consume - queue exists, just with different args
239
240
  } else {
240
241
  // Other error - rethrow
241
- throw assertErr;
242
+ throw checkErr;
242
243
  }
243
244
  }
244
245
  }