@azteam/rabbitmq-async 1.0.231 → 1.0.234

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,7 +22,6 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
22
22
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
23
23
  var RabbitMQAsync = /*#__PURE__*/function () {
24
24
  function RabbitMQAsync(config) {
25
- var _this = this;
26
25
  _classCallCheck(this, RabbitMQAsync);
27
26
  this.connected = false;
28
27
  this.username = config.username;
@@ -32,151 +31,190 @@ var RabbitMQAsync = /*#__PURE__*/function () {
32
31
  this.serverIp = config.server_ip;
33
32
  this.retryKey = config.retry_key;
34
33
  this.listConsume = [];
34
+ this.listSub = [];
35
35
  this.client = null;
36
36
  this.channel = null;
37
- setInterval(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
38
- return _regeneratorRuntime().wrap(function _callee$(_context) {
39
- while (1) switch (_context.prev = _context.next) {
40
- case 0:
41
- if (!_this.connected) {
42
- _this.connect();
43
- }
44
- _this.startListConsume();
45
- case 2:
46
- case "end":
47
- return _context.stop();
48
- }
49
- }, _callee);
50
- })), 5000);
37
+ this.reconnecting = false;
38
+ this.connect();
51
39
  }
52
40
  return _createClass(RabbitMQAsync, [{
53
41
  key: "waitConnection",
54
42
  value: function () {
55
- var _waitConnection = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
43
+ var _waitConnection = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
56
44
  var n,
57
45
  i,
58
- _args2 = arguments;
59
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
60
- while (1) switch (_context2.prev = _context2.next) {
46
+ _args = arguments;
47
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
48
+ while (1) switch (_context.prev = _context.next) {
61
49
  case 0:
62
- n = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : 10;
50
+ n = _args.length > 0 && _args[0] !== undefined ? _args[0] : 60;
63
51
  i = 0;
64
52
  case 2:
65
- if (!(!this.connected || i < n)) {
66
- _context2.next = 8;
53
+ if (!(!this.connected && i < n)) {
54
+ _context.next = 8;
67
55
  break;
68
56
  }
69
- _context2.next = 5;
57
+ _context.next = 5;
70
58
  return (0, _util.timeout)(1000);
71
59
  case 5:
72
60
  i += 1;
73
- _context2.next = 2;
61
+ _context.next = 2;
74
62
  break;
75
63
  case 8:
76
64
  if (this.connected) {
77
- _context2.next = 10;
65
+ _context.next = 10;
78
66
  break;
79
67
  }
80
68
  throw new Error('Rabbit not connected');
81
69
  case 10:
82
70
  case "end":
83
- return _context2.stop();
71
+ return _context.stop();
84
72
  }
85
- }, _callee2, this);
73
+ }, _callee, this);
86
74
  }));
87
75
  function waitConnection() {
88
76
  return _waitConnection.apply(this, arguments);
89
77
  }
90
78
  return waitConnection;
91
79
  }()
80
+ }, {
81
+ key: "scheduleReconnect",
82
+ value: function scheduleReconnect() {
83
+ var _this = this;
84
+ if (this.reconnecting) return;
85
+ this.reconnecting = true;
86
+ setTimeout(function () {
87
+ _this.reconnecting = false;
88
+ if (!_this.connected) {
89
+ _this.connect();
90
+ }
91
+ }, 5000).unref();
92
+ }
92
93
  }, {
93
94
  key: "connect",
94
95
  value: function () {
95
- var _connect = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
96
+ var _connect = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
96
97
  var _this2 = this;
97
98
  var opt;
98
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
99
- while (1) switch (_context3.prev = _context3.next) {
99
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
100
+ while (1) switch (_context2.prev = _context2.next) {
100
101
  case 0:
102
+ if (!this.connected) {
103
+ _context2.next = 2;
104
+ break;
105
+ }
106
+ return _context2.abrupt("return");
107
+ case 2:
101
108
  this._alert('connecting', 'MQ connecting...');
102
109
  opt = {
103
110
  credentials: _amqplib["default"].credentials.plain(this.username, this.password)
104
111
  };
105
- _context3.prev = 2;
106
- _context3.next = 5;
112
+ _context2.prev = 4;
113
+ _context2.next = 7;
107
114
  return _amqplib["default"].connect("amqp://".concat(this.host), opt);
108
- case 5:
109
- this.client = _context3.sent;
110
- _context3.next = 8;
111
- return this.client.createChannel();
112
- case 8:
113
- this.channel = _context3.sent;
114
- this.channel.prefetch(1);
115
- this.connected = true;
116
- this._alert('connect', 'MQ connected');
115
+ case 7:
116
+ this.client = _context2.sent;
117
117
  this.client.on('error', function (err) {
118
- _this2.close();
119
118
  _this2._alert('error', "MQ Error ".concat(err));
119
+ _this2.handleDisconnect();
120
120
  });
121
121
  this.client.on('close', function () {
122
- _this2.close();
123
122
  _this2._alert('close', 'MQ closed');
123
+ _this2.handleDisconnect();
124
124
  });
125
- _context3.next = 20;
125
+ _context2.next = 12;
126
+ return this.client.createChannel();
127
+ case 12:
128
+ this.channel = _context2.sent;
129
+ this.channel.prefetch(1);
130
+ this.connected = true;
131
+ this._alert('connect', 'MQ connected');
132
+ this.startListConsume();
133
+ this.startListSub();
134
+ _context2.next = 24;
126
135
  break;
127
- case 16:
128
- _context3.prev = 16;
129
- _context3.t0 = _context3["catch"](2);
130
- this.close();
131
- this._alert('error', "MQ connect ".concat(_context3.t0));
132
136
  case 20:
137
+ _context2.prev = 20;
138
+ _context2.t0 = _context2["catch"](4);
139
+ this._alert('error', "MQ connect error: ".concat(_context2.t0));
140
+ this.handleDisconnect();
141
+ case 24:
133
142
  case "end":
134
- return _context3.stop();
143
+ return _context2.stop();
135
144
  }
136
- }, _callee3, this, [[2, 16]]);
145
+ }, _callee2, this, [[4, 20]]);
137
146
  }));
