@autofleet/rabbit 1.0.8 → 1.0.10
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 +1 -1
- package/dist/index.js +5 -3
- package/dist/mock.d.ts +1 -0
- package/dist/mock.js +1 -0
- package/package.json +1 -1
- package/src/index.ts +5 -3
- package/src/mock.ts +1 -0
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare class RabbitMq {
|
|
|
8
8
|
static parseMsg(msg: any): any;
|
|
9
9
|
constructor();
|
|
10
10
|
ack: (msg: any) => Promise<void>;
|
|
11
|
-
nack: (
|
|
11
|
+
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
|
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>;
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ class RabbitMq {
|
|
|
10
10
|
await this.channel.ack(msg);
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
|
-
this.nack = async (msg,
|
|
13
|
+
this.nack = (queue) => async (msg, retries) => {
|
|
14
14
|
if (this.channel) {
|
|
15
15
|
await this.channel.ack(msg);
|
|
16
16
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
@@ -18,6 +18,8 @@ class RabbitMq {
|
|
|
18
18
|
await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
+
const deadQueue = `${queue}-dead`;
|
|
22
|
+
await this.assertQueue(deadQueue);
|
|
21
23
|
await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -76,7 +78,7 @@ class RabbitMq {
|
|
|
76
78
|
async consume(queue, callback) {
|
|
77
79
|
const channel = await this.assertChannel();
|
|
78
80
|
await this.assertQueue(queue);
|
|
79
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
|
|
81
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
80
82
|
}
|
|
81
83
|
async consumeFromExchange(queue, exchange, callback) {
|
|
82
84
|
const channel = await this.assertChannel();
|
|
@@ -85,7 +87,7 @@ class RabbitMq {
|
|
|
85
87
|
this.assertQueue(queue),
|
|
86
88
|
]);
|
|
87
89
|
await channel.bindQueue(queue, exchange, '');
|
|
88
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
|
|
90
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
89
91
|
}
|
|
90
92
|
async publish(exchange, content) {
|
|
91
93
|
const channel = await this.assertChannel();
|
package/dist/mock.d.ts
CHANGED
package/dist/mock.js
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -41,13 +41,15 @@ class RabbitMq {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
public nack =
|
|
44
|
+
public nack = (queue: string) => async (msg: any, retries: number) => {
|
|
45
45
|
if (this.channel) {
|
|
46
46
|
await this.channel.ack(msg);
|
|
47
47
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
48
48
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
49
49
|
await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
|
|
50
50
|
} else {
|
|
51
|
+
const deadQueue = `${queue}-dead`;
|
|
52
|
+
await this.assertQueue(deadQueue);
|
|
51
53
|
await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
|
|
52
54
|
}
|
|
53
55
|
}
|
|
@@ -91,7 +93,7 @@ class RabbitMq {
|
|
|
91
93
|
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any) {
|
|
92
94
|
const channel: Channel = await this.assertChannel();
|
|
93
95
|
await this.assertQueue(queue);
|
|
94
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
|
|
96
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any) {
|
|
@@ -101,7 +103,7 @@ class RabbitMq {
|
|
|
101
103
|
this.assertQueue(queue),
|
|
102
104
|
]);
|
|
103
105
|
await channel.bindQueue(queue, exchange, '');
|
|
104
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
|
|
106
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
async publish(exchange: string, content: any) {
|