@azteam/rabbitmq-async 1.0.120 → 1.0.123

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/rabbitmq-async",
3
- "version": "1.0.120",
3
+ "version": "1.0.123",
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.8.0"
20
+ "amqplib": "0.10.1"
21
21
  }
22
22
  }
@@ -42,14 +42,14 @@ class RabbitMQAsync {
42
42
  } catch (err) {
43
43
  this.connected = false;
44
44
  this._alert('error', `MQ connect${err}`);
45
- this.reconnect();
45
+ await this.reconnect();
46
46
  }
47
47
  }
48
48
 
49
49
  async reconnect() {
50
50
  this._alert('reconnect', 'MQ try reconnect...');
51
51
  await timeout(5000);
52
- this.connect();
52
+ await this.connect();
53
53
  }
54
54
 
55
55
  close() {
@@ -112,39 +112,47 @@ class RabbitMQAsync {
112
112
  await channel.prefetch(1);
113
113
 
114
114
  const messageQueue = this;
115
- channel.consume(prefixQueueName, async function (msg) {
116
- const data = JSON.parse(msg.content.toString());
117
-
118
- try {
119
- if (msg.fields.redelivered) {
120
- if (!data.retry) {
121
- data.retry = 0;
122
- }
123
- data.retry += 1;
124
115
 
125
- if (data.retry < 5) {
126
- await messageQueue.send(queueName, data);
116
+ await new Promise((resolve, reject) => {
117
+ channel.consume(prefixQueueName, async function (msg) {
118
+ const data = JSON.parse(msg.content.toString());
119
+
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);
127
137
  } else {
128
- await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
129
- queueName,
130
- data,
131
- retry_time: new Date(),
132
- });
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
+ }
133
151
  }
134
- } else {
135
- await cb(data);
136
- }
137
- await channel.ack(msg);
138
- } catch (err) {
139
- if (data.noRetry) {
140
- await channel.ack(msg);
141
- } else {
142
- await channel.nack(msg);
152
+ } catch (err) {
153
+ reject(err);
143
154
  }
144
- if (callbackError) {
145
- callbackError(prefixQueueName, err);
146
- }
147
- }
155
+ });
148
156
  });
149
157
  } catch (err) {
150
158
  try {