@azteam/rabbitmq-async 1.0.107 → 1.0.108
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/package.json +1 -1
- package/src/Provider.js +2 -2
- package/src/RabbitMQAsync.js +14 -19
- package/src/index.js +1 -4
package/package.json
CHANGED
package/src/Provider.js
CHANGED
|
@@ -6,17 +6,17 @@ class Provider {
|
|
|
6
6
|
this.configs = configs;
|
|
7
7
|
if (isSingle) {
|
|
8
8
|
this.configs = {
|
|
9
|
-
single: configs
|
|
9
|
+
single: configs,
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
+
|
|
13
14
|
getConnection(name = 'single') {
|
|
14
15
|
if (!this.connections[name]) {
|
|
15
16
|
this.connections[name] = new RabbitMQAsync(this.configs[name]);
|
|
16
17
|
}
|
|
17
18
|
return this.connections[name];
|
|
18
19
|
}
|
|
19
|
-
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export default Provider;
|
package/src/RabbitMQAsync.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import amqp from 'amqplib';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {timeout} from '@azteam/util';
|
|
4
3
|
|
|
5
4
|
class RabbitMQAsync {
|
|
6
5
|
constructor(config) {
|
|
@@ -27,7 +26,7 @@ class RabbitMQAsync {
|
|
|
27
26
|
|
|
28
27
|
async connect() {
|
|
29
28
|
this._alert('connecting', 'MQ connecting...');
|
|
30
|
-
const opt = {
|
|
29
|
+
const opt = {credentials: amqp.credentials.plain(this.username, this.password)};
|
|
31
30
|
try {
|
|
32
31
|
this.client = await amqp.connect(`amqp://${this.host}`, opt);
|
|
33
32
|
this.connected = true;
|
|
@@ -62,7 +61,6 @@ class RabbitMQAsync {
|
|
|
62
61
|
this.client.close();
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
|
|
66
64
|
parsePrefix(name) {
|
|
67
65
|
if (this.prefix) {
|
|
68
66
|
return `${this.prefix}:${name}`;
|
|
@@ -70,10 +68,7 @@ class RabbitMQAsync {
|
|
|
70
68
|
return name;
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
|
|
74
71
|
async send(queueName, msg = {}, force = true) {
|
|
75
|
-
|
|
76
|
-
|
|
77
72
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
78
73
|
|
|
79
74
|
if (this.connected) {
|
|
@@ -86,17 +81,18 @@ class RabbitMQAsync {
|
|
|
86
81
|
|
|
87
82
|
if (force || queueInfo.messageCount === 0) {
|
|
88
83
|
await channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(msg)), {
|
|
89
|
-
persistent: true
|
|
84
|
+
persistent: true,
|
|
90
85
|
});
|
|
91
86
|
}
|
|
92
87
|
|
|
93
88
|
return true;
|
|
94
|
-
|
|
95
89
|
} catch (err) {
|
|
96
90
|
await timeout(5000);
|
|
97
91
|
return this.send(queueName, msg);
|
|
98
92
|
} finally {
|
|
99
|
-
try {
|
|
93
|
+
try {
|
|
94
|
+
channel && channel.close();
|
|
95
|
+
} catch (err) {}
|
|
100
96
|
}
|
|
101
97
|
} else {
|
|
102
98
|
await timeout(5000);
|
|
@@ -106,7 +102,6 @@ class RabbitMQAsync {
|
|
|
106
102
|
}
|
|
107
103
|
|
|
108
104
|
async receiving(queueName, cb, callbackError = null) {
|
|
109
|
-
|
|
110
105
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
111
106
|
|
|
112
107
|
if (this.connected) {
|
|
@@ -115,16 +110,15 @@ class RabbitMQAsync {
|
|
|
115
110
|
channel = await this.client.createChannel();
|
|
116
111
|
|
|
117
112
|
await channel.assertQueue(prefixQueueName, {
|
|
118
|
-
durable: true
|
|
113
|
+
durable: true,
|
|
119
114
|
});
|
|
120
115
|
await channel.prefetch(1);
|
|
121
116
|
|
|
122
117
|
const messageQueue = this;
|
|
123
|
-
channel.consume(prefixQueueName, async function(msg) {
|
|
118
|
+
channel.consume(prefixQueueName, async function (msg) {
|
|
124
119
|
try {
|
|
125
120
|
const data = JSON.parse(msg.content.toString());
|
|
126
121
|
if (msg.fields.redelivered) {
|
|
127
|
-
|
|
128
122
|
if (!data.retry) {
|
|
129
123
|
data.retry = 0;
|
|
130
124
|
}
|
|
@@ -135,10 +129,9 @@ class RabbitMQAsync {
|
|
|
135
129
|
} else {
|
|
136
130
|
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
137
131
|
queueName,
|
|
138
|
-
data
|
|
132
|
+
data,
|
|
139
133
|
});
|
|
140
134
|
}
|
|
141
|
-
|
|
142
135
|
} else {
|
|
143
136
|
await cb(data);
|
|
144
137
|
}
|
|
@@ -147,9 +140,11 @@ class RabbitMQAsync {
|
|
|
147
140
|
await channel.nack(msg);
|
|
148
141
|
callbackError && callbackError(prefixQueueName, err);
|
|
149
142
|
}
|
|
150
|
-
})
|
|
143
|
+
});
|
|
151
144
|
} catch (err) {
|
|
152
|
-
try {
|
|
145
|
+
try {
|
|
146
|
+
channel && channel.close();
|
|
147
|
+
} catch (err) {}
|
|
153
148
|
await timeout(5000);
|
|
154
149
|
callbackError && callbackError(prefixQueueName, err);
|
|
155
150
|
return this.receiving(queueName, cb, callbackError);
|
|
@@ -173,4 +168,4 @@ class RabbitMQAsync {
|
|
|
173
168
|
}
|
|
174
169
|
}
|
|
175
170
|
|
|
176
|
-
export default RabbitMQAsync;
|
|
171
|
+
export default RabbitMQAsync;
|