138
147
  function connect() {
139
148
  return _connect.apply(this, arguments);
140
149
  }
141
150
  return connect;
142
151
  }()
152
+ }, {
153
+ key: "handleDisconnect",
154
+ value: function handleDisconnect() {
155
+ if (this.connected) {
156
+ this.connected = false;
157
+ this.listConsume.forEach(function (c) {
158
+ c.listened = false;
159
+ });
160
+ this.listSub.forEach(function (s) {
161
+ s.listened = false;
162
+ });
163
+ }
164
+ this.client = null;
165
+ this.channel = null;
166
+ this.scheduleReconnect();
167
+ }
143
168
  }, {
144
169
  key: "close",
145
170
  value: function () {
146
- var _close = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
147
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
148
- while (1) switch (_context4.prev = _context4.next) {
171
+ var _close = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
172
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
173
+ while (1) switch (_context3.prev = _context3.next) {
149
174
  case 0:
150
175
  this.connected = false;
151
- // eslint-disable-next-line array-callback-return
152
- this.listConsume.map(function (c) {
153
- // eslint-disable-next-line no-param-reassign
176
+ this.listConsume.forEach(function (c) {
154
177
  c.listened = false;
155
178
  });
156
- _context4.prev = 2;
157
- _context4.next = 5;
158
- return this.client.close();
159
- case 5:
160
- _context4.next = 9;
161
- break;
179
+ this.listSub.forEach(function (s) {
180
+ s.listened = false;
181
+ });
182
+ if (!this.channel) {
183
+ _context3.next = 12;
184
+ break;
185
+ }
186
+ _context3.prev = 4;
187
+ _context3.next = 7;
188
+ return this.channel.close();
162
189
  case 7:
163
- _context4.prev = 7;
164
- _context4.t0 = _context4["catch"](2);
190
+ _context3.next = 11;
191
+ break;
165
192
  case 9:
166
- _context4.prev = 9;
167
- _context4.next = 12;
168
- return this.channel.close();
193
+ _context3.prev = 9;
194
+ _context3.t0 = _context3["catch"](4);
195
+ case 11:
196
+ this.channel = null;
169
197
  case 12:
170
- _context4.next = 16;
171
- break;
172
- case 14:
173
- _context4.prev = 14;
174
- _context4.t1 = _context4["catch"](9);
198
+ if (!this.client) {
199
+ _context3.next = 21;
200
+ break;
201
+ }
202
+ _context3.prev = 13;
203
+ _context3.next = 16;
204
+ return this.client.close();
175
205
  case 16:
206
+ _context3.next = 20;
207
+ break;
208
+ case 18:
209
+ _context3.prev = 18;
210
+ _context3.t1 = _context3["catch"](13);
211
+ case 20:
212
+ this.client = null;
213
+ case 21:
176
214
  case "end":
177
- return _context4.stop();
215
+ return _context3.stop();
178
216
  }
179
- }, _callee4, this, [[2, 7], [9, 14]]);
217
+ }, _callee3, this, [[4, 9], [13, 18]]);
180
218
  }));
