@autofleet/rabbit 3.4.0-beta--beta--0.0 → 3.4.0-beta.0
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/dist/index.d.ts +17 -14
- package/dist/index.js +137 -129
- package/dist/lib/consts.d.ts +0 -2
- package/dist/lib/consts.js +1 -3
- package/dist/lib/types.d.ts +8 -0
- package/package.json +1 -1
- package/src/index.ts +165 -146
- package/src/lib/consts.ts +0 -2
- package/src/lib/types.ts +9 -0
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars */
|
|
3
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
4
|
};
|
|
6
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
6
|
exports.sendCeleryTaskViaHttp = void 0;
|
|
7
|
+
/* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars,no-param-reassign */
|
|
8
8
|
const events_1 = require("events");
|
|
9
9
|
const util_1 = require("util");
|
|
10
10
|
const moment_1 = __importDefault(require("moment"));
|
|
@@ -164,9 +164,23 @@ class RabbitMq {
|
|
|
164
164
|
}
|
|
165
165
|
};
|
|
166
166
|
this.em = new events_1.EventEmitter();
|
|
167
|
-
this.
|
|
167
|
+
this.publishChannel = null;
|
|
168
168
|
this.publishChannelSetupPromise = null;
|
|
169
169
|
this.connection = null;
|
|
170
|
+
this.publishConnection = {
|
|
171
|
+
connection: null,
|
|
172
|
+
creatingConnection: false,
|
|
173
|
+
connectionCreatedEventName: 'publishConnectionCreated',
|
|
174
|
+
connectionFailedEventName: 'publishConnectionFailed',
|
|
175
|
+
blockReconnect: false,
|
|
176
|
+
};
|
|
177
|
+
this.consumeConnection = {
|
|
178
|
+
connection: null,
|
|
179
|
+
creatingConnection: false,
|
|
180
|
+
connectionCreatedEventName: 'consumeConnectionCreated',
|
|
181
|
+
connectionFailedEventName: 'consumeConnectionFailed',
|
|
182
|
+
blockReconnect: false,
|
|
183
|
+
};
|
|
170
184
|
this.creatingConnection = false;
|
|
171
185
|
this.exchanges = {};
|
|
172
186
|
this.queues = {};
|
|
@@ -190,9 +204,22 @@ class RabbitMq {
|
|
|
190
204
|
}
|
|
191
205
|
// TODO: [QUORUM-PHASE-3] Delete the old properties that we use for the old consumers and publishers
|
|
192
206
|
this.oldEm = new events_1.EventEmitter();
|
|
193
|
-
this.
|
|
207
|
+
this.oldPublishChannel = null;
|
|
194
208
|
this.oldPublishChannelSetupPromise = null;
|
|
195
|
-
this.
|
|
209
|
+
this.oldPublishConnection = {
|
|
210
|
+
connection: null,
|
|
211
|
+
creatingConnection: false,
|
|
212
|
+
connectionCreatedEventName: 'publishConnectionCreated',
|
|
213
|
+
connectionFailedEventName: 'publishConnectionFailed',
|
|
214
|
+
blockReconnect: false,
|
|
215
|
+
};
|
|
216
|
+
this.oldConsumeConnection = {
|
|
217
|
+
connection: null,
|
|
218
|
+
creatingConnection: false,
|
|
219
|
+
connectionCreatedEventName: 'consumeConnectionCreated',
|
|
220
|
+
connectionFailedEventName: 'consumeConnectionFailed',
|
|
221
|
+
blockReconnect: false,
|
|
222
|
+
};
|
|
196
223
|
this.oldCreatingConnection = false;
|
|
197
224
|
this.oldExchanges = {};
|
|
198
225
|
this.oldQueues = {};
|
|
@@ -201,30 +228,31 @@ class RabbitMq {
|
|
|
201
228
|
this.oldConsumers = [];
|
|
202
229
|
this.oldConsumersTags = [];
|
|
203
230
|
}
|
|
204
|
-
async getConnection() {
|
|
231
|
+
async getConnection(connection) {
|
|
205
232
|
return new Promise(async (resolve, reject) => {
|
|
206
|
-
|
|
233
|
+
const { connection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
|
|
234
|
+
if (blockReconnect) {
|
|
207
235
|
debug('rabbit: block reconnect');
|
|
208
236
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
209
237
|
// @ts-ignore
|
|
210
238
|
return resolve();
|
|
211
239
|
}
|
|
212
|
-
if (
|
|
213
|
-
if (this.options?.disableReconnect ||
|
|
240
|
+
if (connectionLocal !== null) {
|
|
241
|
+
if (this.options?.disableReconnect || connectionLocal?.isConnected()) {
|
|
214
242
|
debug('rabbit: connection - is connected');
|
|
215
243
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
216
244
|
// @ts-ignore
|
|
217
|
-
return resolve(
|
|
245
|
+
return resolve(connectionLocal);
|
|
218
246
|
}
|
|
219
247
|
debug('rabbit: connection - reconnecting');
|
|
220
248
|
}
|
|
221
|
-
if (
|
|
249
|
+
if (creatingConnection) {
|
|
222
250
|
debug('rabbit: creating connection emi');
|
|
223
|
-
this.em.once(
|
|
224
|
-
this.em.once(
|
|
251
|
+
this.em.once(connectionCreatedEventName, resolve);
|
|
252
|
+
this.em.once(connectionFailedEventName, reject);
|
|
225
253
|
return;
|
|
226
254
|
}
|
|
227
|
-
|
|
255
|
+
connection.creatingConnection = true;
|
|
228
256
|
let isResolved = false;
|
|
229
257
|
// It is import to use it as a function and not as a variable
|
|
230
258
|
// because of k8s changes the env variables
|
|
@@ -237,58 +265,58 @@ class RabbitMq {
|
|
|
237
265
|
return [`amqp://${userName}:${password}@${host}/${this.vhost}?heartbeat=${HEARTBEAT}`];
|
|
238
266
|
};
|
|
239
267
|
const defaultUrls = findServers();
|
|
240
|
-
const
|
|
268
|
+
const newConnection = await (0, amqp_connection_manager_1.connect)(defaultUrls, {
|
|
241
269
|
findServers,
|
|
242
270
|
});
|
|
243
|
-
|
|
244
|
-
|
|
271
|
+
connection.connection = newConnection;
|
|
272
|
+
newConnection.on('error', (err) => {
|
|
245
273
|
logger_1.default.error('rabbit: connection error', { err });
|
|
246
274
|
if (!isResolved) {
|
|
247
275
|
isResolved = true;
|
|
248
276
|
reject(err);
|
|
249
|
-
this.em.emit(
|
|
277
|
+
this.em.emit(connectionFailedEventName, err);
|
|
250
278
|
}
|
|
251
279
|
});
|
|
252
|
-
|
|
280
|
+
newConnection.on('connectFailed', (err) => {
|
|
253
281
|
this.consumersTags = [];
|
|
254
282
|
logger_1.default.error('rabbit: connection connectFailed', { err, advice: 'Check if the vhost exist', vhost: this.vhost });
|
|
255
283
|
if (!isResolved) {
|
|
256
284
|
isResolved = true;
|
|
257
285
|
reject(err);
|
|
258
|
-
this.em.emit(
|
|
286
|
+
this.em.emit(connectionFailedEventName, err);
|
|
259
287
|
}
|
|
260
288
|
});
|
|
261
|
-
|
|
262
|
-
// this.
|
|
289
|
+
newConnection.on('disconnect', ({ err }) => {
|
|
290
|
+
// this.publishChannel = null;
|
|
263
291
|
this.consumersTags = [];
|
|
264
292
|
debug('rabbit: connection closed');
|
|
265
293
|
if (this.options?.disableReconnect) {
|
|
266
294
|
logger_1.default.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
267
|
-
|
|
295
|
+
connection.blockReconnect = true;
|
|
268
296
|
}
|
|
269
297
|
else {
|
|
270
298
|
logger_1.default.error(`${this.RECONNECT_MSG}${err && ` - ${err}`}`);
|
|
271
299
|
}
|
|
272
300
|
});
|
|
273
|
-
|
|
301
|
+
newConnection.once('connect', async () => {
|
|
274
302
|
debug('rabbit: connection established');
|
|
275
|
-
|
|
276
|
-
this.em.emit(
|
|
303
|
+
connection.creatingConnection = false;
|
|
304
|
+
this.em.emit(connectionCreatedEventName, connection);
|
|
277
305
|
isResolved = true;
|
|
278
|
-
resolve(
|
|
306
|
+
resolve(newConnection);
|
|
279
307
|
});
|
|
280
308
|
});
|
|
281
309
|
}
|
|
282
|
-
async getNewChannel({ name = (0, utils_1.rand)().toString(), onClose = null, options = {}
|
|
283
|
-
let
|
|
310
|
+
async getNewChannel({ name = (0, utils_1.rand)().toString(), onClose = null, options = {}, connection, }) {
|
|
311
|
+
let localConnection;
|
|
284
312
|
try {
|
|
285
|
-
|
|
313
|
+
localConnection = await this.getConnection(connection);
|
|
286
314
|
}
|
|
287
315
|
catch (e) {
|
|
288
316
|
logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
289
317
|
throw e;
|
|
290
318
|
}
|
|
291
|
-
const channel =
|
|
319
|
+
const channel = localConnection.createChannel({ ...options });
|
|
292
320
|
(0, events_1.once)(channel, 'close').then((args) => {
|
|
293
321
|
logger_1.default.error(`rabbit: channel ${name} closed`);
|
|
294
322
|
onClose?.(args);
|
|
@@ -303,18 +331,20 @@ class RabbitMq {
|
|
|
303
331
|
throw err;
|
|
304
332
|
}
|
|
305
333
|
}
|
|
306
|
-
async assertChannel({ force = false
|
|
334
|
+
async assertChannel({ force = false, connection }) {
|
|
307
335
|
if (!this.publishChannelSetupPromise) {
|
|
308
336
|
this.publishChannelSetupPromise = new Promise(async (resolve, reject) => {
|
|
309
|
-
if (this.
|
|
310
|
-
return resolve(this.
|
|
337
|
+
if (this.publishChannel && !force) {
|
|
338
|
+
return resolve(this.publishChannel);
|
|
311
339
|
}
|
|
312
340
|
try {
|
|
313
|
-
const channel = await this.getNewChannel({});
|
|
341
|
+
const channel = await this.getNewChannel({ connection });
|
|
314
342
|
channel.on('error', (err) => {
|
|
315
343
|
logger_1.default.error('rabbit: channel error', { err });
|
|
316
344
|
});
|
|
317
|
-
this.
|
|
345
|
+
if (this.publishConnection === connection) {
|
|
346
|
+
this.publishChannel = channel;
|
|
347
|
+
}
|
|
318
348
|
resolve(channel);
|
|
319
349
|
}
|
|
320
350
|
catch (e) {
|
|
@@ -324,8 +354,8 @@ class RabbitMq {
|
|
|
324
354
|
}
|
|
325
355
|
return this.publishChannelSetupPromise;
|
|
326
356
|
}
|
|
327
|
-
async assertExchange(exchangeName,
|
|
328
|
-
const channel = await this.assertChannel();
|
|
357
|
+
async assertExchange(exchangeName, connection) {
|
|
358
|
+
const channel = await this.assertChannel({ connection });
|
|
329
359
|
if (this.exchanges[exchangeName]) {
|
|
330
360
|
delete this.assertExchangePromises[exchangeName];
|
|
331
361
|
return this.exchanges[exchangeName];
|
|
@@ -340,16 +370,16 @@ class RabbitMq {
|
|
|
340
370
|
// TODO: [QUORUM-PHASE-2] Change the implementation to the new one
|
|
341
371
|
async getQueueLength(queue) {
|
|
342
372
|
RabbitMq.validateName('queue', queue);
|
|
343
|
-
const {
|
|
373
|
+
const { oldPublishChannel: channel } = this;
|
|
344
374
|
if (!channel) {
|
|
345
375
|
throw new rabbitError_1.default('channel is not defined');
|
|
346
376
|
}
|
|
347
377
|
debug('rabbit: getting queue length', { queue, connected: this.connection?.isConnected() });
|
|
348
378
|
return channel?.checkQueue(queue);
|
|
349
379
|
}
|
|
350
|
-
async deleteQueue(queue) {
|
|
380
|
+
async deleteQueue(queue, connection) {
|
|
351
381
|
RabbitMq.validateName('queue', queue);
|
|
352
|
-
const channel = await this.assertChannel();
|
|
382
|
+
const channel = await this.assertChannel({ connection });
|
|
353
383
|
logger_1.default.info('rabbit: deleting queue', { queue });
|
|
354
384
|
const deleteQueueRes = await channel.deleteQueue(queue);
|
|
355
385
|
debug('queue deleted', deleteQueueRes);
|
|
@@ -357,12 +387,13 @@ class RabbitMq {
|
|
|
357
387
|
}
|
|
358
388
|
// TODO: [QUORUM-PHASE-2] Change the implementation to the new one
|
|
359
389
|
async bindQueue(queue, exchange) {
|
|
360
|
-
const channel = await this.assertChannelOld();
|
|
390
|
+
const channel = await this.assertChannelOld({ connection: this.oldPublishConnection });
|
|
361
391
|
await channel.addSetup((setupChannel) => setupChannel.bindQueue(queue, exchange, ''));
|
|
362
392
|
return channel.bindQueue(queue, exchange, '');
|
|
363
393
|
}
|
|
364
394
|
async setupQueue(queueName, options) {
|
|
365
395
|
let queue;
|
|
396
|
+
const connection = this.publishConnection;
|
|
366
397
|
const localeOptions = {
|
|
367
398
|
...options,
|
|
368
399
|
durable: true,
|
|
@@ -373,7 +404,7 @@ class RabbitMq {
|
|
|
373
404
|
},
|
|
374
405
|
};
|
|
375
406
|
try {
|
|
376
|
-
const channel = await this.assertChannel();
|
|
407
|
+
const channel = await this.assertChannel({ connection });
|
|
377
408
|
debug('assertQueue->channel.addSetup', { queueName });
|
|
378
409
|
await channel.addSetup(async (setupChannel) => {
|
|
379
410
|
await setupChannel.assertQueue(queueName, localeOptions);
|
|
@@ -385,8 +416,8 @@ class RabbitMq {
|
|
|
385
416
|
logger_1.default.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
386
417
|
if (!this.options?.dontRetryAssert) {
|
|
387
418
|
debug('retrying assertQueue', { queueName });
|
|
388
|
-
const channel = await this.assertChannel({ force: true });
|
|
389
|
-
await this.deleteQueue(queueName);
|
|
419
|
+
const channel = await this.assertChannel({ force: true, connection });
|
|
420
|
+
await this.deleteQueue(queueName, connection);
|
|
390
421
|
debug('retrying assertQueue->channel.addSetup', { queueName });
|
|
391
422
|
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, localeOptions));
|
|
392
423
|
debug('retrying assertQueue->channel.assertQueue', { queueName });
|
|
@@ -477,7 +508,7 @@ class RabbitMq {
|
|
|
477
508
|
}
|
|
478
509
|
logger_1.default.info(`rabbit: Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
479
510
|
}
|
|
480
|
-
const channel = await this.getNewChannel({});
|
|
511
|
+
const channel = await this.getNewChannel({ connection: this.consumeConnection });
|
|
481
512
|
return channel.addSetup(async (confirmChannel) => {
|
|
482
513
|
const q = await this.assertQueue(queue, optionsWithDefaults);
|
|
483
514
|
await confirmChannel.prefetch(limit, false);
|
|
@@ -565,7 +596,10 @@ class RabbitMq {
|
|
|
565
596
|
if (options?.isQuorumQueue !== false) {
|
|
566
597
|
await this.assertVHost();
|
|
567
598
|
await this.saveConsumer(queue, callback, options);
|
|
568
|
-
const channel = await this.getNewChannel({
|
|
599
|
+
const channel = await this.getNewChannel({
|
|
600
|
+
name: `consume-exchange-${exchange}-queue-${queue}`,
|
|
601
|
+
connection: this.consumeConnection,
|
|
602
|
+
});
|
|
569
603
|
await channel.addSetup(async (c) => {
|
|
570
604
|
const assertExchange = await (0, utils_1.assertExchangeFanout)(c, exchange);
|
|
571
605
|
await c.assertQueue(queue);
|
|
@@ -579,7 +613,10 @@ class RabbitMq {
|
|
|
579
613
|
}
|
|
580
614
|
// TODO: [QUORUM-PHASE-3] Delete the old implementation
|
|
581
615
|
await this.saveConsumerOld(queue, callback, options);
|
|
582
|
-
const channelOld = await this.getNewChannelOld({
|
|
616
|
+
const channelOld = await this.getNewChannelOld({
|
|
617
|
+
name: `consume-exchange-${exchange}-queue-${queue}-old`,
|
|
618
|
+
connection: this.oldConsumeConnection,
|
|
619
|
+
});
|
|
583
620
|
await channelOld.addSetup(async (c) => {
|
|
584
621
|
const assertExchange = await (0, utils_1.assertExchangeFanout)(c, exchange);
|
|
585
622
|
await c.assertQueue(queue);
|
|
@@ -596,8 +633,8 @@ class RabbitMq {
|
|
|
596
633
|
async publish(exchange, content, customHeaders) {
|
|
597
634
|
return (0, utils_1.wrapSetImmediate)(async () => {
|
|
598
635
|
RabbitMq.validateName('exchange', exchange);
|
|
599
|
-
const channel = await this.assertChannelOld();
|
|
600
|
-
await this.assertExchangeOld(exchange);
|
|
636
|
+
const channel = await this.assertChannelOld({ connection: this.oldPublishConnection });
|
|
637
|
+
await this.assertExchangeOld(exchange, this.oldPublishConnection);
|
|
601
638
|
await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
|
|
602
639
|
});
|
|
603
640
|
}
|
|
@@ -605,7 +642,7 @@ class RabbitMq {
|
|
|
605
642
|
// TODO: [QUORUM-PHASE-2] Change the implementation to the new one
|
|
606
643
|
async sendToQueue(queue, content, options, customHeaders) {
|
|
607
644
|
try {
|
|
608
|
-
await this.assertChannelOld();
|
|
645
|
+
await this.assertChannelOld({ connection: this.oldPublishConnection });
|
|
609
646
|
}
|
|
610
647
|
catch (e) {
|
|
611
648
|
logger_1.default.error(`rabbit sendToQueue: failed to send assert channel when sending to queue ${queue}`, { e });
|
|
@@ -620,7 +657,7 @@ class RabbitMq {
|
|
|
620
657
|
throw e;
|
|
621
658
|
}
|
|
622
659
|
try {
|
|
623
|
-
const res = await this.
|
|
660
|
+
const res = await this.oldPublishChannel?.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
|
|
624
661
|
debug(`rabbit: sending to queue ${queue}`, { res });
|
|
625
662
|
return res;
|
|
626
663
|
}
|
|
@@ -630,51 +667,17 @@ class RabbitMq {
|
|
|
630
667
|
throw e;
|
|
631
668
|
}
|
|
632
669
|
}
|
|
633
|
-
async sendToCeleryQueue(data, taskName, queueName) {
|
|
634
|
-
const taskId = (0, node_crypto_1.randomUUID)();
|
|
635
|
-
// Celery-compatible message body
|
|
636
|
-
const taskPayload = {
|
|
637
|
-
task: taskName,
|
|
638
|
-
id: taskId,
|
|
639
|
-
args: [data],
|
|
640
|
-
kwargs: {},
|
|
641
|
-
retries: 0,
|
|
642
|
-
eta: null,
|
|
643
|
-
};
|
|
644
|
-
// Celery-compatible AMQP headers
|
|
645
|
-
const publishOptions = {
|
|
646
|
-
contentType: 'application/json',
|
|
647
|
-
contentEncoding: 'utf-8',
|
|
648
|
-
deliveryMode: 2,
|
|
649
|
-
headers: {
|
|
650
|
-
lang: 'py',
|
|
651
|
-
task: taskName,
|
|
652
|
-
id: taskId,
|
|
653
|
-
root_id: taskId,
|
|
654
|
-
parent_id: null,
|
|
655
|
-
group: null,
|
|
656
|
-
retries: 0,
|
|
657
|
-
eta: null,
|
|
658
|
-
},
|
|
659
|
-
};
|
|
660
|
-
try {
|
|
661
|
-
return await this.sendToQueue(queueName, taskPayload, undefined, publishOptions);
|
|
662
|
-
}
|
|
663
|
-
catch (e) {
|
|
664
|
-
const isConnected = await this.isConnected();
|
|
665
|
-
logger_1.default.error(`rabbit sendToQueue: failed to send to queue ${queueName}, isConnected: ${isConnected}`, { e });
|
|
666
|
-
throw e;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
670
|
// TODO: [QUORUM-PHASE-2] After changing the publish from old to new change from the old to the new implementation
|
|
670
671
|
async isConnected() {
|
|
671
|
-
|
|
672
|
-
const
|
|
672
|
+
debug('rabbit: start is connected');
|
|
673
|
+
const consumeConnection = await this.getConnectionOld(this.oldConsumeConnection);
|
|
674
|
+
const publishConnection = await this.getConnectionOld(this.oldPublishConnection);
|
|
675
|
+
const isConnected = consumeConnection.isConnected() && publishConnection.isConnected();
|
|
673
676
|
if (!isConnected) {
|
|
674
677
|
logger_1.default.error('rabbit: isConnected - false');
|
|
675
678
|
return false;
|
|
676
679
|
}
|
|
677
|
-
const channel = await this.assertChannelOld();
|
|
680
|
+
const channel = await this.assertChannelOld({ connection: this.oldPublishConnection });
|
|
678
681
|
try {
|
|
679
682
|
await Promise.all([
|
|
680
683
|
channel.waitForConnect(),
|
|
@@ -685,7 +688,7 @@ class RabbitMq {
|
|
|
685
688
|
logger_1.default.error('rabbit: isConnected - false');
|
|
686
689
|
return false;
|
|
687
690
|
}
|
|
688
|
-
logger_1.default.
|
|
691
|
+
logger_1.default.debug('rabbit: isConnected - true');
|
|
689
692
|
return true;
|
|
690
693
|
}
|
|
691
694
|
async gracefulShutdown(signal) {
|
|
@@ -718,7 +721,7 @@ class RabbitMq {
|
|
|
718
721
|
}
|
|
719
722
|
logger_1.default.info(`rabbit: Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
|
|
720
723
|
}
|
|
721
|
-
const channel = await this.getNewChannelOld({});
|
|
724
|
+
const channel = await this.getNewChannelOld({ connection: this.oldConsumeConnection });
|
|
722
725
|
return channel.addSetup(async (confirmChannel) => {
|
|
723
726
|
const q = await this.assertQueueOld(queue, optionsWithDefaults);
|
|
724
727
|
await confirmChannel.prefetch(limit, false);
|
|
@@ -797,16 +800,16 @@ class RabbitMq {
|
|
|
797
800
|
});
|
|
798
801
|
}
|
|
799
802
|
// TODO: [QUORUM-PHASE-3] Delete all the function under this line (getNewChannelOld, getConnectionOld, assertQueueOld, setupQueueOld, saveConsumerOld)
|
|
800
|
-
async getNewChannelOld({ name = (0, utils_1.rand)().toString(), onClose = null, options = {}
|
|
801
|
-
let
|
|
803
|
+
async getNewChannelOld({ name = (0, utils_1.rand)().toString(), onClose = null, options = {}, connection, }) {
|
|
804
|
+
let localConnection;
|
|
802
805
|
try {
|
|
803
|
-
|
|
806
|
+
localConnection = await this.getConnectionOld(connection);
|
|
804
807
|
}
|
|
805
808
|
catch (e) {
|
|
806
809
|
logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
807
810
|
throw e;
|
|
808
811
|
}
|
|
809
|
-
const channel =
|
|
812
|
+
const channel = localConnection.createChannel({ ...options });
|
|
810
813
|
(0, events_1.once)(channel, 'close').then((args) => {
|
|
811
814
|
logger_1.default.error(`rabbit: channel ${name} closed`);
|
|
812
815
|
onClose?.(args);
|
|
@@ -821,30 +824,31 @@ class RabbitMq {
|
|
|
821
824
|
throw err;
|
|
822
825
|
}
|
|
823
826
|
}
|
|
824
|
-
async getConnectionOld() {
|
|
827
|
+
async getConnectionOld(connection) {
|
|
825
828
|
return new Promise(async (resolve, reject) => {
|
|
826
|
-
|
|
829
|
+
const { connection: connectionLocal, creatingConnection, connectionCreatedEventName, connectionFailedEventName, blockReconnect, } = connection;
|
|
830
|
+
if (blockReconnect) {
|
|
827
831
|
debug('rabbit: block reconnect');
|
|
828
832
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
829
833
|
// @ts-ignore
|
|
830
834
|
return resolve();
|
|
831
835
|
}
|
|
832
|
-
if (
|
|
833
|
-
if (this.options?.disableReconnect ||
|
|
836
|
+
if (connectionLocal !== null) {
|
|
837
|
+
if (this.options?.disableReconnect || connectionLocal?.isConnected()) {
|
|
834
838
|
debug('rabbit: connection - is connected');
|
|
835
839
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
836
840
|
// @ts-ignore
|
|
837
|
-
return resolve(
|
|
841
|
+
return resolve(connectionLocal);
|
|
838
842
|
}
|
|
839
843
|
debug('rabbit: connection - reconnecting');
|
|
840
844
|
}
|
|
841
|
-
if (
|
|
845
|
+
if (creatingConnection) {
|
|
842
846
|
debug('rabbit: creating connection emi');
|
|
843
|
-
this.oldEm.once(
|
|
844
|
-
this.oldEm.once(
|
|
847
|
+
this.oldEm.once(connectionCreatedEventName, resolve);
|
|
848
|
+
this.oldEm.once(connectionFailedEventName, reject);
|
|
845
849
|
return;
|
|
846
850
|
}
|
|
847
|
-
|
|
851
|
+
connection.creatingConnection = true;
|
|
848
852
|
let isResolved = false;
|
|
849
853
|
// It is import to use it as a function and not as a variable
|
|
850
854
|
// because of k8s changes the env variables
|
|
@@ -857,44 +861,44 @@ class RabbitMq {
|
|
|
857
861
|
return [`amqp://${userName}:${password}@${host}?heartbeat=${HEARTBEAT}`];
|
|
858
862
|
};
|
|
859
863
|
const defaultUrls = findServers();
|
|
860
|
-
const
|
|
864
|
+
const newConnection = await (0, amqp_connection_manager_1.connect)(defaultUrls, {
|
|
861
865
|
findServers,
|
|
862
866
|
});
|
|
863
|
-
|
|
864
|
-
|
|
867
|
+
connection.connection = newConnection;
|
|
868
|
+
newConnection.on('error', (err) => {
|
|
865
869
|
logger_1.default.error('rabbit: connection error', { err });
|
|
866
870
|
if (!isResolved) {
|
|
867
871
|
isResolved = true;
|
|
868
872
|
reject(err);
|
|
869
|
-
this.oldEm.emit(
|
|
873
|
+
this.oldEm.emit(connectionFailedEventName, err);
|
|
870
874
|
}
|
|
871
875
|
});
|
|
872
|
-
|
|
876
|
+
newConnection.on('connectFailed', (err) => {
|
|
873
877
|
this.oldConsumersTags = [];
|
|
874
878
|
logger_1.default.error('rabbit: connection connectFailed', { err });
|
|
875
879
|
if (!isResolved) {
|
|
876
880
|
isResolved = true;
|
|
877
881
|
reject(err);
|
|
878
|
-
this.oldEm.emit(
|
|
882
|
+
this.oldEm.emit(connectionFailedEventName, err);
|
|
879
883
|
}
|
|
880
884
|
});
|
|
881
|
-
|
|
885
|
+
newConnection.on('disconnect', ({ err }) => {
|
|
882
886
|
this.oldConsumersTags = [];
|
|
883
887
|
debug('rabbit: connection closed');
|
|
884
888
|
if (this.options?.disableReconnect) {
|
|
885
889
|
logger_1.default.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
886
|
-
|
|
890
|
+
connection.blockReconnect = true;
|
|
887
891
|
}
|
|
888
892
|
else {
|
|
889
893
|
logger_1.default.error(`${this.RECONNECT_MSG}${err && ` - ${err}`}`);
|
|
890
894
|
}
|
|
891
895
|
});
|
|
892
|
-
|
|
896
|
+
newConnection.once('connect', async () => {
|
|
893
897
|
debug('rabbit: connection established');
|
|
894
|
-
|
|
895
|
-
this.oldEm.emit(
|
|
898
|
+
connection.creatingConnection = false;
|
|
899
|
+
this.oldEm.emit(connectionCreatedEventName, connection);
|
|
896
900
|
isResolved = true;
|
|
897
|
-
resolve(
|
|
901
|
+
resolve(newConnection);
|
|
898
902
|
});
|
|
899
903
|
});
|
|
900
904
|
}
|
|
@@ -923,6 +927,7 @@ class RabbitMq {
|
|
|
923
927
|
}
|
|
924
928
|
async setupQueueOld(queueName, options) {
|
|
925
929
|
let queue;
|
|
930
|
+
const connection = this.oldPublishConnection;
|
|
926
931
|
const shouldUseQuorum = RabbitMq.shouldUseQuorum(queueName);
|
|
927
932
|
const localeOptions = {
|
|
928
933
|
...options,
|
|
@@ -934,7 +939,7 @@ class RabbitMq {
|
|
|
934
939
|
},
|
|
935
940
|
};
|
|
936
941
|
try {
|
|
937
|
-
const channel = await this.assertChannelOld();
|
|
942
|
+
const channel = await this.assertChannelOld({ connection });
|
|
938
943
|
debug('assertQueue->channel.addSetup', { queueName });
|
|
939
944
|
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, localeOptions));
|
|
940
945
|
debug('assertQueue->channel.assertQueue', { queueName });
|
|
@@ -944,8 +949,8 @@ class RabbitMq {
|
|
|
944
949
|
logger_1.default.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
945
950
|
if (!this.options?.dontRetryAssert) {
|
|
946
951
|
debug('retrying assertQueue', { queueName });
|
|
947
|
-
const channel = await this.assertChannelOld({ force: true });
|
|
948
|
-
await this.deleteQueueOld(queueName);
|
|
952
|
+
const channel = await this.assertChannelOld({ force: true, connection });
|
|
953
|
+
await this.deleteQueueOld(queueName, connection);
|
|
949
954
|
debug('retrying assertQueue->channel.addSetup', { queueName });
|
|
950
955
|
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, localeOptions));
|
|
951
956
|
debug('retrying assertQueue->channel.assertQueue', { queueName });
|
|
@@ -958,18 +963,21 @@ class RabbitMq {
|
|
|
958
963
|
this.oldQueues[queueName] = queue;
|
|
959
964
|
return queue;
|
|
960
965
|
}
|
|
961
|
-
async assertChannelOld({ force = false
|
|
966
|
+
async assertChannelOld({ force = false, connection }) {
|
|
962
967
|
if (!this.oldPublishChannelSetupPromise) {
|
|
963
968
|
this.oldPublishChannelSetupPromise = new Promise(async (resolve, reject) => {
|
|
964
|
-
if (this.
|
|
965
|
-
return resolve(this.
|
|
969
|
+
if (this.oldPublishChannel && !force) {
|
|
970
|
+
return resolve(this.oldPublishChannel);
|
|
966
971
|
}
|
|
967
972
|
try {
|
|
968
|
-
const channel = await this.getNewChannelOld({});
|
|
973
|
+
const channel = await this.getNewChannelOld({ connection });
|
|
969
974
|
channel.on('error', (err) => {
|
|
970
975
|
logger_1.default.error('rabbit: channel error', { err });
|
|
971
976
|
});
|
|
972
|
-
this.
|
|
977
|
+
if (this.oldPublishConnection === connection) {
|
|
978
|
+
debug('rabbit: a new channel assigned to the oldPublishChannel', { connection });
|
|
979
|
+
this.oldPublishChannel = channel;
|
|
980
|
+
}
|
|
973
981
|
resolve(channel);
|
|
974
982
|
}
|
|
975
983
|
catch (e) {
|
|
@@ -979,16 +987,16 @@ class RabbitMq {
|
|
|
979
987
|
}
|
|
980
988
|
return this.oldPublishChannelSetupPromise;
|
|
981
989
|
}
|
|
982
|
-
async deleteQueueOld(queue) {
|
|
990
|
+
async deleteQueueOld(queue, connection) {
|
|
983
991
|
RabbitMq.validateName('queue', queue);
|
|
984
|
-
const channel = await this.assertChannelOld();
|
|
992
|
+
const channel = await this.assertChannelOld({ connection });
|
|
985
993
|
logger_1.default.info('rabbit: deleting queue', { queue });
|
|
986
994
|
const deleteQueueRes = await channel.deleteQueue(queue);
|
|
987
995
|
debug('queue deleted', deleteQueueRes);
|
|
988
996
|
return deleteQueueRes;
|
|
989
997
|
}
|
|
990
|
-
async assertExchangeOld(exchangeName,
|
|
991
|
-
const channel = await this.assertChannelOld();
|
|
998
|
+
async assertExchangeOld(exchangeName, connection) {
|
|
999
|
+
const channel = await this.assertChannelOld({ connection });
|
|
992
1000
|
if (this.oldExchanges[exchangeName]) {
|
|
993
1001
|
delete this.oldAssertExchangePromises[exchangeName];
|
|
994
1002
|
return this.oldExchanges[exchangeName];
|
package/dist/lib/consts.d.ts
CHANGED
|
@@ -6,8 +6,6 @@ export declare const USER_TRACING_HEADER = "x-af-user-id";
|
|
|
6
6
|
export declare const AUTOMATION_ID_HEADER = "x-af-automation-id";
|
|
7
7
|
export declare const USER_OBJECT = "userObject";
|
|
8
8
|
export declare const DEFAULT_USE_CONSUME_WITH_LOCK = false;
|
|
9
|
-
export declare const CONNECTION_CREATED_CONST = "connectionCreated";
|
|
10
|
-
export declare const CONNECTION_FAILED_CONST = "connectionFailed";
|
|
11
9
|
export declare const DEFAULT_OPTIONS: {
|
|
12
10
|
limit: number;
|
|
13
11
|
retries: number;
|
package/dist/lib/consts.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_OPTIONS = exports.
|
|
3
|
+
exports.DEFAULT_OPTIONS = exports.DEFAULT_USE_CONSUME_WITH_LOCK = exports.USER_OBJECT = exports.AUTOMATION_ID_HEADER = exports.USER_TRACING_HEADER = exports.TRACING_HEADER = exports.RETRY_HEADER = exports.DEFAULT_LOCK_TIMEOUT = exports.DEFAULT_DEAD_TTL_TWO_DAYS = void 0;
|
|
4
4
|
exports.DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 12;
|
|
5
5
|
exports.DEFAULT_LOCK_TIMEOUT = 1000 * 5;
|
|
6
6
|
exports.RETRY_HEADER = 'x-retry-count';
|
|
@@ -9,8 +9,6 @@ exports.USER_TRACING_HEADER = 'x-af-user-id';
|
|
|
9
9
|
exports.AUTOMATION_ID_HEADER = 'x-af-automation-id';
|
|
10
10
|
exports.USER_OBJECT = 'userObject';
|
|
11
11
|
exports.DEFAULT_USE_CONSUME_WITH_LOCK = false;
|
|
12
|
-
exports.CONNECTION_CREATED_CONST = 'connectionCreated';
|
|
13
|
-
exports.CONNECTION_FAILED_CONST = 'connectionFailed';
|
|
14
12
|
exports.DEFAULT_OPTIONS = {
|
|
15
13
|
limit: 1,
|
|
16
14
|
retries: 1,
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AmqpConnectionManager } from 'amqp-connection-manager';
|
|
1
2
|
import { ConsumeMessage, Options, Replies } from 'amqplib';
|
|
2
3
|
export interface ExchangesCache {
|
|
3
4
|
[key: string]: any;
|
|
@@ -42,3 +43,10 @@ export type AfConsumer = {
|
|
|
42
43
|
options: ConsumeOptions | undefined;
|
|
43
44
|
};
|
|
44
45
|
export declare const CONSUMER_DEFAULT_OPTIONS: Options.Consume;
|
|
46
|
+
export type ConnectionData = {
|
|
47
|
+
connection: AmqpConnectionManager | null;
|
|
48
|
+
creatingConnection: boolean;
|
|
49
|
+
connectionCreatedEventName: string;
|
|
50
|
+
connectionFailedEventName: string;
|
|
51
|
+
blockReconnect: boolean;
|
|
52
|
+
};
|