@autofleet/rabbit 1.0.11 → 1.0.13
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 +15 -3
- package/package.json +1 -1
- package/src/index.ts +15 -4
package/dist/index.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ declare class RabbitMq {
|
|
|
12
12
|
assertChannel(): Promise<Channel>;
|
|
13
13
|
assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
|
|
14
14
|
assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
|
|
15
|
-
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any): Promise<import("amqplib").Replies.Consume>;
|
|
16
|
-
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any): Promise<import("amqplib").Replies.Consume>;
|
|
15
|
+
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
|
|
16
|
+
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
|
|
17
17
|
publish(exchange: string, content: any): Promise<boolean>;
|
|
18
18
|
sendToQueue(queue: string, content: any): Promise<boolean>;
|
|
19
19
|
}
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,6 @@ class RabbitMq {
|
|
|
12
12
|
};
|
|
13
13
|
this.nack = (queue) => async (msg, retries) => {
|
|
14
14
|
if (this.channel) {
|
|
15
|
-
await this.channel.ack(msg);
|
|
16
15
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
17
16
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
18
17
|
await this.sendToQueue(queue, msg.content);
|
|
@@ -22,6 +21,7 @@ class RabbitMq {
|
|
|
22
21
|
await this.assertQueue(deadQueue);
|
|
23
22
|
await this.sendToQueue(deadQueue, msg.content);
|
|
24
23
|
}
|
|
24
|
+
await this.channel.ack(msg);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
this.creatingChannel = false;
|
|
@@ -58,6 +58,14 @@ class RabbitMq {
|
|
|
58
58
|
connection = await amqplib_1.connect(`amqp://${host}`);
|
|
59
59
|
channel = await connection.createChannel();
|
|
60
60
|
this.channel = channel;
|
|
61
|
+
channel.on('close', async () => {
|
|
62
|
+
this.channel = null;
|
|
63
|
+
await this.assertChannel();
|
|
64
|
+
});
|
|
65
|
+
connection.on('close', async () => {
|
|
66
|
+
this.channel = null;
|
|
67
|
+
await this.assertChannel();
|
|
68
|
+
});
|
|
61
69
|
this.creatingChannel = false;
|
|
62
70
|
this.em.emit(channelCreatedConst, channel);
|
|
63
71
|
resolve(channel);
|
|
@@ -75,17 +83,21 @@ class RabbitMq {
|
|
|
75
83
|
const channel = await this.assertChannel();
|
|
76
84
|
return channel.assertQueue(queue);
|
|
77
85
|
}
|
|
78
|
-
async consume(queue, callback) {
|
|
86
|
+
async consume(queue, callback, options) {
|
|
87
|
+
const { limit } = options ? options : 1;
|
|
79
88
|
const channel = await this.assertChannel();
|
|
80
89
|
await this.assertQueue(queue);
|
|
90
|
+
channel.prefetch(limit, false);
|
|
81
91
|
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
82
92
|
}
|
|
83
|
-
async consumeFromExchange(queue, exchange, callback) {
|
|
93
|
+
async consumeFromExchange(queue, exchange, callback, options) {
|
|
94
|
+
const { limit } = options ? options : 1;
|
|
84
95
|
const channel = await this.assertChannel();
|
|
85
96
|
await Promise.all([
|
|
86
97
|
this.assertExchange(exchange),
|
|
87
98
|
this.assertQueue(queue),
|
|
88
99
|
]);
|
|
100
|
+
channel.prefetch(limit, false);
|
|
89
101
|
await channel.bindQueue(queue, exchange, '');
|
|
90
102
|
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
91
103
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { connect, Connection, Channel } from 'amqplib';
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import { reject } from 'bluebird';
|
|
4
3
|
|
|
5
4
|
const channelCreatedConst: string = 'channelCreated';
|
|
6
5
|
class RabbitMq {
|
|
@@ -43,7 +42,6 @@ class RabbitMq {
|
|
|
43
42
|
|
|
44
43
|
public nack = (queue: string) => async (msg: any, retries: number) => {
|
|
45
44
|
if (this.channel) {
|
|
46
|
-
await this.channel.ack(msg);
|
|
47
45
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
48
46
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
49
47
|
await this.sendToQueue(queue, msg.content);
|
|
@@ -52,6 +50,7 @@ class RabbitMq {
|
|
|
52
50
|
await this.assertQueue(deadQueue);
|
|
53
51
|
await this.sendToQueue(deadQueue, msg.content);
|
|
54
52
|
}
|
|
53
|
+
await this.channel.ack(msg);
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
|
|
@@ -71,6 +70,14 @@ class RabbitMq {
|
|
|
71
70
|
connection = await connect(`amqp://${host}`);
|
|
72
71
|
channel = await connection.createChannel();
|
|
73
72
|
this.channel = channel;
|
|
73
|
+
channel.on('close', async () => {
|
|
74
|
+
this.channel = null;
|
|
75
|
+
await this.assertChannel();
|
|
76
|
+
});
|
|
77
|
+
connection.on('close', async () => {
|
|
78
|
+
this.channel = null;
|
|
79
|
+
await this.assertChannel();
|
|
80
|
+
});
|
|
74
81
|
this.creatingChannel = false;
|
|
75
82
|
this.em.emit(channelCreatedConst, channel);
|
|
76
83
|
resolve(channel);
|
|
@@ -90,18 +97,22 @@ class RabbitMq {
|
|
|
90
97
|
return channel.assertQueue(queue);
|
|
91
98
|
}
|
|
92
99
|
|
|
93
|
-
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any) {
|
|
100
|
+
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
101
|
+
const { limit } = options ? options : 1;
|
|
94
102
|
const channel: Channel = await this.assertChannel();
|
|
95
103
|
await this.assertQueue(queue);
|
|
104
|
+
channel.prefetch(limit, false);
|
|
96
105
|
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
97
106
|
}
|
|
98
107
|
|
|
99
|
-
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any) {
|
|
108
|
+
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
109
|
+
const { limit } = options ? options : 1;
|
|
100
110
|
const channel: Channel = await this.assertChannel();
|
|
101
111
|
await Promise.all([
|
|
102
112
|
this.assertExchange(exchange),
|
|
103
113
|
this.assertQueue(queue),
|
|
104
114
|
]);
|
|
115
|
+
channel.prefetch(limit, false);
|
|
105
116
|
await channel.bindQueue(queue, exchange, '');
|
|
106
117
|
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
107
118
|
}
|