181
219
  function close() {
182
220
  return _close.apply(this, arguments);
@@ -195,80 +233,154 @@ var RabbitMQAsync = /*#__PURE__*/function () {
195
233
  }, {
196
234
  key: "send",
197
235
  value: function () {
198
- var _send = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
236
+ var _send = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(queueName) {
199
237
  var msg,
200
238
  limit,
239
+ retryCount,
201
240
  prefixQueueName,
202
- channel,
203
241
  queueInfo,
204
- _args5 = arguments;
205
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
206
- while (1) switch (_context5.prev = _context5.next) {
242
+ _args4 = arguments;
243
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
244
+ while (1) switch (_context4.prev = _context4.next) {
207
245
  case 0:
208
- msg = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
209
- limit = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : 0;
210
- _context5.prev = 2;
246
+ msg = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
247
+ limit = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 0;
248
+ retryCount = _args4.length > 3 && _args4[3] !== undefined ? _args4[3] : 0;
249
+ if (!(retryCount >= 10)) {
250
+ _context4.next = 6;
251
+ break;
252
+ }
253
+ this._alert('error', "Failed to send to queue ".concat(queueName, " after 10 retries"));
254
+ return _context4.abrupt("return", false);
255
+ case 6:
256
+ _context4.prev = 6;
211
257
  prefixQueueName = this.parsePrefix(queueName);
212
- if (!this.connected) {
213
- _context5.next = 12;
258
+ if (!(!this.connected || !this.channel)) {
259
+ _context4.next = 10;
214
260
  break;
215
261
  }
216
- channel = this.channel;
217
- _context5.next = 8;
262
+ throw new Error('Not connected');
263
+ case 10:
264
+ _context4.next = 12;
218
265
  return this.channel.assertQueue(prefixQueueName, {
219
266
  durable: true
220
267
  });
221
- case 8:
222
- queueInfo = _context5.sent;
268
+ case 12:
269
+ queueInfo = _context4.sent;
223
270
  if (!(limit === 0 || queueInfo.messageCount < limit)) {
224
- _context5.next = 12;
271
+ _context4.next = 16;
225
272
  break;
226
273
  }
227
- _context5.next = 12;
228
- return channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(_objectSpread({
274
+ _context4.next = 16;
275
+ return this.channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(_objectSpread({
229
276
  retry: 5
230
277
  }, msg))), {
231
278
  persistent: true
232
279
  });
233
- case 12:
234
- return _context5.abrupt("return", true);
235
- case 15:
236
- _context5.prev = 15;
237
- _context5.t0 = _context5["catch"](2);
238
- _context5.next = 19;
239
- return (0, _util.timeout)(2000);
280
+ case 16:
281
+ return _context4.abrupt("return", true);
240
282
  case 19:
241
- return _context5.abrupt("return", this.send(queueName, msg));
242
- case 20:
283
+ _context4.prev = 19;
284
+ _context4.t0 = _context4["catch"](6);
285
+ _context4.next = 23;
286
+ return (0, _util.timeout)(2000);
287
+ case 23:
288
+ return _context4.abrupt("return", this.send(queueName, msg, limit, retryCount + 1));
289
+ case 24:
243
290
  case "end":
244
- return _context5.stop();
291
+ return _context4.stop();
245
292
  }
246
- }, _callee5, this, [[2, 15]]);
293
+ }, _callee4, this, [[6, 19]]);
247
294
  }));
248
295
  function send(_x) {
249
296
  return _send.apply(this, arguments);
250
297
  }
251
298
  return send;
252
299
  }()
