@onlineapps/conn-infra-mq 1.1.40 → 1.1.42
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
|
@@ -365,121 +365,73 @@ class QueueManager {
|
|
|
365
365
|
}
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
-
// CRITICAL: Use
|
|
369
|
-
//
|
|
370
|
-
//
|
|
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
|
|
375
|
+
channel._lastOperation = `About to assertQueue ${queueName}`;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
try {
|
|
379
|
-
console.log(`[QueueManager] DEBUG: About to
|
|
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
|
|
383
|
-
//
|
|
384
|
-
|
|
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
|
|
387
|
-
console.log(`[QueueManager] DEBUG:
|
|
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 = `
|
|
391
|
+
channel._lastOperation = `assertQueue ${queueName} succeeded`;
|
|
390
392
|
}
|
|
391
393
|
queues[queueInfo.type] = queueName;
|
|
392
394
|
queueCreated = true;
|
|
393
|
-
console.info(`[QueueManager] ✓
|
|
394
|
-
} catch (
|
|
395
|
-
console.log(`[QueueManager] DEBUG:
|
|
396
|
-
console.log(`[QueueManager] DEBUG: Error 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
|
-
//
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (checkErr.code === 404) {
|
|
406
|
-
console.log(`[QueueManager] DEBUG: Queue ${queueName} doesn't exist (404), creating with assertQueue...`);
|
|
407
|
-
|
|
408
|
-
// CRITICAL: checkQueue() on 404 may close channel - check and recreate if needed
|
|
409
|
-
if (!channel || channel.closed) {
|
|
410
|
-
console.warn(`[QueueManager] DEBUG: Channel closed after checkQueue(404) for ${queueName}, recreating...`);
|
|
411
|
-
try {
|
|
412
|
-
const newChannel = await transport._connection.createChannel();
|
|
413
|
-
newChannel._createdAt = new Date().toISOString();
|
|
414
|
-
newChannel._closeReason = null;
|
|
415
|
-
newChannel._lastOperation = `Recreated after checkQueue(404) for ${queueName}`;
|
|
416
|
-
|
|
417
|
-
newChannel.on('error', (err) => {
|
|
418
|
-
console.error('[RabbitMQClient] Recreated queue channel error:', err.message);
|
|
419
|
-
newChannel._closeReason = `Error: ${err.message} (code: ${err.code})`;
|
|
420
|
-
});
|
|
421
|
-
newChannel.on('close', () => {
|
|
422
|
-
console.error('[RabbitMQClient] Recreated queue channel closed');
|
|
423
|
-
console.error('[RabbitMQClient] Close reason:', newChannel._closeReason || 'Unknown');
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
channel = newChannel;
|
|
427
|
-
transport._queueChannel = channel;
|
|
428
|
-
console.log(`[QueueManager] DEBUG: Channel recreated after checkQueue(404) for ${queueName}`);
|
|
429
|
-
} catch (recreateErr) {
|
|
430
|
-
throw new Error(`Channel closed after checkQueue(404) for ${queueName} and failed to recreate: ${recreateErr.message}`);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (channel && channel._lastOperation !== undefined) {
|
|
435
|
-
channel._lastOperation = `About to assertQueue ${queueName} (queue doesn't exist)`;
|
|
436
|
-
}
|
|
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}`);
|
|
437
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
|
|
438
410
|
try {
|
|
439
|
-
await
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
}
|
|
444
|
-
queues[queueInfo.type] = queueName;
|
|
445
|
-
queueCreated = true;
|
|
446
|
-
console.info(`[QueueManager] ✓ Created business queue: ${queueName}`);
|
|
447
|
-
} catch (assertErr) {
|
|
448
|
-
// If channel closed during assertQueue, it's a real error
|
|
449
|
-
if (!channel || channel.closed) {
|
|
450
|
-
throw new Error(`Channel closed during assertQueue for ${queueName} - check channel management`);
|
|
451
|
-
}
|
|
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}`;
|
|
452
415
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
} else {
|
|
469
|
-
// Other errors - rethrow
|
|
470
|
-
throw assertErr;
|
|
471
|
-
}
|
|
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);
|
|
472
431
|
}
|
|
473
|
-
} else if (checkErr.code === 406) {
|
|
474
|
-
// Queue exists with different args (406 from checkQueue) - use it as-is
|
|
475
|
-
// CRITICAL: checkQueue() on 406 does NOT close channel (unlike assertQueue() on 406)
|
|
476
|
-
console.warn(`[QueueManager] Queue ${queueName} exists with different arguments (406 from checkQueue), using as-is:`, checkErr.message);
|
|
477
|
-
queues[queueInfo.type] = queueName;
|
|
478
|
-
queueCreated = true;
|
|
479
|
-
console.info(`[QueueManager] ✓ Business queue exists (different args): ${queueName}`);
|
|
480
432
|
} else {
|
|
481
|
-
// Other error -
|
|
482
|
-
throw
|
|
433
|
+
// Other error - critical, cannot continue
|
|
434
|
+
throw new Error(`Failed to create business queue ${queueName}: ${assertErr.message}`);
|
|
483
435
|
}
|
|
484
436
|
}
|
|
485
437
|
|
|
@@ -114,6 +114,40 @@ class RabbitMQClient extends EventEmitter {
|
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Recreates the publish channel if it's closed.
|
|
119
|
+
* Used when channel closes unexpectedly (e.g., due to 406 errors).
|
|
120
|
+
* @private
|
|
121
|
+
* @returns {Promise<void>}
|
|
122
|
+
* @throws {Error} If connection is not available or channel recreation fails.
|
|
123
|
+
*/
|
|
124
|
+
async _recreateChannel() {
|
|
125
|
+
if (!this._connection) {
|
|
126
|
+
throw new Error('Cannot recreate channel: connection is not available');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
// Close old channel if it exists
|
|
131
|
+
if (this._channel) {
|
|
132
|
+
try {
|
|
133
|
+
await this._channel.close();
|
|
134
|
+
} catch (err) {
|
|
135
|
+
// Ignore errors when closing already-closed channel
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Create new ConfirmChannel
|
|
140
|
+
this._channel = await this._connection.createConfirmChannel();
|
|
141
|
+
this._channel.on('error', (err) => this.emit('error', err));
|
|
142
|
+
this._channel.on('close', () => {
|
|
143
|
+
this.emit('error', new Error('RabbitMQ channel closed unexpectedly'));
|
|
144
|
+
});
|
|
145
|
+
} catch (err) {
|
|
146
|
+
this._channel = null;
|
|
147
|
+
throw new Error(`Failed to recreate channel: ${err.message}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
117
151
|
/**
|
|
118
152
|
* Disconnects: closes channel and connection.
|
|
119
153
|
* @returns {Promise<void>}
|
|
@@ -158,6 +192,16 @@ class RabbitMQClient extends EventEmitter {
|
|
|
158
192
|
if (!this._channel) {
|
|
159
193
|
throw new Error('Cannot publish: channel is not initialized');
|
|
160
194
|
}
|
|
195
|
+
|
|
196
|
+
// Check if channel is closed and recreate if needed
|
|
197
|
+
if (this._channel.closed) {
|
|
198
|
+
console.warn('[RabbitMQClient] Publish channel is closed, recreating...');
|
|
199
|
+
try {
|
|
200
|
+
await this._recreateChannel();
|
|
201
|
+
} catch (recreateErr) {
|
|
202
|
+
throw new Error(`Cannot publish: channel is closed and recreation failed: ${recreateErr.message}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
161
205
|
|
|
162
206
|
const exchange = this._config.exchange || '';
|
|
163
207
|
const routingKey = options.routingKey || queue;
|
|
@@ -177,19 +221,47 @@ class RabbitMQClient extends EventEmitter {
|
|
|
177
221
|
// Business queue - should already exist, skip checkQueue() to avoid channel closure
|
|
178
222
|
console.log(`[RabbitMQClient] DEBUG: Business queue ${queue}, skipping checkQueue() in publish() - queue should already exist`);
|
|
179
223
|
} else {
|
|
180
|
-
// Infrastructure queue -
|
|
224
|
+
// Infrastructure queue - use assertQueue() directly (idempotent, won't cause 406)
|
|
225
|
+
// assertQueue() will use existing queue even if args differ, avoiding channel closure
|
|
226
|
+
// Recreate queueChannel if it's closed
|
|
227
|
+
if (!this._queueChannel || this._queueChannel.closed) {
|
|
228
|
+
if (!this._connection) {
|
|
229
|
+
throw new Error('Cannot assertQueue: connection is not available');
|
|
230
|
+
}
|
|
231
|
+
console.warn('[RabbitMQClient] Queue channel is closed, recreating...');
|
|
232
|
+
try {
|
|
233
|
+
if (this._queueChannel) {
|
|
234
|
+
try {
|
|
235
|
+
await this._queueChannel.close();
|
|
236
|
+
} catch (err) {
|
|
237
|
+
// Ignore errors when closing already-closed channel
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
this._queueChannel = await this._connection.createChannel();
|
|
241
|
+
this._queueChannel._createdAt = new Date().toISOString();
|
|
242
|
+
this._queueChannel._closeReason = null;
|
|
243
|
+
this._queueChannel._lastOperation = null;
|
|
244
|
+
this._queueChannel.on('error', (err) => {
|
|
245
|
+
console.error('[RabbitMQClient] Queue channel error:', err.message);
|
|
246
|
+
this._queueChannel._closeReason = `Error: ${err.message} (code: ${err.code})`;
|
|
247
|
+
});
|
|
248
|
+
this._queueChannel.on('close', () => {
|
|
249
|
+
console.error('[RabbitMQClient] Queue channel closed');
|
|
250
|
+
});
|
|
251
|
+
} catch (recreateErr) {
|
|
252
|
+
console.warn(`[RabbitMQClient] Failed to recreate queue channel, proceeding anyway:`, recreateErr.message);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
181
256
|
const queueOptions = this._getQueueOptions(queue);
|
|
182
257
|
try {
|
|
183
|
-
|
|
184
|
-
// Queue exists - proceed to publish
|
|
185
|
-
} catch (checkErr) {
|
|
186
|
-
// If queue doesn't exist (404), create it
|
|
187
|
-
if (checkErr.code === 404) {
|
|
258
|
+
if (this._queueChannel && !this._queueChannel.closed) {
|
|
188
259
|
await this._queueChannel.assertQueue(queue, queueOptions);
|
|
189
|
-
} else {
|
|
190
|
-
// Other error (including 406) - queue exists with different args, proceed
|
|
191
|
-
console.warn(`[RabbitMQClient] Queue ${queue} exists with different arguments, using as-is`);
|
|
192
260
|
}
|
|
261
|
+
} catch (assertErr) {
|
|
262
|
+
// If assertQueue fails (shouldn't happen), log and proceed anyway
|
|
263
|
+
// The queue likely exists with different args - publish will work or fail clearly
|
|
264
|
+
console.warn(`[RabbitMQClient] assertQueue failed for ${queue}, proceeding anyway:`, assertErr.message);
|
|
193
265
|
}
|
|
194
266
|
}
|
|
195
267
|
// Publish using ConfirmChannel (for publisher confirms)
|