@azteam/rabbitmq-async 1.0.118 → 1.0.121
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 +48 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/rabbitmq-async",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.121",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"engines": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"author": "toda <sp.azsolution.net@gmail.com>",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@azteam/util": "1.0.
|
|
19
|
+
"@azteam/util": "1.0.13",
|
|
20
20
|
"amqplib": "0.8.0"
|
|
21
21
|
}
|
|
22
22
|
}
|
package/src/RabbitMQAsync.js
CHANGED
|
@@ -87,14 +87,15 @@ class RabbitMQAsync {
|
|
|
87
87
|
return this.send(queueName, msg);
|
|
88
88
|
} finally {
|
|
89
89
|
try {
|
|
90
|
-
|
|
90
|
+
if (channel) {
|
|
91
|
+
channel.close();
|
|
92
|
+
}
|
|
91
93
|
} catch (err) {}
|
|
92
94
|
}
|
|
93
95
|
} else {
|
|
94
96
|
await timeout(5000);
|
|
95
97
|
return this.send(queueName, msg);
|
|
96
98
|
}
|
|
97
|
-
return false;
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
async receiving(queueName, cb, callbackError = null) {
|
|
@@ -111,46 +112,65 @@ class RabbitMQAsync {
|
|
|
111
112
|
await channel.prefetch(1);
|
|
112
113
|
|
|
113
114
|
const messageQueue = this;
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
|
|
116
|
+
await new Promise((resolve, reject) => {
|
|
117
|
+
channel.consume(prefixQueueName, async function (msg) {
|
|
116
118
|
const data = JSON.parse(msg.content.toString());
|
|
117
|
-
if (msg.fields.redelivered) {
|
|
118
|
-
if (!data.retry) {
|
|
119
|
-
data.retry = 0;
|
|
120
|
-
}
|
|
121
|
-
data.retry += 1;
|
|
122
119
|
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
try {
|
|
121
|
+
if (msg.fields.redelivered) {
|
|
122
|
+
if (!data.retry) {
|
|
123
|
+
data.retry = 0;
|
|
124
|
+
}
|
|
125
|
+
data.retry += 1;
|
|
126
|
+
|
|
127
|
+
if (data.retry < 5) {
|
|
128
|
+
await messageQueue.send(queueName, data);
|
|
129
|
+
} else {
|
|
130
|
+
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
131
|
+
queueName,
|
|
132
|
+
data,
|
|
133
|
+
retry_time: new Date(),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
await channel.ack(msg);
|
|
125
137
|
} else {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
138
|
+
try {
|
|
139
|
+
await cb(data);
|
|
140
|
+
await channel.ack(msg);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
if (callbackError) {
|
|
143
|
+
callbackError(prefixQueueName, err);
|
|
144
|
+
}
|
|
145
|
+
if (data.noRetry) {
|
|
146
|
+
await channel.ack(msg);
|
|
147
|
+
} else {
|
|
148
|
+
await channel.nack(msg);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
131
151
|
}
|
|
132
|
-
}
|
|
133
|
-
|
|
152
|
+
} catch (err) {
|
|
153
|
+
reject(err);
|
|
134
154
|
}
|
|
135
|
-
|
|
136
|
-
} catch (err) {
|
|
137
|
-
await channel.nack(msg);
|
|
138
|
-
callbackError && callbackError(prefixQueueName, err);
|
|
139
|
-
}
|
|
155
|
+
});
|
|
140
156
|
});
|
|
141
157
|
} catch (err) {
|
|
142
158
|
try {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
159
|
+
if (channel) {
|
|
160
|
+
channel.close();
|
|
161
|
+
}
|
|
162
|
+
} catch (err2) {}
|
|
146
163
|
await timeout(5000);
|
|
147
|
-
|
|
164
|
+
if (callbackError) {
|
|
165
|
+
callbackError(prefixQueueName, err);
|
|
166
|
+
}
|
|
148
167
|
return this.receiving(queueName, cb, callbackError);
|
|
149
168
|
}
|
|
150
169
|
} else {
|
|
151
170
|
await timeout(5000);
|
|
152
171
|
return this.receiving(queueName, cb, callbackError);
|
|
153
172
|
}
|
|
173
|
+
return false;
|
|
154
174
|
}
|
|
155
175
|
|
|
156
176
|
setAlertCallback(callback) {
|
|
@@ -161,7 +181,7 @@ class RabbitMQAsync {
|
|
|
161
181
|
if (typeof this.alertCallback === 'function') {
|
|
162
182
|
this.alertCallback(status, msg);
|
|
163
183
|
} else {
|
|
164
|
-
console.
|
|
184
|
+
console.info(status, msg);
|
|
165
185
|
}
|
|
166
186
|
}
|
|
167
187
|
}
|