300
+ }, {
301
+ key: "pub",
302
+ value: function () {
303
+ var _pub = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(exchangeName) {
304
+ var routingKey,
305
+ msg,
306
+ type,
307
+ retryCount,
308
+ prefixExchangeName,
309
+ _args5 = arguments;
310
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
311
+ while (1) switch (_context5.prev = _context5.next) {
312
+ case 0:
313
+ routingKey = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : '';
314
+ msg = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : {};
315
+ type = _args5.length > 3 && _args5[3] !== undefined ? _args5[3] : 'fanout';
316
+ retryCount = _args5.length > 4 && _args5[4] !== undefined ? _args5[4] : 0;
317
+ if (!(retryCount >= 10)) {
318
+ _context5.next = 7;
319
+ break;
320
+ }
321
+ this._alert('error', "Failed to publish to exchange ".concat(exchangeName, " after 10 retries"));
322
+ return _context5.abrupt("return", false);
323
+ case 7:
324
+ _context5.prev = 7;
325
+ prefixExchangeName = this.parsePrefix(exchangeName);
326
+ if (!(!this.connected || !this.channel)) {
327
+ _context5.next = 11;
328
+ break;
329
+ }
330
+ throw new Error('Not connected');
331
+ case 11:
332
+ _context5.next = 13;
333
+ return this.channel.assertExchange(prefixExchangeName, type, {
334
+ durable: true
335
+ });
336
+ case 13:
337
+ _context5.next = 15;
338
+ return this.channel.publish(prefixExchangeName, routingKey, Buffer.from(JSON.stringify(_objectSpread({
339
+ retry: 5
340
+ }, msg))), {
341
+ persistent: true
342
+ });
343
+ case 15:
344
+ return _context5.abrupt("return", true);
345
+ case 18:
346
+ _context5.prev = 18;
347
+ _context5.t0 = _context5["catch"](7);
348
+ _context5.next = 22;
349
+ return (0, _util.timeout)(2000);
350
+ case 22:
351
+ return _context5.abrupt("return", this.pub(exchangeName, routingKey, msg, type, retryCount + 1));
352
+ case 23:
353
+ case "end":
354
+ return _context5.stop();
355
+ }
356
+ }, _callee5, this, [[7, 18]]);
357
+ }));
358
+ function pub(_x2) {
359
+ return _pub.apply(this, arguments);
360
+ }
361
+ return pub;
362
+ }()
253
363
  }, {
254
364
  key: "getInfo",
255
365
  value: function () {
256
366
  var _getInfo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(queueName) {
257
- var prefixQueueName, channel;
367
+ var options,
368
+ prefixQueueName,
369
+ _args6 = arguments;
258
370
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
259
371
  while (1) switch (_context6.prev = _context6.next) {
260
372
  case 0:
261
- _context6.prev = 0;
373
+ options = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : {};
374
+ _context6.prev = 1;
262
375
  prefixQueueName = this.parsePrefix(queueName);
263
- if (!this.connected) {
376
+ if (!(this.connected && this.channel)) {
264
377
  _context6.next = 7;
265
378
  break;
266
379
  }
267
- channel = this.channel;
268
380
  _context6.next = 6;
269
- return channel.assertQueue(prefixQueueName, {
381
+ return this.channel.assertQueue(prefixQueueName, _objectSpread({
270
382
  durable: true
271
- });
383
+ }, options));
272
384
  case 6:
273
385
  return _context6.abrupt("return", _context6.sent);
274
386
  case 7:
@@ -276,25 +388,29 @@ var RabbitMQAsync = /*#__PURE__*/function () {
276
388
  break;
277
389
  case 9:
278
390
  _context6.prev = 9;
279
- _context6.t0 = _context6["catch"](0);
391
+ _context6.t0 = _context6["catch"](1);
280
392
  case 11:
281
- return _context6.abrupt("return", this.getInfo(queueName));
282
- case 12:
393
+ _context6.next = 13;
394
+ return (0, _util.timeout)(2000);
395
+ case 13:
396
+ return _context6.abrupt("return", this.getInfo(queueName, options));
397
+ case 14:
283
398
  case "end":
284
399
  return _context6.stop();
285
400
  }
286
- }, _callee6, this, [[0, 9]]);
401
+ }, _callee6, this, [[1, 9]]);
287
402
  }));
