@azteam/rabbitmq-async 1.0.124 → 1.0.135
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 +2 -2
- package/src/RabbitMQAsync.js +40 -50
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/rabbitmq-async",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.135",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"engines": {
|
|
@@ -17,6 +17,6 @@
|
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@azteam/util": "1.0.13",
|
|
20
|
-
"amqplib": "0.
|
|
20
|
+
"amqplib": "0.8.0"
|
|
21
21
|
}
|
|
22
22
|
}
|
package/src/RabbitMQAsync.js
CHANGED
|
@@ -68,7 +68,7 @@ class RabbitMQAsync {
|
|
|
68
68
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
69
69
|
|
|
70
70
|
if (this.connected) {
|
|
71
|
-
let channel
|
|
71
|
+
let channel;
|
|
72
72
|
try {
|
|
73
73
|
channel = await this.client.createChannel();
|
|
74
74
|
const queueInfo = await channel.assertQueue(prefixQueueName, {
|
|
@@ -87,9 +87,7 @@ class RabbitMQAsync {
|
|
|
87
87
|
return this.send(queueName, msg);
|
|
88
88
|
} finally {
|
|
89
89
|
try {
|
|
90
|
-
|
|
91
|
-
channel.close();
|
|
92
|
-
}
|
|
90
|
+
await channel.close();
|
|
93
91
|
} catch (err) {}
|
|
94
92
|
}
|
|
95
93
|
} else {
|
|
@@ -102,21 +100,17 @@ class RabbitMQAsync {
|
|
|
102
100
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
103
101
|
|
|
104
102
|
if (this.connected) {
|
|
105
|
-
let channel
|
|
103
|
+
let channel;
|
|
106
104
|
try {
|
|
107
105
|
channel = await this.client.createChannel();
|
|
108
|
-
|
|
106
|
+
return await channel.assertQueue(prefixQueueName, {
|
|
109
107
|
durable: true,
|
|
110
108
|
});
|
|
111
|
-
|
|
112
|
-
return queueInfo;
|
|
113
109
|
} catch (err) {
|
|
114
110
|
return this.getInfo(queueName);
|
|
115
111
|
} finally {
|
|
116
112
|
try {
|
|
117
|
-
|
|
118
|
-
channel.close();
|
|
119
|
-
}
|
|
113
|
+
await channel.close();
|
|
120
114
|
} catch (err) {}
|
|
121
115
|
}
|
|
122
116
|
} else {
|
|
@@ -128,11 +122,11 @@ class RabbitMQAsync {
|
|
|
128
122
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
129
123
|
|
|
130
124
|
if (this.connected) {
|
|
131
|
-
let channel
|
|
125
|
+
let channel;
|
|
132
126
|
try {
|
|
133
127
|
channel = await this.client.createChannel();
|
|
134
128
|
|
|
135
|
-
|
|
129
|
+
await channel.assertQueue(prefixQueueName, {
|
|
136
130
|
durable: true,
|
|
137
131
|
});
|
|
138
132
|
|
|
@@ -142,62 +136,58 @@ class RabbitMQAsync {
|
|
|
142
136
|
|
|
143
137
|
await new Promise((resolve, reject) => {
|
|
144
138
|
channel.consume(prefixQueueName, async function (msg) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
139
|
+
if (msg !== null) {
|
|
140
|
+
const data = JSON.parse(msg.content.toString());
|
|
141
|
+
try {
|
|
142
|
+
if (msg.fields.redelivered) {
|
|
143
|
+
if (!data.retry) {
|
|
144
|
+
data.retry = 0;
|
|
145
|
+
}
|
|
146
|
+
data.retry += 1;
|
|
153
147
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
await channel.ack(msg);
|
|
164
|
-
} else {
|
|
165
|
-
try {
|
|
166
|
-
await cb(data);
|
|
167
|
-
await channel.ack(msg);
|
|
168
|
-
} catch (err) {
|
|
169
|
-
if (callbackError) {
|
|
170
|
-
callbackError(prefixQueueName, err);
|
|
148
|
+
if (data.retry < 5) {
|
|
149
|
+
await messageQueue.send(queueName, data);
|
|
150
|
+
} else {
|
|
151
|
+
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
152
|
+
queueName,
|
|
153
|
+
data,
|
|
154
|
+
retry_time: new Date(),
|
|
155
|
+
});
|
|
171
156
|
}
|
|
172
|
-
|
|
157
|
+
await channel.ack(msg);
|
|
158
|
+
} else {
|
|
159
|
+
try {
|
|
160
|
+
await cb(data);
|
|
173
161
|
await channel.ack(msg);
|
|
174
|
-
}
|
|
175
|
-
|
|
162
|
+
} catch (err) {
|
|
163
|
+
if (callbackError) {
|
|
164
|
+
callbackError(prefixQueueName, err);
|
|
165
|
+
}
|
|
166
|
+
if (data.noRetry) {
|
|
167
|
+
await channel.ack(msg);
|
|
168
|
+
} else {
|
|
169
|
+
await channel.nack(msg);
|
|
170
|
+
}
|
|
176
171
|
}
|
|
177
172
|
}
|
|
173
|
+
} catch (err) {
|
|
174
|
+
reject(err);
|
|
178
175
|
}
|
|
179
|
-
} catch (err) {
|
|
180
|
-
reject(err);
|
|
181
176
|
}
|
|
182
177
|
});
|
|
183
178
|
});
|
|
184
179
|
} catch (err) {
|
|
185
180
|
try {
|
|
186
|
-
|
|
187
|
-
channel.close();
|
|
188
|
-
}
|
|
181
|
+
await channel.close();
|
|
189
182
|
} catch (err2) {}
|
|
183
|
+
|
|
190
184
|
await timeout(5000);
|
|
191
|
-
if (callbackError) {
|
|
192
|
-
callbackError(prefixQueueName, err);
|
|
193
|
-
}
|
|
194
185
|
return this.receiving(queueName, cb, callbackError);
|
|
195
186
|
}
|
|
196
187
|
} else {
|
|
197
188
|
await timeout(5000);
|
|
198
189
|
return this.receiving(queueName, cb, callbackError);
|
|
199
190
|
}
|
|
200
|
-
return false;
|
|
201
191
|
}
|
|
202
192
|
|
|
203
193
|
setAlertCallback(callback) {
|