@azteam/rabbitmq-async 1.0.134 → 1.0.137
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/RabbitMQAsync.js +40 -46
package/package.json
CHANGED
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,7 +100,7 @@ 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, {
|
|
@@ -112,9 +110,7 @@ class RabbitMQAsync {
|
|
|
112
110
|
return this.getInfo(queueName);
|
|
113
111
|
} finally {
|
|
114
112
|
try {
|
|
115
|
-
|
|
116
|
-
channel.close();
|
|
117
|
-
}
|
|
113
|
+
await channel.close();
|
|
118
114
|
} catch (err) {}
|
|
119
115
|
}
|
|
120
116
|
} else {
|
|
@@ -126,7 +122,7 @@ class RabbitMQAsync {
|
|
|
126
122
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
127
123
|
|
|
128
124
|
if (this.connected) {
|
|
129
|
-
let channel
|
|
125
|
+
let channel;
|
|
130
126
|
try {
|
|
131
127
|
channel = await this.client.createChannel();
|
|
132
128
|
|
|
@@ -140,62 +136,60 @@ class RabbitMQAsync {
|
|
|
140
136
|
|
|
141
137
|
await new Promise((resolve, reject) => {
|
|
142
138
|
channel.consume(prefixQueueName, async function (msg) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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;
|
|
151
147
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
await channel.ack(msg);
|
|
162
|
-
} else {
|
|
163
|
-
try {
|
|
164
|
-
await cb(data);
|
|
165
|
-
await channel.ack(msg);
|
|
166
|
-
} catch (err) {
|
|
167
|
-
if (callbackError) {
|
|
168
|
-
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
|
+
});
|
|
169
156
|
}
|
|
170
|
-
|
|
157
|
+
await channel.ack(msg);
|
|
158
|
+
} else {
|
|
159
|
+
try {
|
|
160
|
+
await cb(data);
|
|
171
161
|
await channel.ack(msg);
|
|
172
|
-
}
|
|
173
|
-
|
|
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
|
+
}
|
|
174
171
|
}
|
|
175
172
|
}
|
|
173
|
+
} catch (err) {
|
|
174
|
+
reject(err);
|
|
176
175
|
}
|
|
177
|
-
}
|
|
178
|
-
reject(
|
|
176
|
+
} else {
|
|
177
|
+
reject(new Error('msg null'));
|
|
179
178
|
}
|
|
180
179
|
});
|
|
181
180
|
});
|
|
182
181
|
} catch (err) {
|
|
183
182
|
try {
|
|
184
|
-
|
|
185
|
-
channel.close();
|
|
186
|
-
}
|
|
183
|
+
await channel.close();
|
|
187
184
|
} catch (err2) {}
|
|
185
|
+
|
|
188
186
|
await timeout(5000);
|
|
189
|
-
if (callbackError) {
|
|
190
|
-
callbackError(prefixQueueName, err);
|
|
191
|
-
}
|
|
192
187
|
return this.receiving(queueName, cb, callbackError);
|
|
193
188
|
}
|
|
194
189
|
} else {
|
|
195
190
|
await timeout(5000);
|
|
196
191
|
return this.receiving(queueName, cb, callbackError);
|
|
197
192
|
}
|
|
198
|
-
return false;
|
|
199
193
|
}
|
|
200
194
|
|
|
201
195
|
setAlertCallback(callback) {
|