288
- function getInfo(_x2) {
403
+ function getInfo(_x3) {
289
404
  return _getInfo.apply(this, arguments);
290
405
  }
291
406
  return getInfo;
292
- }() // eslint-disable-next-line consistent-return
407
+ }()
293
408
  }, {
294
409
  key: "consume",
295
410
  value: function () {
296
411
  var _consume = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, asyncFunction) {
297
412
  var callbackError,
413
+ options,
298
414
  messageQueue,
299
415
  prefixQueueName,
300
416
  channel,
@@ -303,156 +419,161 @@ var RabbitMQAsync = /*#__PURE__*/function () {
303
419
  while (1) switch (_context8.prev = _context8.next) {
304
420
  case 0:
305
421
  callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
422
+ options = _args8.length > 3 && _args8[3] !== undefined ? _args8[3] : {};
306
423
  messageQueue = this;
307
- _context8.prev = 2;
308
- if (!this.connected) {
309
- _context8.next = 10;
424
+ _context8.prev = 3;
425
+ if (!(!this.connected || !this.channel)) {
426
+ _context8.next = 6;
310
427
  break;
311
428
  }
312
- prefixQueueName = this.parsePrefix(queueName), channel = messageQueue.channel;
313
- if (!channel) {
314
- _context8.next = 10;
315
- break;
316
- }
317
- _context8.next = 8;
318
- return channel.assertQueue(prefixQueueName, {
319
- durable: true
320
- });
321
- case 8:
429
+ return _context8.abrupt("return");
430
+ case 6:
431
+ prefixQueueName = this.parsePrefix(queueName);
432
+ channel = messageQueue.channel;
322
433
  _context8.next = 10;
323
- return new Promise(function (resolve, reject) {
324
- channel.consume(prefixQueueName, /*#__PURE__*/function () {
325
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
326
- var data, errString;
327
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
328
- while (1) switch (_context7.prev = _context7.next) {
329
- case 0:
330
- if (!msg) {
331
- _context7.next = 48;
332
- break;
333
- }
334
- data = JSON.parse(msg.content.toString());
335
- if (!data) {
336
- _context7.next = 47;
337
- break;
338
- }
339
- _context7.prev = 3;
340
- if (!msg.fields.redelivered) {
341
- _context7.next = 18;
342
- break;
343
- }
344
- data.retry -= 1;
345
- if (!(data.retry > 0)) {
346
- _context7.next = 11;
347
- break;
348
- }
349
- _context7.next = 9;
350
- return messageQueue.send(queueName, data);
351
- case 9:
352
- _context7.next = 14;
434
+ return channel.assertQueue(prefixQueueName, _objectSpread({
435
+ durable: true
436
+ }, options));
437
+ case 10:
438
+ _context8.next = 12;
439
+ return channel.consume(prefixQueueName, /*#__PURE__*/function () {
440
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
441
+ var data, errString;
442
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
443
+ while (1) switch (_context7.prev = _context7.next) {
444
+ case 0:
445
+ if (msg) {
446
+ _context7.next = 2;
353
447
  break;
354
- case 11:
355
- if (data.notCheck) {
356
- _context7.next = 14;
357
- break;
358
- }
359
- _context7.next = 14;
360
- return messageQueue.send(messageQueue.retryKey, {
361
- queueName: queueName,
362
- data: data,
363
- retry_time: new Date(),
364
- ip: messageQueue.serverIp
365
- });
366
- case 14:
367
- _context7.next = 16;
368
- return channel.ack(msg);
369
- case 16:
370
- _context7.next = 40;
448
+ }
449
+ return _context7.abrupt("return");
450
+ case 2:
451
+ _context7.prev = 2;
452
+ data = JSON.parse(msg.content.toString());
453
+ if (!data) {
454
+ _context7.next = 48;
371
455
  break;
372
- case 18:
373
- _context7.prev = 18;
374
- _context7.next = 21;
375
- return asyncFunction(data);
376
- case 21:
377
- _context7.next = 23;
378
- return channel.ack(msg);
379
- case 23:
380
- _context7.next = 40;
456
+ }
457
+ _context7.prev = 5;
458
+ if (!msg.fields.redelivered) {
459
+ _context7.next = 20;
381
460
  break;
382
- case 25:
383
- _context7.prev = 25;
384
- _context7.t0 = _context7["catch"](18);
385
- errString = _context7.t0.toString();
386
- if (!_lodash["default"].some(['Channel closed', 'Channel closing'], function (el) {
387
- return _lodash["default"].includes(errString, el);
388
- })) {
389
- _context7.next = 32;
390
- break;
391
- }
392
- reject(_context7.t0);
393
- _context7.next = 40;
461
+ }
462
+ data.retry = (data.retry || 0) - 1;
463
+ if (!(data.retry > 0)) {
464
+ _context7.next = 13;
394
465
  break;
395
- case 32:
396
- if (callbackError && data.retry === 1) {
397
- callbackError(prefixQueueName, errString);
398
- }
399
- if (!(data.retry <= 0)) {
400
- _context7.next = 38;
401
- break;
402
- }
403
- _context7.next = 36;
404
- return channel.ack(msg);
405
- case 36:
406
- _context7.next = 40;
466
+ }
467
+ _context7.next = 11;
468
+ return messageQueue.send(queueName, data);
469
+ case 11:
470
+ _context7.next = 16;
471
+ break;
472
+ case 13:
473
+ if (!(!data.notCheck && messageQueue.retryKey)) {
474
+ _context7.next = 16;
407
475
  break;
408
- case 38:
409
- _context7.next = 40;
410
- return channel.nack(msg);
411
- case 40:
412
- _context7.next = 45;
476
+ }
477
+ _context7.next = 16;
478
+ return messageQueue.send(messageQueue.retryKey, {
479
+ queueName: queueName,
480
+ data: data,
481
+ retry_time: new Date(),
482
+ ip: messageQueue.serverIp
483
+ });
484
+ case 16:
485
+ _context7.next = 18;
486
+ return channel.ack(msg);
487
+ case 18:
488
+ _context7.next = 41;
489
+ break;
490
+ case 20:
491
+ _context7.prev = 20;
492
+ _context7.next = 23;
493
+ return asyncFunction(data);
494
+ case 23:
495
+ _context7.next = 25;
496
+ return channel.ack(msg);
497
+ case 25:
498
+ _context7.next = 41;
499
+ break;
500
+ case 27:
501
+ _context7.prev = 27;
502
+ _context7.t0 = _context7["catch"](20);
503
+ errString = _context7.t0.toString();
504
+ if (!_lodash["default"].some(['Channel closed', 'Channel closing'], function (el) {
505
+ return _lodash["default"].includes(errString, el);
506
+ })) {
507
+ _context7.next = 33;
413
508
  break;
414
- case 42:
415
- _context7.prev = 42;
416
- _context7.t1 = _context7["catch"](3);
417
- reject(_context7.t1);
418
- case 45:
419
- _context7.next = 48;
509
+ }
510
+ _context7.next = 41;
511
+ break;
512
+ case 33:
513
+ if (callbackError && data.retry === 1) {
514
+ callbackError(prefixQueueName, errString);
515
+ }
516
+ if (!(data.retry <= 0)) {
517
+ _context7.next = 39;
420
518
  break;
421
- case 47:
422
- channel.ack(msg);
423
- case 48:
424
- case "end":
425
- return _context7.stop();
426
- }
427
- }, _callee7, null, [[3, 42], [18, 25]]);
428
- }));
429
- return function (_x5) {
430
- return _ref2.apply(this, arguments);
431
- };
432
- }());
433
- });
434
- case 10:
519
+ }
520
+ _context7.next = 37;
521
+ return channel.ack(msg);
522
+ case 37:
523
+ _context7.next = 41;
524
+ break;
525
+ case 39:
526
+ _context7.next = 41;
527
+ return channel.nack(msg);
528
+ case 41:
529
+ _context7.next = 46;
530
+ break;
531
+ case 43:
532
+ _context7.prev = 43;
533
+ _context7.t1 = _context7["catch"](5);
534
+ messageQueue._alert('error', _context7.t1);
535
+ case 46:
536
+ _context7.next = 49;
537
+ break;
538
+ case 48:
539
+ channel.ack(msg);
540
+ case 49:
541
+ _context7.next = 55;
542
+ break;
543
+ case 51:
544
+ _context7.prev = 51;
545
+ _context7.t2 = _context7["catch"](2);
546
+ messageQueue._alert('error', _context7.t2);
547
+ channel.ack(msg);
548
+ case 55:
549
+ case "end":
550
+ return _context7.stop();
551
+ }
552
+ }, _callee7, null, [[2, 51], [5, 43], [20, 27]]);
553
+ }));
554
+ return function (_x6) {
555
+ return _ref.apply(this, arguments);
556
+ };
557
+ }());
558
+ case 12:
435
559
  _context8.next = 18;
