@autofleet/rabbit 3.2.26-beta.3 → 3.2.26-beta.4
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 +2 -2
- package/dist/index.js +36 -16
- package/package.json +1 -1
- package/src/index.ts +38 -16
package/dist/index.d.ts
CHANGED
|
@@ -89,9 +89,9 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
89
89
|
getQueueLength(queue: string, connectionPurpose?: ConnectionPurpose): Promise<Replies.AssertQueue>;
|
|
90
90
|
private deleteQueue;
|
|
91
91
|
bindQueue(queue: string, exchange: string): Promise<void>;
|
|
92
|
-
setupQueue(queueName: string,
|
|
92
|
+
setupQueue(queueName: string, options?: Options.AssertQueue): Promise<Replies.AssertQueue>;
|
|
93
93
|
static shouldUseQuorum(queueName: string): boolean;
|
|
94
|
-
assertQueue(queueName: string,
|
|
94
|
+
assertQueue(queueName: string, options?: Options.AssertQueue): Promise<any>;
|
|
95
95
|
private saveConsumer;
|
|
96
96
|
consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
|
|
97
97
|
private lockRedisIfNeeded;
|
package/dist/index.js
CHANGED
|
@@ -261,7 +261,7 @@ class RabbitMq {
|
|
|
261
261
|
}
|
|
262
262
|
async assertChannel({ force = false, connectionPurpose = consts_1.ConnectionPurpose.Consume }) {
|
|
263
263
|
debug('rabbit: start assert channel', { connectionPurpose, publishPromise: this.publishChannelSetupPromise, channel: this.publishChannel });
|
|
264
|
-
if (!this.publishChannelSetupPromise
|
|
264
|
+
if (!this.publishChannelSetupPromise) {
|
|
265
265
|
this.publishChannelSetupPromise = new Promise(async (resolve, reject) => {
|
|
266
266
|
if (this.publishChannel && !force) {
|
|
267
267
|
return resolve(this.publishChannel);
|
|
@@ -320,8 +320,9 @@ class RabbitMq {
|
|
|
320
320
|
await channel.addSetup((setupChannel) => setupChannel.bindQueue(queue, exchange, ''));
|
|
321
321
|
return channel.bindQueue(queue, exchange, '');
|
|
322
322
|
}
|
|
323
|
-
async setupQueue(queueName,
|
|
323
|
+
async setupQueue(queueName, options) {
|
|
324
324
|
let queue;
|
|
325
|
+
const connectionPurpose = consts_1.ConnectionPurpose.Publish;
|
|
325
326
|
const shouldUseQuorum = RabbitMq.shouldUseQuorum(queueName);
|
|
326
327
|
const localeOptions = {
|
|
327
328
|
...options,
|
|
@@ -368,8 +369,8 @@ class RabbitMq {
|
|
|
368
369
|
}
|
|
369
370
|
return false;
|
|
370
371
|
}
|
|
371
|
-
async assertQueue(queueName,
|
|
372
|
-
debug('rabbit: start assert queue', {
|
|
372
|
+
async assertQueue(queueName, options) {
|
|
373
|
+
debug('rabbit: start assert queue', { queueName });
|
|
373
374
|
RabbitMq.validateName('queue', queueName);
|
|
374
375
|
if (this.queues[queueName]) {
|
|
375
376
|
delete this.queueSetupPromises[queueName];
|
|
@@ -378,8 +379,8 @@ class RabbitMq {
|
|
|
378
379
|
if (this.queueSetupPromises[queueName]) {
|
|
379
380
|
return this.queueSetupPromises[queueName];
|
|
380
381
|
}
|
|
381
|
-
this.queueSetupPromises[queueName] = this.setupQueue(queueName,
|
|
382
|
-
debug('rabbit: done assert queue', {
|
|
382
|
+
this.queueSetupPromises[queueName] = this.setupQueue(queueName, options);
|
|
383
|
+
debug('rabbit: done assert queue', { queueName });
|
|
383
384
|
return this.queueSetupPromises[queueName];
|
|
384
385
|
}
|
|
385
386
|
saveConsumer(queue, callback, options) {
|
|
@@ -424,7 +425,7 @@ class RabbitMq {
|
|
|
424
425
|
}
|
|
425
426
|
const channel = await this.getNewChannel({ connectionPurpose: consts_1.ConnectionPurpose.Consume });
|
|
426
427
|
return channel.addSetup(async (confirmChannel) => {
|
|
427
|
-
const q = await this.assertQueue(queue,
|
|
428
|
+
const q = await this.assertQueue(queue, optionsWithDefaults);
|
|
428
429
|
await confirmChannel.prefetch(limit, false);
|
|
429
430
|
const { consumerTag } = await confirmChannel.consume(queue, async (msg) => {
|
|
430
431
|
if (!msg) {
|
|
@@ -537,7 +538,7 @@ class RabbitMq {
|
|
|
537
538
|
}
|
|
538
539
|
try {
|
|
539
540
|
RabbitMq.validateName('queue', queue);
|
|
540
|
-
await this.assertQueue(queue,
|
|
541
|
+
await this.assertQueue(queue, options);
|
|
541
542
|
}
|
|
542
543
|
catch (e) {
|
|
543
544
|
logger_1.default.error(`rabbit sendToQueue: failed to assert queue ${queue}`, { e });
|
|
@@ -553,10 +554,29 @@ class RabbitMq {
|
|
|
553
554
|
throw e;
|
|
554
555
|
}
|
|
555
556
|
}
|
|
557
|
+
// const connection = await this.getConnection();
|
|
558
|
+
// const isConnected = connection.isConnected();
|
|
559
|
+
// if (!isConnected) {
|
|
560
|
+
// logger.error('rabbit: isConnected - false');
|
|
561
|
+
// return false;
|
|
562
|
+
// }
|
|
563
|
+
// const channel: any = await this.assertChannel();
|
|
564
|
+
// try {
|
|
565
|
+
// await Promise.all([
|
|
566
|
+
// channel.waitForConnect(),
|
|
567
|
+
// ...this.consumers.map((c: AfConsumer) => channel.checkQueue(c.queue)),
|
|
568
|
+
// ]);
|
|
569
|
+
// } catch (e) {
|
|
570
|
+
// logger.error('rabbit: isConnected - false');
|
|
571
|
+
// return false;
|
|
572
|
+
// }
|
|
573
|
+
// logger.info('rabbit: isConnected - true');
|
|
574
|
+
// return true;
|
|
556
575
|
async isConnected() {
|
|
557
576
|
debug('rabbit: start is connected');
|
|
558
577
|
const isEachConnectionConnected = await Promise.all(Object.entries(this.connectionsMap).map(async ([connectionPurpose, connectionData]) => {
|
|
559
|
-
|
|
578
|
+
const { connection } = connectionData;
|
|
579
|
+
if (connectionPurpose === consts_1.ConnectionPurpose.Publish && connection) {
|
|
560
580
|
const channel = await this.assertChannel({ connectionPurpose: connectionPurpose });
|
|
561
581
|
try {
|
|
562
582
|
await Promise.all([
|
|
@@ -569,14 +589,14 @@ class RabbitMq {
|
|
|
569
589
|
return false;
|
|
570
590
|
}
|
|
571
591
|
}
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
592
|
+
if (connection) {
|
|
593
|
+
const isConnected = connection?.isConnected();
|
|
594
|
+
if (!isConnected) {
|
|
595
|
+
logger_1.default.error('rabbit: isConnected - false', { connectionPurpose });
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
logger_1.default.info('rabbit: isConnected - true', { connectionPurpose });
|
|
578
599
|
}
|
|
579
|
-
logger_1.default.info('rabbit: isConnected - true');
|
|
580
600
|
return true;
|
|
581
601
|
}));
|
|
582
602
|
return isEachConnectionConnected.every((isConnected) => isConnected === true);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -412,7 +412,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
412
412
|
|
|
413
413
|
async assertChannel({ force = false, connectionPurpose = ConnectionPurpose.Consume }: assertChannelOpts): Promise<ChannelWrapper> {
|
|
414
414
|
debug('rabbit: start assert channel', { connectionPurpose, publishPromise: this.publishChannelSetupPromise, channel: this.publishChannel });
|
|
415
|
-
if (!this.publishChannelSetupPromise
|
|
415
|
+
if (!this.publishChannelSetupPromise) {
|
|
416
416
|
this.publishChannelSetupPromise = new Promise<ChannelWrapper>(async (resolve, reject) => {
|
|
417
417
|
if (this.publishChannel && !force) {
|
|
418
418
|
return resolve(this.publishChannel);
|
|
@@ -478,8 +478,9 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
478
478
|
return channel.bindQueue(queue, exchange, '');
|
|
479
479
|
}
|
|
480
480
|
|
|
481
|
-
async setupQueue(queueName: string,
|
|
481
|
+
async setupQueue(queueName: string, options?: Options.AssertQueue): Promise<Replies.AssertQueue> {
|
|
482
482
|
let queue: Replies.AssertQueue;
|
|
483
|
+
const connectionPurpose = ConnectionPurpose.Publish;
|
|
483
484
|
const shouldUseQuorum = RabbitMq.shouldUseQuorum(queueName);
|
|
484
485
|
const localeOptions = {
|
|
485
486
|
...options,
|
|
@@ -531,8 +532,8 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
531
532
|
return false;
|
|
532
533
|
}
|
|
533
534
|
|
|
534
|
-
async assertQueue(queueName: string,
|
|
535
|
-
debug('rabbit: start assert queue', {
|
|
535
|
+
async assertQueue(queueName: string, options?: Options.AssertQueue) {
|
|
536
|
+
debug('rabbit: start assert queue', { queueName });
|
|
536
537
|
RabbitMq.validateName('queue', queueName);
|
|
537
538
|
if (this.queues[queueName]) {
|
|
538
539
|
delete this.queueSetupPromises[queueName];
|
|
@@ -543,8 +544,8 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
543
544
|
return this.queueSetupPromises[queueName];
|
|
544
545
|
}
|
|
545
546
|
|
|
546
|
-
this.queueSetupPromises[queueName] = this.setupQueue(queueName,
|
|
547
|
-
debug('rabbit: done assert queue', {
|
|
547
|
+
this.queueSetupPromises[queueName] = this.setupQueue(queueName, options);
|
|
548
|
+
debug('rabbit: done assert queue', { queueName });
|
|
548
549
|
return this.queueSetupPromises[queueName];
|
|
549
550
|
}
|
|
550
551
|
|
|
@@ -600,7 +601,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
600
601
|
}
|
|
601
602
|
const channel = await this.getNewChannel({ connectionPurpose: ConnectionPurpose.Consume });
|
|
602
603
|
return channel.addSetup(async (confirmChannel: ConfirmChannel) => {
|
|
603
|
-
const q = await this.assertQueue(queue,
|
|
604
|
+
const q = await this.assertQueue(queue, optionsWithDefaults);
|
|
604
605
|
await confirmChannel.prefetch(limit, false);
|
|
605
606
|
const { consumerTag } = await confirmChannel.consume(
|
|
606
607
|
queue,
|
|
@@ -739,7 +740,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
739
740
|
|
|
740
741
|
try {
|
|
741
742
|
RabbitMq.validateName('queue', queue);
|
|
742
|
-
await this.assertQueue(queue,
|
|
743
|
+
await this.assertQueue(queue, options);
|
|
743
744
|
} catch (e) {
|
|
744
745
|
logger.error(`rabbit sendToQueue: failed to assert queue ${queue}`, { e });
|
|
745
746
|
throw e;
|
|
@@ -757,11 +758,31 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
757
758
|
}
|
|
758
759
|
}
|
|
759
760
|
|
|
761
|
+
// const connection = await this.getConnection();
|
|
762
|
+
// const isConnected = connection.isConnected();
|
|
763
|
+
// if (!isConnected) {
|
|
764
|
+
// logger.error('rabbit: isConnected - false');
|
|
765
|
+
// return false;
|
|
766
|
+
// }
|
|
767
|
+
// const channel: any = await this.assertChannel();
|
|
768
|
+
// try {
|
|
769
|
+
// await Promise.all([
|
|
770
|
+
// channel.waitForConnect(),
|
|
771
|
+
// ...this.consumers.map((c: AfConsumer) => channel.checkQueue(c.queue)),
|
|
772
|
+
// ]);
|
|
773
|
+
// } catch (e) {
|
|
774
|
+
// logger.error('rabbit: isConnected - false');
|
|
775
|
+
// return false;
|
|
776
|
+
// }
|
|
777
|
+
// logger.info('rabbit: isConnected - true');
|
|
778
|
+
// return true;
|
|
779
|
+
|
|
760
780
|
async isConnected(): Promise<boolean> {
|
|
761
781
|
debug('rabbit: start is connected');
|
|
762
782
|
const isEachConnectionConnected = await Promise.all(
|
|
763
783
|
Object.entries(this.connectionsMap).map(async ([connectionPurpose, connectionData]) => {
|
|
764
|
-
|
|
784
|
+
const { connection } = connectionData;
|
|
785
|
+
if (connectionPurpose === ConnectionPurpose.Publish && connection) {
|
|
765
786
|
const channel: any = await this.assertChannel({ connectionPurpose: connectionPurpose as ConnectionPurpose });
|
|
766
787
|
try {
|
|
767
788
|
await Promise.all([
|
|
@@ -773,14 +794,15 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
773
794
|
return false;
|
|
774
795
|
}
|
|
775
796
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
797
|
+
|
|
798
|
+
if (connection) {
|
|
799
|
+
const isConnected = connection?.isConnected();
|
|
800
|
+
if (!isConnected) {
|
|
801
|
+
logger.error('rabbit: isConnected - false', { connectionPurpose });
|
|
802
|
+
return false;
|
|
803
|
+
}
|
|
804
|
+
logger.info('rabbit: isConnected - true', { connectionPurpose });
|
|
782
805
|
}
|
|
783
|
-
logger.info('rabbit: isConnected - true');
|
|
784
806
|
return true;
|
|
785
807
|
}),
|
|
786
808
|
);
|