@azteam/rabbitmq-async 1.0.205 → 1.0.209
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/lib/RabbitMQAsync.js +257 -278
- package/package.json +1 -1
- package/src/RabbitMQAsync.js +118 -132
package/lib/RabbitMQAsync.js
CHANGED
|
@@ -22,6 +22,7 @@ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _ty
|
|
|
22
22
|
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
23
23
|
var RabbitMQAsync = /*#__PURE__*/function () {
|
|
24
24
|
function RabbitMQAsync(config) {
|
|
25
|
+
var _this = this;
|
|
25
26
|
_classCallCheck(this, RabbitMQAsync);
|
|
26
27
|
this.connected = false;
|
|
27
28
|
this.username = config.username;
|
|
@@ -29,43 +30,58 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
29
30
|
this.host = config.host;
|
|
30
31
|
this.prefix = config.prefix;
|
|
31
32
|
this.serverIp = config.server_ip;
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
33
|
+
this.listConsume = [];
|
|
34
|
+
this.client = null;
|
|
35
|
+
this.channel = null;
|
|
36
|
+
setInterval( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
37
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
38
|
+
while (1) switch (_context.prev = _context.next) {
|
|
39
|
+
case 0:
|
|
40
|
+
if (!_this.connected) {
|
|
41
|
+
_this.connect();
|
|
42
|
+
}
|
|
43
|
+
_this.startListConsume();
|
|
44
|
+
case 2:
|
|
45
|
+
case "end":
|
|
46
|
+
return _context.stop();
|
|
47
|
+
}
|
|
48
|
+
}, _callee);
|
|
49
|
+
})), 5000);
|
|
34
50
|
}
|
|
35
51
|
_createClass(RabbitMQAsync, [{
|
|
36
52
|
key: "waitConnection",
|
|
37
53
|
value: function () {
|
|
38
|
-
var _waitConnection = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
54
|
+
var _waitConnection = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
39
55
|
var n,
|
|
40
56
|
i,
|
|
41
|
-
|
|
42
|
-
return _regeneratorRuntime().wrap(function
|
|
43
|
-
while (1) switch (
|
|
57
|
+
_args2 = arguments;
|
|
58
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
59
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
44
60
|
case 0:
|
|
45
|
-
n =
|
|
61
|
+
n = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : 10;
|
|
46
62
|
i = 0;
|
|
47
63
|
case 2:
|
|
48
64
|
if (!(!this.connected || i < n)) {
|
|
49
|
-
|
|
65
|
+
_context2.next = 8;
|
|
50
66
|
break;
|
|
51
67
|
}
|
|
52
|
-
|
|
68
|
+
_context2.next = 5;
|
|
53
69
|
return (0, _util.timeout)(1000);
|
|
54
70
|
case 5:
|
|
55
71
|
i += 1;
|
|
56
|
-
|
|
72
|
+
_context2.next = 2;
|
|
57
73
|
break;
|
|
58
74
|
case 8:
|
|
59
75
|
if (this.connected) {
|
|
60
|
-
|
|
76
|
+
_context2.next = 10;
|
|
61
77
|
break;
|
|
62
78
|
}
|
|
63
79
|
throw new Error('Rabbit not connected');
|
|
64
80
|
case 10:
|
|
65
81
|
case "end":
|
|
66
|
-
return
|
|
82
|
+
return _context2.stop();
|
|
67
83
|
}
|
|
68
|
-
},
|
|
84
|
+
}, _callee2, this);
|
|
69
85
|
}));
|
|
70
86
|
function waitConnection() {
|
|
71
87
|
return _waitConnection.apply(this, arguments);
|
|
@@ -75,46 +91,48 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
75
91
|
}, {
|
|
76
92
|
key: "connect",
|
|
77
93
|
value: function () {
|
|
78
|
-
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
79
|
-
var
|
|
94
|
+
var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
95
|
+
var _this2 = this;
|
|
80
96
|
var opt;
|
|
81
|
-
return _regeneratorRuntime().wrap(function
|
|
82
|
-
while (1) switch (
|
|
97
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
98
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
83
99
|
case 0:
|
|
84
100
|
this._alert('connecting', 'MQ connecting...');
|
|
85
101
|
opt = {
|
|
86
102
|
credentials: _amqplib["default"].credentials.plain(this.username, this.password)
|
|
87
103
|
};
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
_context3.prev = 2;
|
|
105
|
+
_context3.next = 5;
|
|
90
106
|
return _amqplib["default"].connect("amqp://".concat(this.host), opt);
|
|
91
107
|
case 5:
|
|
92
|
-
this.client =
|
|
108
|
+
this.client = _context3.sent;
|
|
109
|
+
_context3.next = 8;
|
|
110
|
+
return this.client.createChannel();
|
|
111
|
+
case 8:
|
|
112
|
+
this.channel = _context3.sent;
|
|
113
|
+
this.channel.prefetch(1);
|
|
93
114
|
this.connected = true;
|
|
94
115
|
this._alert('connect', 'MQ connected');
|
|
95
116
|
this.client.on('error', function (err) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
process.exit(1);
|
|
117
|
+
_this2.close();
|
|
118
|
+
_this2._alert('error', "MQ Error ".concat(err));
|
|
99
119
|
});
|
|
100
120
|
this.client.on('close', function () {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
process.exit(1);
|
|
121
|
+
_this2.close();
|
|
122
|
+
_this2._alert('close', 'MQ closed');
|
|
104
123
|
});
|
|
105
|
-
|
|
124
|
+
_context3.next = 20;
|
|
106
125
|
break;
|
|
107
|
-
case
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
this.
|
|
111
|
-
this._alert('error', "MQ connect ".concat(
|
|
112
|
-
|
|
113
|
-
case 17:
|
|
126
|
+
case 16:
|
|
127
|
+
_context3.prev = 16;
|
|
128
|
+
_context3.t0 = _context3["catch"](2);
|
|
129
|
+
this.close();
|
|
130
|
+
this._alert('error', "MQ connect ".concat(_context3.t0));
|
|
131
|
+
case 20:
|
|
114
132
|
case "end":
|
|
115
|
-
return
|
|
133
|
+
return _context3.stop();
|
|
116
134
|
}
|
|
117
|
-
},
|
|
135
|
+
}, _callee3, this, [[2, 16]]);
|
|
118
136
|
}));
|
|
119
137
|
function connect() {
|
|
120
138
|
return _connect.apply(this, arguments);
|
|
@@ -123,18 +141,51 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
123
141
|
}()
|
|
124
142
|
}, {
|
|
125
143
|
key: "close",
|
|
126
|
-
value: function
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
144
|
+
value: function () {
|
|
145
|
+
var _close = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
146
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
147
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
148
|
+
case 0:
|
|
149
|
+
this.connected = false;
|
|
150
|
+
// eslint-disable-next-line array-callback-return
|
|
151
|
+
this.listConsume.map(function (c) {
|
|
152
|
+
// eslint-disable-next-line no-param-reassign
|
|
153
|
+
c.listened = false;
|
|
154
|
+
});
|
|
155
|
+
_context4.prev = 2;
|
|
156
|
+
_context4.next = 5;
|
|
157
|
+
return this.client.close();
|
|
158
|
+
case 5:
|
|
159
|
+
_context4.next = 9;
|
|
160
|
+
break;
|
|
161
|
+
case 7:
|
|
162
|
+
_context4.prev = 7;
|
|
163
|
+
_context4.t0 = _context4["catch"](2);
|
|
164
|
+
case 9:
|
|
165
|
+
_context4.prev = 9;
|
|
166
|
+
_context4.next = 12;
|
|
167
|
+
return this.channel.close();
|
|
168
|
+
case 12:
|
|
169
|
+
_context4.next = 16;
|
|
170
|
+
break;
|
|
171
|
+
case 14:
|
|
172
|
+
_context4.prev = 14;
|
|
173
|
+
_context4.t1 = _context4["catch"](9);
|
|
174
|
+
case 16:
|
|
175
|
+
case "end":
|
|
176
|
+
return _context4.stop();
|
|
177
|
+
}
|
|
178
|
+
}, _callee4, this, [[2, 7], [9, 14]]);
|
|
179
|
+
}));
|
|
180
|
+
function close() {
|
|
181
|
+
return _close.apply(this, arguments);
|
|
182
|
+
}
|
|
183
|
+
return close;
|
|
184
|
+
}()
|
|
130
185
|
}, {
|
|
131
186
|
key: "parsePrefix",
|
|
132
187
|
value: function parsePrefix(name) {
|
|
133
|
-
var self = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
134
188
|
var newName = name;
|
|
135
|
-
if (self) {
|
|
136
|
-
newName = "".concat(this.serverIp, "_").concat(newName);
|
|
137
|
-
}
|
|
138
189
|
if (this.prefix) {
|
|
139
190
|
newName = "".concat(this.prefix, ":").concat(newName);
|
|
140
191
|
}
|
|
@@ -143,79 +194,61 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
143
194
|
}, {
|
|
144
195
|
key: "send",
|
|
145
196
|
value: function () {
|
|
146
|
-
var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
197
|
+
var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
|
|
147
198
|
var msg,
|
|
148
199
|
limit,
|
|
149
200
|
prefixQueueName,
|
|
150
201
|
channel,
|
|
151
202
|
queueInfo,
|
|
152
|
-
|
|
153
|
-
return _regeneratorRuntime().wrap(function
|
|
154
|
-
while (1) switch (
|
|
203
|
+
_args5 = arguments;
|
|
204
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
205
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
155
206
|
case 0:
|
|
156
|
-
msg =
|
|
157
|
-
limit =
|
|
158
|
-
|
|
207
|
+
msg = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
|
|
208
|
+
limit = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : 0;
|
|
209
|
+
_context5.prev = 2;
|
|
210
|
+
prefixQueueName = this.parsePrefix(queueName);
|
|
159
211
|
if (!this.connected) {
|
|
160
|
-
|
|
212
|
+
_context5.next = 16;
|
|
161
213
|
break;
|
|
162
214
|
}
|
|
163
|
-
|
|
164
|
-
_context3.next = 7;
|
|
215
|
+
_context5.next = 7;
|
|
165
216
|
return this.client.createChannel();
|
|
166
217
|
case 7:
|
|
167
|
-
channel =
|
|
168
|
-
|
|
169
|
-
return channel.assertQueue(prefixQueueName, {
|
|
218
|
+
channel = _context5.sent;
|
|
219
|
+
_context5.next = 10;
|
|
220
|
+
return this.channel.assertQueue(prefixQueueName, {
|
|
170
221
|
durable: true
|
|
171
222
|
});
|
|
172
223
|
case 10:
|
|
173
|
-
queueInfo =
|
|
224
|
+
queueInfo = _context5.sent;
|
|
174
225
|
if (!(limit === 0 || queueInfo.messageCount < limit)) {
|
|
175
|
-
|
|
226
|
+
_context5.next = 16;
|
|
176
227
|
break;
|
|
177
228
|
}
|
|
178
|
-
|
|
229
|
+
_context5.next = 14;
|
|
179
230
|
return channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(_objectSpread({
|
|
180
231
|
retry: 5
|
|
181
232
|
}, msg))), {
|
|
182
233
|
persistent: true
|
|
183
234
|
});
|
|
184
235
|
case 14:
|
|
185
|
-
|
|
186
|
-
case 17:
|
|
187
|
-
_context3.prev = 17;
|
|
188
|
-
_context3.t0 = _context3["catch"](4);
|
|
189
|
-
_context3.next = 21;
|
|
190
|
-
return (0, _util.timeout)(2000);
|
|
191
|
-
case 21:
|
|
192
|
-
return _context3.abrupt("return", this.send(queueName, msg));
|
|
193
|
-
case 22:
|
|
194
|
-
_context3.prev = 22;
|
|
195
|
-
_context3.prev = 23;
|
|
196
|
-
_context3.next = 26;
|
|
236
|
+
_context5.next = 16;
|
|
197
237
|
return channel.close();
|
|
198
|
-
case
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
case 30:
|
|
205
|
-
return _context3.finish(22);
|
|
206
|
-
case 31:
|
|
207
|
-
_context3.next = 36;
|
|
208
|
-
break;
|
|
209
|
-
case 33:
|
|
210
|
-
_context3.next = 35;
|
|
238
|
+
case 16:
|
|
239
|
+
return _context5.abrupt("return", true);
|
|
240
|
+
case 19:
|
|
241
|
+
_context5.prev = 19;
|
|
242
|
+
_context5.t0 = _context5["catch"](2);
|
|
243
|
+
_context5.next = 23;
|
|
211
244
|
return (0, _util.timeout)(2000);
|
|
212
|
-
case
|
|
213
|
-
return
|
|
214
|
-
case
|
|
245
|
+
case 23:
|
|
246
|
+
return _context5.abrupt("return", this.send(queueName, msg));
|
|
247
|
+
case 24:
|
|
215
248
|
case "end":
|
|
216
|
-
return
|
|
249
|
+
return _context5.stop();
|
|
217
250
|
}
|
|
218
|
-
},
|
|
251
|
+
}, _callee5, this, [[2, 19]]);
|
|
219
252
|
}));
|
|
220
253
|
function send(_x) {
|
|
221
254
|
return _send.apply(this, arguments);
|
|
@@ -225,297 +258,243 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
225
258
|
}, {
|
|
226
259
|
key: "getInfo",
|
|
227
260
|
value: function () {
|
|
228
|
-
var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
229
|
-
var
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
_args4 = arguments;
|
|
233
|
-
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
234
|
-
while (1) switch (_context4.prev = _context4.next) {
|
|
261
|
+
var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(queueName) {
|
|
262
|
+
var prefixQueueName, channel;
|
|
263
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
264
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
235
265
|
case 0:
|
|
236
|
-
|
|
237
|
-
prefixQueueName = this.parsePrefix(queueName
|
|
266
|
+
_context6.prev = 0;
|
|
267
|
+
prefixQueueName = this.parsePrefix(queueName);
|
|
238
268
|
if (!this.connected) {
|
|
239
|
-
|
|
269
|
+
_context6.next = 7;
|
|
240
270
|
break;
|
|
241
271
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return this.client.createChannel();
|
|
245
|
-
case 6:
|
|
246
|
-
channel = _context4.sent;
|
|
247
|
-
_context4.next = 9;
|
|
272
|
+
channel = this.channel;
|
|
273
|
+
_context6.next = 6;
|
|
248
274
|
return channel.assertQueue(prefixQueueName, {
|
|
249
275
|
durable: true
|
|
250
276
|
});
|
|
277
|
+
case 6:
|
|
278
|
+
return _context6.abrupt("return", _context6.sent);
|
|
279
|
+
case 7:
|
|
280
|
+
_context6.next = 11;
|
|
281
|
+
break;
|
|
251
282
|
case 9:
|
|
252
|
-
|
|
283
|
+
_context6.prev = 9;
|
|
284
|
+
_context6.t0 = _context6["catch"](0);
|
|
285
|
+
case 11:
|
|
286
|
+
return _context6.abrupt("return", this.getInfo(queueName));
|
|
253
287
|
case 12:
|
|
254
|
-
_context4.prev = 12;
|
|
255
|
-
_context4.t0 = _context4["catch"](3);
|
|
256
|
-
return _context4.abrupt("return", this.getInfo(queueName));
|
|
257
|
-
case 15:
|
|
258
|
-
_context4.prev = 15;
|
|
259
|
-
_context4.prev = 16;
|
|
260
|
-
_context4.next = 19;
|
|
261
|
-
return channel.close();
|
|
262
|
-
case 19:
|
|
263
|
-
_context4.next = 23;
|
|
264
|
-
break;
|
|
265
|
-
case 21:
|
|
266
|
-
_context4.prev = 21;
|
|
267
|
-
_context4.t1 = _context4["catch"](16);
|
|
268
|
-
case 23:
|
|
269
|
-
return _context4.finish(15);
|
|
270
|
-
case 24:
|
|
271
|
-
_context4.next = 27;
|
|
272
|
-
break;
|
|
273
|
-
case 26:
|
|
274
|
-
return _context4.abrupt("return", this.getInfo(queueName));
|
|
275
|
-
case 27:
|
|
276
288
|
case "end":
|
|
277
|
-
return
|
|
289
|
+
return _context6.stop();
|
|
278
290
|
}
|
|
279
|
-
},
|
|
291
|
+
}, _callee6, this, [[0, 9]]);
|
|
280
292
|
}));
|
|
281
293
|
function getInfo(_x2) {
|
|
282
294
|
return _getInfo.apply(this, arguments);
|
|
283
295
|
}
|
|
284
296
|
return getInfo;
|
|
285
|
-
}()
|
|
297
|
+
}() // eslint-disable-next-line consistent-return
|
|
286
298
|
}, {
|
|
287
299
|
key: "consume",
|
|
288
300
|
value: function () {
|
|
289
|
-
var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
290
|
-
var
|
|
291
|
-
|
|
292
|
-
|
|
301
|
+
var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, asyncFunction) {
|
|
302
|
+
var callbackError,
|
|
303
|
+
prefixQueueName,
|
|
304
|
+
channel,
|
|
305
|
+
messageQueue,
|
|
306
|
+
_args8 = arguments;
|
|
307
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
308
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
293
309
|
case 0:
|
|
294
|
-
|
|
310
|
+
callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
|
|
311
|
+
_context8.prev = 1;
|
|
295
312
|
if (!this.connected) {
|
|
296
|
-
|
|
313
|
+
_context8.next = 10;
|
|
297
314
|
break;
|
|
298
315
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
316
|
+
prefixQueueName = this.parsePrefix(queueName), channel = this.channel;
|
|
317
|
+
if (!channel) {
|
|
318
|
+
_context8.next = 10;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
_context8.next = 7;
|
|
305
322
|
return channel.assertQueue(prefixQueueName, {
|
|
306
323
|
durable: true
|
|
307
324
|
});
|
|
308
|
-
case
|
|
309
|
-
_context6.next = 10;
|
|
310
|
-
return channel.prefetch(1);
|
|
311
|
-
case 10:
|
|
325
|
+
case 7:
|
|
312
326
|
messageQueue = this;
|
|
313
|
-
|
|
327
|
+
_context8.next = 10;
|
|
314
328
|
return new Promise(function (resolve, reject) {
|
|
315
329
|
channel.consume(prefixQueueName, /*#__PURE__*/function () {
|
|
316
|
-
var
|
|
330
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
|
|
317
331
|
var data, errString;
|
|
318
|
-
return _regeneratorRuntime().wrap(function
|
|
319
|
-
while (1) switch (
|
|
332
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
333
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
320
334
|
case 0:
|
|
321
335
|
if (!msg) {
|
|
322
|
-
|
|
336
|
+
_context7.next = 47;
|
|
323
337
|
break;
|
|
324
338
|
}
|
|
325
339
|
data = JSON.parse(msg.content.toString());
|
|
326
340
|
if (!data) {
|
|
327
|
-
|
|
341
|
+
_context7.next = 46;
|
|
328
342
|
break;
|
|
329
343
|
}
|
|
330
|
-
|
|
344
|
+
_context7.prev = 3;
|
|
331
345
|
if (!msg.fields.redelivered) {
|
|
332
|
-
|
|
346
|
+
_context7.next = 17;
|
|
333
347
|
break;
|
|
334
348
|
}
|
|
335
349
|
data.retry -= 1;
|
|
336
350
|
if (!(data.retry > 0)) {
|
|
337
|
-
|
|
351
|
+
_context7.next = 11;
|
|
338
352
|
break;
|
|
339
353
|
}
|
|
340
|
-
|
|
341
|
-
_context5.next = 12;
|
|
342
|
-
break;
|
|
343
|
-
}
|
|
344
|
-
_context5.next = 10;
|
|
345
|
-
return messageQueue.send(queueName, _objectSpread(_objectSpread({}, data), {}, {
|
|
346
|
-
self: true
|
|
347
|
-
}));
|
|
348
|
-
case 10:
|
|
349
|
-
_context5.next = 14;
|
|
350
|
-
break;
|
|
351
|
-
case 12:
|
|
352
|
-
_context5.next = 14;
|
|
354
|
+
_context7.next = 9;
|
|
353
355
|
return messageQueue.send(queueName, data);
|
|
354
|
-
case
|
|
355
|
-
|
|
356
|
+
case 9:
|
|
357
|
+
_context7.next = 13;
|
|
356
358
|
break;
|
|
357
|
-
case
|
|
358
|
-
|
|
359
|
+
case 11:
|
|
360
|
+
_context7.next = 13;
|
|
359
361
|
return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
360
362
|
queueName: queueName,
|
|
361
363
|
data: data,
|
|
362
364
|
retry_time: new Date(),
|
|
363
365
|
ip: messageQueue.serverIp
|
|
364
366
|
});
|
|
365
|
-
case
|
|
366
|
-
|
|
367
|
+
case 13:
|
|
368
|
+
_context7.next = 15;
|
|
367
369
|
return channel.ack(msg);
|
|
368
|
-
case
|
|
369
|
-
|
|
370
|
+
case 15:
|
|
371
|
+
_context7.next = 39;
|
|
370
372
|
break;
|
|
371
|
-
case
|
|
372
|
-
|
|
373
|
-
|
|
373
|
+
case 17:
|
|
374
|
+
_context7.prev = 17;
|
|
375
|
+
_context7.next = 20;
|
|
374
376
|
return asyncFunction(data);
|
|
375
|
-
case
|
|
376
|
-
|
|
377
|
+
case 20:
|
|
378
|
+
_context7.next = 22;
|
|
377
379
|
return channel.ack(msg);
|
|
378
|
-
case
|
|
379
|
-
|
|
380
|
+
case 22:
|
|
381
|
+
_context7.next = 39;
|
|
380
382
|
break;
|
|
381
|
-
case
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
errString =
|
|
383
|
+
case 24:
|
|
384
|
+
_context7.prev = 24;
|
|
385
|
+
_context7.t0 = _context7["catch"](17);
|
|
386
|
+
errString = _context7.t0.toString();
|
|
385
387
|
if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
|
|
386
388
|
return _lodash["default"].includes(errString, el);
|
|
387
389
|
})) {
|
|
388
|
-
|
|
390
|
+
_context7.next = 31;
|
|
389
391
|
break;
|
|
390
392
|
}
|
|
391
|
-
reject(
|
|
392
|
-
|
|
393
|
+
reject(_context7.t0);
|
|
394
|
+
_context7.next = 39;
|
|
393
395
|
break;
|
|
394
|
-
case
|
|
396
|
+
case 31:
|
|
395
397
|
if (callbackError && data.retry === 1) {
|
|
396
|
-
callbackError(prefixQueueName,
|
|
398
|
+
callbackError(prefixQueueName, errString);
|
|
397
399
|
}
|
|
398
400
|
if (!(data.retry <= 0)) {
|
|
399
|
-
|
|
401
|
+
_context7.next = 37;
|
|
400
402
|
break;
|
|
401
403
|
}
|
|
402
|
-
|
|
404
|
+
_context7.next = 35;
|
|
403
405
|
return channel.ack(msg);
|
|
404
|
-
case
|
|
405
|
-
|
|
406
|
+
case 35:
|
|
407
|
+
_context7.next = 39;
|
|
406
408
|
break;
|
|
407
|
-
case
|
|
408
|
-
|
|
409
|
+
case 37:
|
|
410
|
+
_context7.next = 39;
|
|
409
411
|
return channel.nack(msg);
|
|
412
|
+
case 39:
|
|
413
|
+
_context7.next = 44;
|
|
414
|
+
break;
|
|
415
|
+
case 41:
|
|
416
|
+
_context7.prev = 41;
|
|
417
|
+
_context7.t1 = _context7["catch"](3);
|
|
418
|
+
reject(_context7.t1);
|
|
410
419
|
case 44:
|
|
411
|
-
|
|
420
|
+
_context7.next = 47;
|
|
412
421
|
break;
|
|
413
422
|
case 46:
|
|
414
|
-
_context5.prev = 46;
|
|
415
|
-
_context5.t1 = _context5["catch"](3);
|
|
416
|
-
reject(_context5.t1);
|
|
417
|
-
case 49:
|
|
418
|
-
_context5.next = 52;
|
|
419
|
-
break;
|
|
420
|
-
case 51:
|
|
421
423
|
channel.ack(msg);
|
|
422
|
-
case
|
|
423
|
-
_context5.next = 55;
|
|
424
|
-
break;
|
|
425
|
-
case 54:
|
|
426
|
-
reject(new Error('msg null'));
|
|
427
|
-
case 55:
|
|
424
|
+
case 47:
|
|
428
425
|
case "end":
|
|
429
|
-
return
|
|
426
|
+
return _context7.stop();
|
|
430
427
|
}
|
|
431
|
-
},
|
|
428
|
+
}, _callee7, null, [[3, 41], [17, 24]]);
|
|
432
429
|
}));
|
|
433
|
-
return function (
|
|
434
|
-
return
|
|
430
|
+
return function (_x5) {
|
|
431
|
+
return _ref2.apply(this, arguments);
|
|
435
432
|
};
|
|
436
433
|
}());
|
|
437
434
|
});
|
|
438
|
-
case
|
|
439
|
-
|
|
440
|
-
break;
|
|
441
|
-
case 15:
|
|
442
|
-
_context6.prev = 15;
|
|
443
|
-
_context6.t0 = _context6["catch"](2);
|
|
444
|
-
_context6.prev = 17;
|
|
445
|
-
_context6.next = 20;
|
|
446
|
-
return channel.close();
|
|
447
|
-
case 20:
|
|
448
|
-
_context6.next = 24;
|
|
449
|
-
break;
|
|
450
|
-
case 22:
|
|
451
|
-
_context6.prev = 22;
|
|
452
|
-
_context6.t1 = _context6["catch"](17);
|
|
453
|
-
case 24:
|
|
454
|
-
_context6.next = 26;
|
|
455
|
-
return (0, _util.timeout)(5000);
|
|
456
|
-
case 26:
|
|
457
|
-
return _context6.abrupt("return", this.consume(queueName, self, asyncFunction, callbackError));
|
|
458
|
-
case 27:
|
|
459
|
-
_context6.next = 32;
|
|
435
|
+
case 10:
|
|
436
|
+
_context8.next = 17;
|
|
460
437
|
break;
|
|
461
|
-
case
|
|
462
|
-
|
|
438
|
+
case 12:
|
|
439
|
+
_context8.prev = 12;
|
|
440
|
+
_context8.t0 = _context8["catch"](1);
|
|
441
|
+
_context8.next = 16;
|
|
463
442
|
return (0, _util.timeout)(5000);
|
|
464
|
-
case
|
|
465
|
-
return
|
|
466
|
-
case
|
|
467
|
-
return _context6.abrupt("return", true);
|
|
468
|
-
case 33:
|
|
443
|
+
case 16:
|
|
444
|
+
return _context8.abrupt("return", this.consume(queueName, asyncFunction, callbackError));
|
|
445
|
+
case 17:
|
|
469
446
|
case "end":
|
|
470
|
-
return
|
|
447
|
+
return _context8.stop();
|
|
471
448
|
}
|
|
472
|
-
},
|
|
449
|
+
}, _callee8, this, [[1, 12]]);
|
|
473
450
|
}));
|
|
474
|
-
function consume(_x3, _x4
|
|
451
|
+
function consume(_x3, _x4) {
|
|
475
452
|
return _consume.apply(this, arguments);
|
|
476
453
|
}
|
|
477
454
|
return consume;
|
|
478
455
|
}()
|
|
479
456
|
}, {
|
|
480
|
-
key: "
|
|
481
|
-
value: function () {
|
|
482
|
-
var
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
457
|
+
key: "startListConsume",
|
|
458
|
+
value: function startListConsume() {
|
|
459
|
+
var _this3 = this;
|
|
460
|
+
if (this.connected) {
|
|
461
|
+
// eslint-disable-next-line array-callback-return
|
|
462
|
+
this.listConsume.map(function (c) {
|
|
463
|
+
if (!c.listened) {
|
|
464
|
+
_this3.consume(c.queueName, c.asyncFunction, c.callbackError);
|
|
465
|
+
// eslint-disable-next-line no-param-reassign
|
|
466
|
+
c.listened = true;
|
|
490
467
|
}
|
|
491
|
-
}
|
|
492
|
-
}));
|
|
493
|
-
function selfReceiving(_x8, _x9, _x10) {
|
|
494
|
-
return _selfReceiving.apply(this, arguments);
|
|
468
|
+
});
|
|
495
469
|
}
|
|
496
|
-
|
|
497
|
-
}()
|
|
470
|
+
}
|
|
498
471
|
}, {
|
|
499
472
|
key: "receiving",
|
|
500
473
|
value: function () {
|
|
501
|
-
var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
474
|
+
var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(queueName, asyncFunction) {
|
|
502
475
|
var callbackError,
|
|
503
|
-
|
|
504
|
-
return _regeneratorRuntime().wrap(function
|
|
505
|
-
while (1) switch (
|
|
476
|
+
_args9 = arguments;
|
|
477
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
478
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
506
479
|
case 0:
|
|
507
|
-
callbackError =
|
|
508
|
-
this.
|
|
509
|
-
|
|
510
|
-
|
|
480
|
+
callbackError = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : null;
|
|
481
|
+
if (!_lodash["default"].find(this.listConsume, function (c) {
|
|
482
|
+
return c.queueName === queueName;
|
|
483
|
+
})) {
|
|
484
|
+
this.listConsume.push({
|
|
485
|
+
queueName: queueName,
|
|
486
|
+
asyncFunction: asyncFunction,
|
|
487
|
+
callbackError: callbackError,
|
|
488
|
+
listened: false
|
|
489
|
+
});
|
|
511
490
|
}
|
|
512
|
-
case
|
|
491
|
+
case 2:
|
|
513
492
|
case "end":
|
|
514
|
-
return
|
|
493
|
+
return _context9.stop();
|
|
515
494
|
}
|
|
516
|
-
},
|
|
495
|
+
}, _callee9, this);
|
|
517
496
|
}));
|
|
518
|
-
function receiving(
|
|
497
|
+
function receiving(_x6, _x7) {
|
|
519
498
|
return _receiving.apply(this, arguments);
|
|
520
499
|
}
|
|
521
500
|
return receiving;
|
package/package.json
CHANGED
package/src/RabbitMQAsync.js
CHANGED
|
@@ -11,8 +11,18 @@ class RabbitMQAsync {
|
|
|
11
11
|
this.host = config.host;
|
|
12
12
|
this.prefix = config.prefix;
|
|
13
13
|
this.serverIp = config.server_ip;
|
|
14
|
-
|
|
15
|
-
this.
|
|
14
|
+
|
|
15
|
+
this.listConsume = [];
|
|
16
|
+
|
|
17
|
+
this.client = null;
|
|
18
|
+
this.channel = null;
|
|
19
|
+
|
|
20
|
+
setInterval(async () => {
|
|
21
|
+
if (!this.connected) {
|
|
22
|
+
this.connect();
|
|
23
|
+
}
|
|
24
|
+
this.startListConsume();
|
|
25
|
+
}, 5000);
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
async waitConnection(n = 10) {
|
|
@@ -29,38 +39,42 @@ class RabbitMQAsync {
|
|
|
29
39
|
const opt = {credentials: amqp.credentials.plain(this.username, this.password)};
|
|
30
40
|
try {
|
|
31
41
|
this.client = await amqp.connect(`amqp://${this.host}`, opt);
|
|
42
|
+
this.channel = await this.client.createChannel();
|
|
43
|
+
this.channel.prefetch(1);
|
|
32
44
|
this.connected = true;
|
|
33
45
|
this._alert('connect', 'MQ connected');
|
|
34
|
-
|
|
35
46
|
this.client.on('error', (err) => {
|
|
36
|
-
this.
|
|
47
|
+
this.close();
|
|
37
48
|
this._alert('error', `MQ Error ${err}`);
|
|
38
|
-
|
|
39
|
-
process.exit(1);
|
|
40
49
|
});
|
|
41
50
|
|
|
42
51
|
this.client.on('close', () => {
|
|
43
|
-
this.
|
|
52
|
+
this.close();
|
|
44
53
|
this._alert('close', 'MQ closed');
|
|
45
|
-
process.exit(1);
|
|
46
54
|
});
|
|
47
55
|
} catch (err) {
|
|
48
|
-
this.
|
|
56
|
+
this.close();
|
|
49
57
|
this._alert('error', `MQ connect ${err}`);
|
|
50
|
-
process.exit(1);
|
|
51
58
|
}
|
|
52
59
|
}
|
|
53
60
|
|
|
54
|
-
close() {
|
|
61
|
+
async close() {
|
|
55
62
|
this.connected = false;
|
|
56
|
-
|
|
63
|
+
// eslint-disable-next-line array-callback-return
|
|
64
|
+
this.listConsume.map((c) => {
|
|
65
|
+
// eslint-disable-next-line no-param-reassign
|
|
66
|
+
c.listened = false;
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
await this.client.close();
|
|
70
|
+
} catch (err) {}
|
|
71
|
+
try {
|
|
72
|
+
await this.channel.close();
|
|
73
|
+
} catch (err) {}
|
|
57
74
|
}
|
|
58
75
|
|
|
59
|
-
parsePrefix(name
|
|
76
|
+
parsePrefix(name) {
|
|
60
77
|
let newName = name;
|
|
61
|
-
if (self) {
|
|
62
|
-
newName = `${this.serverIp}_${newName}`;
|
|
63
|
-
}
|
|
64
78
|
if (this.prefix) {
|
|
65
79
|
newName = `${this.prefix}:${newName}`;
|
|
66
80
|
}
|
|
@@ -68,159 +82,131 @@ class RabbitMQAsync {
|
|
|
68
82
|
}
|
|
69
83
|
|
|
70
84
|
async send(queueName, msg = {}, limit = 0) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
durable: true,
|
|
79
|
-
});
|
|
85
|
+
try {
|
|
86
|
+
const prefixQueueName = this.parsePrefix(queueName);
|
|
87
|
+
if (this.connected) {
|
|
88
|
+
const channel = await this.client.createChannel(),
|
|
89
|
+
queueInfo = await this.channel.assertQueue(prefixQueueName, {
|
|
90
|
+
durable: true,
|
|
91
|
+
});
|
|
80
92
|
|
|
81
93
|
if (limit === 0 || queueInfo.messageCount < limit) {
|
|
82
94
|
await channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify({retry: 5, ...msg})), {
|
|
83
95
|
persistent: true,
|
|
84
96
|
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return true;
|
|
88
|
-
} catch (err) {
|
|
89
|
-
await timeout(2000);
|
|
90
|
-
return this.send(queueName, msg);
|
|
91
|
-
} finally {
|
|
92
|
-
try {
|
|
93
97
|
await channel.close();
|
|
94
|
-
}
|
|
98
|
+
}
|
|
95
99
|
}
|
|
96
|
-
|
|
100
|
+
|
|
101
|
+
return true;
|
|
102
|
+
} catch (err) {
|
|
97
103
|
await timeout(2000);
|
|
98
104
|
return this.send(queueName, msg);
|
|
99
105
|
}
|
|
100
106
|
}
|
|
101
107
|
|
|
102
|
-
async getInfo(queueName
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
channel = await this.client.createChannel();
|
|
108
|
+
async getInfo(queueName) {
|
|
109
|
+
try {
|
|
110
|
+
const prefixQueueName = this.parsePrefix(queueName);
|
|
111
|
+
if (this.connected) {
|
|
112
|
+
const {channel} = this;
|
|
109
113
|
return await channel.assertQueue(prefixQueueName, {
|
|
110
114
|
durable: true,
|
|
111
115
|
});
|
|
112
|
-
} catch (err) {
|
|
113
|
-
return this.getInfo(queueName);
|
|
114
|
-
} finally {
|
|
115
|
-
try {
|
|
116
|
-
await channel.close();
|
|
117
|
-
} catch (err) {}
|
|
118
116
|
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
}
|
|
117
|
+
} catch (err) {}
|
|
118
|
+
return this.getInfo(queueName);
|
|
122
119
|
}
|
|
123
120
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (data) {
|
|
146
|
-
try {
|
|
147
|
-
if (msg.fields.redelivered) {
|
|
148
|
-
data.retry -= 1;
|
|
149
|
-
if (data.retry > 0) {
|
|
150
|
-
if (messageQueue.worker) {
|
|
151
|
-
await messageQueue.send(queueName, {
|
|
152
|
-
...data,
|
|
153
|
-
self: true,
|
|
154
|
-
});
|
|
155
|
-
} else {
|
|
121
|
+
// eslint-disable-next-line consistent-return
|
|
122
|
+
async consume(queueName, asyncFunction, callbackError = null) {
|
|
123
|
+
try {
|
|
124
|
+
if (this.connected) {
|
|
125
|
+
const prefixQueueName = this.parsePrefix(queueName),
|
|
126
|
+
{channel} = this;
|
|
127
|
+
if (channel) {
|
|
128
|
+
await channel.assertQueue(prefixQueueName, {
|
|
129
|
+
durable: true,
|
|
130
|
+
});
|
|
131
|
+
const messageQueue = this;
|
|
132
|
+
await new Promise((resolve, reject) => {
|
|
133
|
+
channel.consume(prefixQueueName, async function (msg) {
|
|
134
|
+
if (msg) {
|
|
135
|
+
const data = JSON.parse(msg.content.toString());
|
|
136
|
+
|
|
137
|
+
if (data) {
|
|
138
|
+
try {
|
|
139
|
+
if (msg.fields.redelivered) {
|
|
140
|
+
data.retry -= 1;
|
|
141
|
+
if (data.retry > 0) {
|
|
156
142
|
await messageQueue.send(queueName, data);
|
|
143
|
+
} else {
|
|
144
|
+
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
145
|
+
queueName,
|
|
146
|
+
data,
|
|
147
|
+
retry_time: new Date(),
|
|
148
|
+
ip: messageQueue.serverIp,
|
|
149
|
+
});
|
|
157
150
|
}
|
|
158
|
-
} else {
|
|
159
|
-
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
160
|
-
queueName,
|
|
161
|
-
data,
|
|
162
|
-
retry_time: new Date(),
|
|
163
|
-
ip: messageQueue.serverIp,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
await channel.ack(msg);
|
|
167
|
-
} else {
|
|
168
|
-
try {
|
|
169
|
-
await asyncFunction(data);
|
|
170
151
|
await channel.ack(msg);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
if (data.retry <= 0) {
|
|
181
|
-
await channel.ack(msg);
|
|
152
|
+
} else {
|
|
153
|
+
try {
|
|
154
|
+
await asyncFunction(data);
|
|
155
|
+
await channel.ack(msg);
|
|
156
|
+
} catch (err1) {
|
|
157
|
+
const errString = err1.toString();
|
|
158
|
+
if (_.some(['IllegalOperationError: Channel closed'], (el) => _.includes(errString, el))) {
|
|
159
|
+
reject(err1);
|
|
182
160
|
} else {
|
|
183
|
-
|
|
161
|
+
if (callbackError && data.retry === 1) {
|
|
162
|
+
callbackError(prefixQueueName, errString);
|
|
163
|
+
}
|
|
164
|
+
if (data.retry <= 0) {
|
|
165
|
+
await channel.ack(msg);
|
|
166
|
+
} else {
|
|
167
|
+
await channel.nack(msg);
|
|
168
|
+
}
|
|
184
169
|
}
|
|
185
170
|
}
|
|
186
171
|
}
|
|
172
|
+
} catch (err2) {
|
|
173
|
+
reject(err2);
|
|
187
174
|
}
|
|
188
|
-
}
|
|
189
|
-
|
|
175
|
+
} else {
|
|
176
|
+
channel.ack(msg);
|
|
190
177
|
}
|
|
191
|
-
} else {
|
|
192
|
-
channel.ack(msg);
|
|
193
178
|
}
|
|
194
|
-
}
|
|
195
|
-
reject(new Error('msg null'));
|
|
196
|
-
}
|
|
179
|
+
});
|
|
197
180
|
});
|
|
198
|
-
}
|
|
199
|
-
} catch (err) {
|
|
200
|
-
try {
|
|
201
|
-
await channel.close();
|
|
202
|
-
} catch (err2) {}
|
|
203
|
-
|
|
204
|
-
await timeout(5000);
|
|
205
|
-
return this.consume(queueName, self, asyncFunction, callbackError);
|
|
181
|
+
}
|
|
206
182
|
}
|
|
207
|
-
}
|
|
183
|
+
} catch (err3) {
|
|
208
184
|
await timeout(5000);
|
|
209
|
-
return this.consume(queueName,
|
|
185
|
+
return this.consume(queueName, asyncFunction, callbackError);
|
|
210
186
|
}
|
|
211
|
-
|
|
212
|
-
return true;
|
|
213
187
|
}
|
|
214
188
|
|
|
215
|
-
|
|
216
|
-
this.
|
|
189
|
+
startListConsume() {
|
|
190
|
+
if (this.connected) {
|
|
191
|
+
// eslint-disable-next-line array-callback-return
|
|
192
|
+
this.listConsume.map((c) => {
|
|
193
|
+
if (!c.listened) {
|
|
194
|
+
this.consume(c.queueName, c.asyncFunction, c.callbackError);
|
|
195
|
+
// eslint-disable-next-line no-param-reassign
|
|
196
|
+
c.listened = true;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
217
200
|
}
|
|
218
201
|
|
|
219
202
|
async receiving(queueName, asyncFunction, callbackError = null) {
|
|
220
|
-
this.
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
203
|
+
if (!_.find(this.listConsume, (c) => c.queueName === queueName)) {
|
|
204
|
+
this.listConsume.push({
|
|
205
|
+
queueName,
|
|
206
|
+
asyncFunction,
|
|
207
|
+
callbackError,
|
|
208
|
+
listened: false,
|
|
209
|
+
});
|
|
224
210
|
}
|
|
225
211
|
}
|
|
226
212
|
|