436
560
  break;
437
- case 12:
438
- _context8.prev = 12;
439
- _context8.t0 = _context8["catch"](2);
440
- _context8.next = 16;
441
- return (0, _util.timeout)(5000);
442
- case 16:
443
- this._alert('err3', _context8.t0);
561
+ case 14:
562
+ _context8.prev = 14;
563
+ _context8.t0 = _context8["catch"](3);
564
+ this._alert('error', _context8.t0);
444
565
  if (_lodash["default"].some(['Channel closed'], function (el) {
445
566
  return _lodash["default"].includes(_context8.t0.toString(), el);
446
567
  })) {
447
- messageQueue.close();
568
+ messageQueue.handleDisconnect();
448
569
  }
449
570
  case 18:
450
571
  case "end":
451
572
  return _context8.stop();
452
573
  }
453
- }, _callee8, this, [[2, 12]]);
574
+ }, _callee8, this, [[3, 14]]);
454
575
  }));
455
- function consume(_x3, _x4) {
576
+ function consume(_x4, _x5) {
456
577
  return _consume.apply(this, arguments);
457
578
  }
458
579
  return consume;
@@ -461,12 +582,10 @@ var RabbitMQAsync = /*#__PURE__*/function () {
461
582
  key: "startListConsume",
462
583
  value: function startListConsume() {
463
584
  var _this3 = this;
464
- if (this.connected) {
465
- // eslint-disable-next-line array-callback-return
466
- this.listConsume.map(function (c) {
585
+ if (this.connected && this.channel) {
586
+ this.listConsume.forEach(function (c) {
467
587
  if (!c.listened) {
468
- _this3.consume(c.queueName, c.asyncFunction, c.callbackError);
469
- // eslint-disable-next-line no-param-reassign
588
+ _this3.consume(c.queueName, c.asyncFunction, c.callbackError, c.options);
470
589
  c.listened = true;
471
590
  }
472
591
  });
@@ -477,11 +596,13 @@ var RabbitMQAsync = /*#__PURE__*/function () {
477
596
  value: function () {
478
597
  var _receiving = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(queueName, asyncFunction) {
479
598
  var callbackError,
599
+ options,
480
600
  _args9 = arguments;
481
601
  return _regeneratorRuntime().wrap(function _callee9$(_context9) {
482
602
  while (1) switch (_context9.prev = _context9.next) {
483
603
  case 0:
484
604
  callbackError = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : null;
605
+ options = _args9.length > 3 && _args9[3] !== undefined ? _args9[3] : {};
485
606
  if (!_lodash["default"].find(this.listConsume, function (c) {
486
607
  return c.queueName === queueName;
487
608
  })) {
@@ -489,20 +610,275 @@ var RabbitMQAsync = /*#__PURE__*/function () {
489
610
  queueName: queueName,
490
611
  asyncFunction: asyncFunction,
491
612
  callbackError: callbackError,
613
+ options: options,
492
614
  listened: false
493
615
  });
616
+ if (this.connected) {
617
+ this.startListConsume();
618
+ }
494
619
  }
495
- case 2:
620
+ case 3:
496
621
  case "end":
497
622
  return _context9.stop();
498
623
  }
499
624
  }, _callee9, this);
500
625
  }));
501
- function receiving(_x6, _x7) {
626
+ function receiving(_x7, _x8) {
502
627
  return _receiving.apply(this, arguments);
503
628
  }
504
629
  return receiving;
505
630
  }()
631
+ }, {
632
+ key: "subscribe",
633
+ value: function () {
634
+ var _subscribe = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(exchangeName, routingKey, queueName, asyncFunction) {
635
+ var callbackError,
636
+ type,
637
+ messageQueue,
638
+ prefixExchangeName,
639
+ prefixQueueName,
640
+ channel,
641
+ q,
642
+ _args1 = arguments;
643
+ return _regeneratorRuntime().wrap(function _callee1$(_context1) {
644
+ while (1) switch (_context1.prev = _context1.next) {
645
+ case 0:
646
+ callbackError = _args1.length > 4 && _args1[4] !== undefined ? _args1[4] : null;
647
+ type = _args1.length > 5 && _args1[5] !== undefined ? _args1[5] : 'fanout';
648
+ messageQueue = this;
649
+ _context1.prev = 3;
650
+ if (!(!this.connected || !this.channel)) {
651
+ _context1.next = 6;
652
+ break;
653
+ }
654
+ return _context1.abrupt("return");
655
+ case 6:
656
+ prefixExchangeName = this.parsePrefix(exchangeName);
657
+ prefixQueueName = queueName ? this.parsePrefix(queueName) : '';
658
+ channel = messageQueue.channel;
659
+ _context1.next = 11;
660
+ return channel.assertExchange(prefixExchangeName, type, {
661
+ durable: true
662
+ });
663
+ case 11:
664
+ _context1.next = 13;
665
+ return channel.assertQueue(prefixQueueName, {
666
+ exclusive: !queueName,
667
+ durable: !!queueName
668
+ });
669
+ case 13:
670
+ q = _context1.sent;
671
+ _context1.next = 16;
672
+ return channel.bindQueue(q.queue, prefixExchangeName, routingKey);
673
+ case 16:
674
+ _context1.next = 18;
675
+ return channel.consume(q.queue, /*#__PURE__*/function () {
676
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(msg) {
677
+ var data, errString;
678
+ return _regeneratorRuntime().wrap(function _callee0$(_context0) {
679
+ while (1) switch (_context0.prev = _context0.next) {
680
+ case 0:
681
+ if (msg) {
682
+ _context0.next = 2;
683
+ break;
684
+ }
685
+ return _context0.abrupt("return");
686
+ case 2:
687
+ _context0.prev = 2;
688
+ data = JSON.parse(msg.content.toString());
689
+ if (!data) {
690
+ _context0.next = 53;
691
+ break;
692
+ }
693
+ _context0.prev = 5;
694
+ if (!(msg.fields.redelivered && queueName)) {
695
+ _context0.next = 20;
696
+ break;
697
+ }
698
+ // named queue error retry flow
699
+ data.retry = (data.retry || 0) - 1;
700
+ if (!(data.retry > 0)) {
701
+ _context0.next = 13;
702
+ break;
703
+ }
704
+ _context0.next = 11;
705
+ return messageQueue.send(queueName, data);
706
+ case 11:
707
+ _context0.next = 16;
708
+ break;
709
+ case 13:
710
+ if (!(!data.notCheck && messageQueue.retryKey)) {
711
+ _context0.next = 16;
712
+ break;
713
+ }
714
+ _context0.next = 16;
715
+ return messageQueue.send(messageQueue.retryKey, {
716
+ queueName: q.queue,
717
+ data: data,
718
+ retry_time: new Date(),
719
+ ip: messageQueue.serverIp
720
+ });
721
+ case 16:
722
+ _context0.next = 18;
723
+ return channel.ack(msg);
724
+ case 18:
725
+ _context0.next = 46;
726
+ break;
727
+ case 20:
728
+ _context0.prev = 20;
729
+ _context0.next = 23;
730
+ return asyncFunction(data);
731
+ case 23:
732
+ _context0.next = 25;
733
+ return channel.ack(msg);
734
+ case 25:
735
+ _context0.next = 46;
736
+ break;
737
+ case 27:
738
+ _context0.prev = 27;
739
+ _context0.t0 = _context0["catch"](20);
740
+ errString = _context0.t0.toString();
741
+ if (!_lodash["default"].some(['Channel closed', 'Channel closing'], function (el) {
742
+ return _lodash["default"].includes(errString, el);
743
+ })) {
744
+ _context0.next = 33;
745
+ break;
746
+ }
747
+ _context0.next = 46;
748
+ break;
749
+ case 33:
750
+ if (callbackError && data.retry === 1) {
751
+ callbackError(q.queue, errString);
752
+ }
753
+ if (!queueName) {
754
+ _context0.next = 44;
755
+ break;
756
+ }
757
+ if (!(data.retry <= 0)) {
758
+ _context0.next = 40;
759
+ break;
760
+ }
761
+ _context0.next = 38;
762
+ return channel.ack(msg);
763
+ case 38:
764
+ _context0.next = 42;
765
+ break;
766
+ case 40:
767
+ _context0.next = 42;
768
+ return channel.nack(msg);
769
+ case 42:
770
+ _context0.next = 46;
771
+ break;
772
+ case 44:
773
+ _context0.next = 46;
774
+ return channel.nack(msg, false, false);
775
+ case 46:
776
+ _context0.next = 51;
777
+ break;
778
+ case 48:
779
+ _context0.prev = 48;
780
+ _context0.t1 = _context0["catch"](5);
781
+ messageQueue._alert('error', _context0.t1);
782
+ case 51:
783
+ _context0.next = 54;
784
+ break;
785
+ case 53:
786
+ channel.ack(msg);
787
+ case 54:
788
+ _context0.next = 60;
789
+ break;
790
+ case 56:
791
+ _context0.prev = 56;
792
+ _context0.t2 = _context0["catch"](2);
793
+ messageQueue._alert('error', _context0.t2);
794
+ channel.ack(msg);
795
+ case 60:
796
+ case "end":
797
+ return _context0.stop();
798
+ }
799
+ }, _callee0, null, [[2, 56], [5, 48], [20, 27]]);
800
+ }));
801
+ return function (_x11) {
802
+ return _ref2.apply(this, arguments);
803
+ };
804
+ }());
805
+ case 18:
806
+ _context1.next = 24;
807
+ break;
808
+ case 20:
809
+ _context1.prev = 20;
810
+ _context1.t0 = _context1["catch"](3);
811
+ this._alert('error', _context1.t0);
812
+ if (_lodash["default"].some(['Channel closed'], function (el) {
813
+ return _lodash["default"].includes(_context1.t0.toString(), el);
814
+ })) {
815
+ messageQueue.handleDisconnect();
816
+ }
817
+ case 24:
818
+ case "end":
819
+ return _context1.stop();
820
+ }
821
+ }, _callee1, this, [[3, 20]]);
822
+ }));
823
+ function subscribe(_x9, _x0, _x1, _x10) {
824
+ return _subscribe.apply(this, arguments);
825
+ }
826
+ return subscribe;
827
+ }()
828
+ }, {
829
+ key: "startListSub",
830
+ value: function startListSub() {
831
+ var _this4 = this;
832
+ if (this.connected && this.channel) {
833
+ this.listSub.forEach(function (s) {
834
+ if (!s.listened) {
835
+ _this4.subscribe(s.exchangeName, s.routingKey, s.queueName, s.asyncFunction, s.callbackError, s.type);
836
+ s.listened = true;
837
+ }
838
+ });
839
+ }
840
+ }
841
+ }, {
842
+ key: "sub",
843
+ value: function () {
844
+ var _sub = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(exchangeName, routingKey, queueName, asyncFunction) {
845
+ var callbackError,
846
+ type,
847
+ existing,
848
+ _args10 = arguments;
849
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
850
+ while (1) switch (_context10.prev = _context10.next) {
851
+ case 0:
852
+ callbackError = _args10.length > 4 && _args10[4] !== undefined ? _args10[4] : null;
853
+ type = _args10.length > 5 && _args10[5] !== undefined ? _args10[5] : 'fanout';
854
+ existing = queueName ? _lodash["default"].find(this.listSub, function (s) {
855
+ return s.exchangeName === exchangeName && s.routingKey === routingKey && s.queueName === queueName;
856
+ }) : false;
857
+ if (!existing) {
858
+ this.listSub.push({
859
+ exchangeName: exchangeName,
860
+ routingKey: routingKey,
861
+ queueName: queueName,
862
+ asyncFunction: asyncFunction,
863
+ callbackError: callbackError,
864
+ type: type,
865
+ listened: false
866
+ });
867
+ if (this.connected) {
868
+ this.startListSub();
869
+ }
870
+ }
871
+ case 4:
872
+ case "end":
873
+ return _context10.stop();
874
+ }
875
+ }, _callee10, this);
876
+ }));
877
+ function sub(_x12, _x13, _x14, _x15) {
878
+ return _sub.apply(this, arguments);
879
+ }
880
+ return sub;
881
+ }()
506
882
  }, {
507
883
  key: "setAlertCallback",
508
884
  value: function setAlertCallback(callback) {
@@ -514,7 +890,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
514
890
  if (typeof this.alertCallback === 'function') {
515
891
  this.alertCallback(status, msg);
516
892
  } else {
517
- console.error(status, msg);
893
+ console.error("[RabbitMQAsync][".concat(status, "]"), msg);
518
894
  }
519
895
  }
520
896
  }]);