@azteam/rabbitmq-async 1.0.205 → 1.0.208

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