@autofleet/rabbit 3.2.16-beta.2 → 3.2.16-beta.3
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 +3 -2
- package/dist/index.js +34 -31
- package/dist/lib/types.d.ts +4 -1
- package/package.json +1 -1
- package/src/index.ts +37 -33
- package/src/lib/types.ts +5 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
|
|
|
3
3
|
import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-connection-manager';
|
|
4
4
|
import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
|
|
5
5
|
import { RedisConfig } from './lib/redis';
|
|
6
|
-
import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache } from './lib/types';
|
|
6
|
+
import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary } from './lib/types';
|
|
7
7
|
export interface IAfRabbitMq {
|
|
8
8
|
ack: any;
|
|
9
9
|
nack: any;
|
|
@@ -64,7 +64,7 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
64
64
|
creatingConnection: boolean;
|
|
65
65
|
exchanges: ExchangesCache;
|
|
66
66
|
queues: QueuesCache;
|
|
67
|
-
|
|
67
|
+
queueSetupPromises: QueueSetupPromisesDictionary;
|
|
68
68
|
options: AfRabbitOptions | undefined;
|
|
69
69
|
redisClient: any;
|
|
70
70
|
redisLock?: RedisLockType;
|
|
@@ -82,6 +82,7 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
82
82
|
getQueueLength(queue: string): Promise<Replies.AssertQueue>;
|
|
83
83
|
private deleteQueue;
|
|
84
84
|
bindQueue(queue: string, exchange: string): Promise<void>;
|
|
85
|
+
setupQueue(queueName: string, options?: Options.AssertQueue): Promise<Replies.AssertQueue>;
|
|
85
86
|
assertQueue(queueName: string, options?: Options.AssertQueue): Promise<any>;
|
|
86
87
|
private saveConsumer;
|
|
87
88
|
consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ const utils_1 = require("./lib/utils");
|
|
|
18
18
|
const consts_1 = require("./lib/consts");
|
|
19
19
|
const types_1 = require("./lib/types");
|
|
20
20
|
// const debug = nodeDebug('af-rabbitmq')
|
|
21
|
-
const debug = logger_1.default.
|
|
21
|
+
const debug = logger_1.default.debug.bind(logger_1.default);
|
|
22
22
|
const PUBLISH_TIMEOUT = 1000 * 10;
|
|
23
23
|
const HEARTBEAT = '60';
|
|
24
24
|
class RabbitMq {
|
|
@@ -121,7 +121,7 @@ class RabbitMq {
|
|
|
121
121
|
this.creatingConnection = false;
|
|
122
122
|
this.exchanges = {};
|
|
123
123
|
this.queues = {};
|
|
124
|
-
this.
|
|
124
|
+
this.queueSetupPromises = {};
|
|
125
125
|
this.consumers = [];
|
|
126
126
|
this.options = options;
|
|
127
127
|
this.redisClient = redisConfig && (0, redis_1.default)(redisConfig);
|
|
@@ -289,41 +289,44 @@ class RabbitMq {
|
|
|
289
289
|
await channel.addSetup((setupChannel) => setupChannel.bindQueue(queue, exchange, ''));
|
|
290
290
|
return channel.bindQueue(queue, exchange, '');
|
|
291
291
|
}
|
|
292
|
+
async setupQueue(queueName, options) {
|
|
293
|
+
let queue;
|
|
294
|
+
try {
|
|
295
|
+
const channel = await this.assertChannel();
|
|
296
|
+
debug('assertQueue->channel.addSetup', { queueName });
|
|
297
|
+
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
|
|
298
|
+
debug('assertQueue->channel.assertQueue', { queueName });
|
|
299
|
+
queue = await channel.assertQueue(queueName, options);
|
|
300
|
+
}
|
|
301
|
+
catch (e) {
|
|
302
|
+
logger_1.default.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
303
|
+
if (!this.options?.dontRetryAssert) {
|
|
304
|
+
debug('retrying assertQueue', { queueName });
|
|
305
|
+
const channel = await this.assertChannel({ force: true });
|
|
306
|
+
await this.deleteQueue(queueName);
|
|
307
|
+
debug('retrying assertQueue->channel.addSetup', { queueName });
|
|
308
|
+
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
|
|
309
|
+
debug('retrying assertQueue->channel.assertQueue', { queueName });
|
|
310
|
+
queue = await channel.assertQueue(queueName, options);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
throw e;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
this.queues[queueName] = queueName;
|
|
317
|
+
return queue;
|
|
318
|
+
}
|
|
292
319
|
async assertQueue(queueName, options) {
|
|
293
320
|
RabbitMq.validateName('queue', queueName);
|
|
294
321
|
if (this.queues[queueName]) {
|
|
322
|
+
delete this.queueSetupPromises[queueName];
|
|
295
323
|
return this.queues[queueName];
|
|
296
324
|
}
|
|
297
|
-
if (this.
|
|
298
|
-
return this.
|
|
325
|
+
if (this.queueSetupPromises[queueName]) {
|
|
326
|
+
return this.queueSetupPromises[queueName];
|
|
299
327
|
}
|
|
300
|
-
this.
|
|
301
|
-
|
|
302
|
-
try {
|
|
303
|
-
const channel = await this.assertChannel();
|
|
304
|
-
debug('assertQueue->channel.addSetup', { queueName });
|
|
305
|
-
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
|
|
306
|
-
debug('assertQueue->channel.assertQueue', { queueName });
|
|
307
|
-
queue = await channel.assertQueue(queueName, options);
|
|
308
|
-
}
|
|
309
|
-
catch (e) {
|
|
310
|
-
logger_1.default.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
311
|
-
if (!this.options?.dontRetryAssert) {
|
|
312
|
-
debug('retrying assertQueue', { queueName });
|
|
313
|
-
const channel = await this.assertChannel({ force: true });
|
|
314
|
-
await this.deleteQueue(queueName);
|
|
315
|
-
debug('1assertQueue->channel.addSetup', { queueName });
|
|
316
|
-
await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
|
|
317
|
-
debug('1assertQueue->channel.assertQueue', { queueName });
|
|
318
|
-
queue = await channel.assertQueue(queueName, options);
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
throw e;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
this.queues[queueName] = queueName;
|
|
325
|
-
return queue;
|
|
326
|
-
})());
|
|
328
|
+
this.queueSetupPromises[queueName] = this.setupQueue(queueName, options);
|
|
329
|
+
return this.queueSetupPromises[queueName];
|
|
327
330
|
}
|
|
328
331
|
saveConsumer(queue, callback, options) {
|
|
329
332
|
const isConsumerExist = this.consumers.some((consumer) => consumer.queue === queue);
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { ConsumeMessage, Options } from 'amqplib';
|
|
1
|
+
import { ConsumeMessage, Options, Replies } from 'amqplib';
|
|
2
2
|
export interface ExchangesCache {
|
|
3
3
|
[key: string]: any;
|
|
4
4
|
}
|
|
5
5
|
export interface QueuesCache {
|
|
6
6
|
[key: string]: any;
|
|
7
7
|
}
|
|
8
|
+
export interface QueueSetupPromisesDictionary {
|
|
9
|
+
[key: string]: Promise<Replies.AssertQueue> | undefined;
|
|
10
|
+
}
|
|
8
11
|
export type CustomMessageHeaders = {
|
|
9
12
|
redisTimestampValidationKey?: string;
|
|
10
13
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -35,11 +35,11 @@ import {
|
|
|
35
35
|
CustomMessageHeaders,
|
|
36
36
|
QueuesCache,
|
|
37
37
|
RedisLockType,
|
|
38
|
-
ExchangesCache, CONSUMER_DEFAULT_OPTIONS,
|
|
38
|
+
ExchangesCache, CONSUMER_DEFAULT_OPTIONS, QueueSetupPromisesDictionary,
|
|
39
39
|
} from './lib/types';
|
|
40
40
|
|
|
41
41
|
// const debug = nodeDebug('af-rabbitmq')
|
|
42
|
-
const debug = logger.
|
|
42
|
+
const debug = logger.debug.bind(logger);
|
|
43
43
|
|
|
44
44
|
const PUBLISH_TIMEOUT = 1000 * 10;
|
|
45
45
|
|
|
@@ -153,7 +153,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
153
153
|
|
|
154
154
|
queues: QueuesCache;
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
queueSetupPromises: QueueSetupPromisesDictionary;
|
|
157
157
|
|
|
158
158
|
options: AfRabbitOptions | undefined;
|
|
159
159
|
|
|
@@ -173,7 +173,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
173
173
|
this.creatingConnection = false;
|
|
174
174
|
this.exchanges = {};
|
|
175
175
|
this.queues = {};
|
|
176
|
-
this.
|
|
176
|
+
this.queueSetupPromises = {};
|
|
177
177
|
this.consumers = [];
|
|
178
178
|
this.options = options;
|
|
179
179
|
this.redisClient = redisConfig && getRedisInstance(redisConfig);
|
|
@@ -428,43 +428,47 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
428
428
|
return channel.bindQueue(queue, exchange, '');
|
|
429
429
|
}
|
|
430
430
|
|
|
431
|
+
async setupQueue(queueName: string, options?: Options.AssertQueue): Promise<Replies.AssertQueue> {
|
|
432
|
+
let queue: Replies.AssertQueue;
|
|
433
|
+
try {
|
|
434
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
435
|
+
debug('assertQueue->channel.addSetup', { queueName });
|
|
436
|
+
await channel.addSetup((setupChannel: ConfirmChannel) => setupChannel.assertQueue(queueName, options));
|
|
437
|
+
debug('assertQueue->channel.assertQueue', { queueName });
|
|
438
|
+
queue = await channel.assertQueue(queueName, options);
|
|
439
|
+
} catch (e) {
|
|
440
|
+
logger.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
441
|
+
if (!this.options?.dontRetryAssert) {
|
|
442
|
+
debug('retrying assertQueue', { queueName });
|
|
443
|
+
const channel = await this.assertChannel({ force: true });
|
|
444
|
+
await this.deleteQueue(queueName);
|
|
445
|
+
|
|
446
|
+
debug('retrying assertQueue->channel.addSetup', { queueName });
|
|
447
|
+
await channel.addSetup((setupChannel: ConfirmChannel) => setupChannel.assertQueue(queueName, options));
|
|
448
|
+
debug('retrying assertQueue->channel.assertQueue', { queueName });
|
|
449
|
+
queue = await channel.assertQueue(queueName, options);
|
|
450
|
+
} else {
|
|
451
|
+
throw e;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
this.queues[queueName] = queueName;
|
|
456
|
+
return queue;
|
|
457
|
+
}
|
|
458
|
+
|
|
431
459
|
async assertQueue(queueName: string, options?: Options.AssertQueue) {
|
|
432
460
|
RabbitMq.validateName('queue', queueName);
|
|
433
461
|
if (this.queues[queueName]) {
|
|
462
|
+
delete this.queueSetupPromises[queueName];
|
|
434
463
|
return this.queues[queueName];
|
|
435
464
|
}
|
|
436
465
|
|
|
437
|
-
if (this.
|
|
438
|
-
return this.
|
|
466
|
+
if (this.queueSetupPromises[queueName]) {
|
|
467
|
+
return this.queueSetupPromises[queueName];
|
|
439
468
|
}
|
|
440
469
|
|
|
441
|
-
this.
|
|
442
|
-
|
|
443
|
-
try {
|
|
444
|
-
const channel: ChannelWrapper = await this.assertChannel();
|
|
445
|
-
debug('assertQueue->channel.addSetup', { queueName });
|
|
446
|
-
await channel.addSetup((setupChannel: ConfirmChannel) => setupChannel.assertQueue(queueName, options));
|
|
447
|
-
debug('assertQueue->channel.assertQueue', { queueName });
|
|
448
|
-
queue = await channel.assertQueue(queueName, options);
|
|
449
|
-
} catch (e) {
|
|
450
|
-
logger.error('rabbit: assertQueue error', { queueName, options, error: e });
|
|
451
|
-
if (!this.options?.dontRetryAssert) {
|
|
452
|
-
debug('retrying assertQueue', { queueName });
|
|
453
|
-
const channel = await this.assertChannel({ force: true });
|
|
454
|
-
await this.deleteQueue(queueName);
|
|
455
|
-
|
|
456
|
-
debug('1assertQueue->channel.addSetup', { queueName });
|
|
457
|
-
await channel.addSetup((setupChannel: ConfirmChannel) => setupChannel.assertQueue(queueName, options));
|
|
458
|
-
debug('1assertQueue->channel.assertQueue', { queueName });
|
|
459
|
-
queue = await channel.assertQueue(queueName, options);
|
|
460
|
-
} else {
|
|
461
|
-
throw e;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
this.queues[queueName] = queueName;
|
|
466
|
-
return queue;
|
|
467
|
-
})());
|
|
470
|
+
this.queueSetupPromises[queueName] = this.setupQueue(queueName, options);
|
|
471
|
+
return this.queueSetupPromises[queueName];
|
|
468
472
|
}
|
|
469
473
|
|
|
470
474
|
private saveConsumer(queue: string, callback: CallbackFunction, options: ConsumeOptions | undefined) {
|
package/src/lib/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConsumeMessage, Options } from 'amqplib';
|
|
1
|
+
import { ConsumeMessage, Options, Replies } from 'amqplib';
|
|
2
2
|
|
|
3
3
|
export interface ExchangesCache {
|
|
4
4
|
[key: string]: any
|
|
@@ -8,6 +8,10 @@ export interface QueuesCache {
|
|
|
8
8
|
[key: string]: any
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export interface QueueSetupPromisesDictionary {
|
|
12
|
+
[key: string]: Promise<Replies.AssertQueue> | undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
export type CustomMessageHeaders = {
|
|
12
16
|
redisTimestampValidationKey?: string;
|
|
13
17
|
}
|