@azteam/rabbitmq-async 1.0.231 → 1.0.233

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,101 +233,175 @@ 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 prefixQueueName;
258
368
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
259
369
  while (1) switch (_context6.prev = _context6.next) {
260
370
  case 0:
261
371
  _context6.prev = 0;
262
372
  prefixQueueName = this.parsePrefix(queueName);
263
- if (!this.connected) {
264
- _context6.next = 7;
373
+ if (!(this.connected && this.channel)) {
374
+ _context6.next = 6;
265
375
  break;
266
376
  }
267
- channel = this.channel;
268
- _context6.next = 6;
269
- return channel.assertQueue(prefixQueueName, {
377
+ _context6.next = 5;
378
+ return this.channel.assertQueue(prefixQueueName, {
270
379
  durable: true
271
380
  });
272
- case 6:
381
+ case 5:
273
382
  return _context6.abrupt("return", _context6.sent);
274
- case 7:
275
- _context6.next = 11;
383
+ case 6:
384
+ _context6.next = 10;
276
385
  break;
277
- case 9:
278
- _context6.prev = 9;
386
+ case 8:
387
+ _context6.prev = 8;
279
388
  _context6.t0 = _context6["catch"](0);
280
- case 11:
281
- return _context6.abrupt("return", this.getInfo(queueName));
389
+ case 10:
390
+ _context6.next = 12;
391
+ return (0, _util.timeout)(2000);
282
392
  case 12:
393
+ return _context6.abrupt("return", this.getInfo(queueName));
394
+ case 13:
283
395
  case "end":
284
396
  return _context6.stop();
285
397
  }
286
- }, _callee6, this, [[0, 9]]);
398
+ }, _callee6, this, [[0, 8]]);
287
399
  }));
288
- function getInfo(_x2) {
400
+ function getInfo(_x3) {
289
401
  return _getInfo.apply(this, arguments);
290
402
  }
291
403
  return getInfo;
292
- }() // eslint-disable-next-line consistent-return
404
+ }()
293
405
  }, {
294
406
  key: "consume",
295
407
  value: function () {
@@ -305,154 +417,158 @@ var RabbitMQAsync = /*#__PURE__*/function () {
305
417
  callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
306
418
  messageQueue = this;
307
419
  _context8.prev = 2;
308
- if (!this.connected) {
309
- _context8.next = 10;
420
+ if (!(!this.connected || !this.channel)) {
421
+ _context8.next = 5;
310
422
  break;
311
423
  }
312
- prefixQueueName = this.parsePrefix(queueName), channel = messageQueue.channel;
313
- if (!channel) {
314
- _context8.next = 10;
315
- break;
316
- }
317
- _context8.next = 8;
424
+ return _context8.abrupt("return");
425
+ case 5:
426
+ prefixQueueName = this.parsePrefix(queueName);
427
+ channel = messageQueue.channel;
428
+ _context8.next = 9;
318
429
  return channel.assertQueue(prefixQueueName, {
319
430
  durable: true
320
431
  });
321
- case 8:
322
- _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;
432
+ case 9:
433
+ _context8.next = 11;
434
+ return channel.consume(prefixQueueName, /*#__PURE__*/function () {
435
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
436
+ var data, errString;
437
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
438
+ while (1) switch (_context7.prev = _context7.next) {
439
+ case 0:
440
+ if (msg) {
441
+ _context7.next = 2;
353
442
  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;
443
+ }
444
+ return _context7.abrupt("return");
445
+ case 2:
446
+ _context7.prev = 2;
447
+ data = JSON.parse(msg.content.toString());
448
+ if (!data) {
449
+ _context7.next = 48;
371
450
  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;
451
+ }
452
+ _context7.prev = 5;
453
+ if (!msg.fields.redelivered) {
454
+ _context7.next = 20;
381
455
  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;
456
+ }
457
+ data.retry = (data.retry || 0) - 1;
458
+ if (!(data.retry > 0)) {
459
+ _context7.next = 13;
394
460
  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;
461
+ }
462
+ _context7.next = 11;
463
+ return messageQueue.send(queueName, data);
464
+ case 11:
465
+ _context7.next = 16;
466
+ break;
467
+ case 13:
468
+ if (!(!data.notCheck && messageQueue.retryKey)) {
469
+ _context7.next = 16;
407
470
  break;
408
- case 38:
409
- _context7.next = 40;
410
- return channel.nack(msg);
411
- case 40:
412
- _context7.next = 45;
471
+ }
472
+ _context7.next = 16;
473
+ return messageQueue.send(messageQueue.retryKey, {
474
+ queueName: queueName,
475
+ data: data,
476
+ retry_time: new Date(),
477
+ ip: messageQueue.serverIp
478
+ });
479
+ case 16:
480
+ _context7.next = 18;
481
+ return channel.ack(msg);
482
+ case 18:
483
+ _context7.next = 41;
484
+ break;
485
+ case 20:
486
+ _context7.prev = 20;
487
+ _context7.next = 23;
488
+ return asyncFunction(data);
489
+ case 23:
490
+ _context7.next = 25;
491
+ return channel.ack(msg);
492
+ case 25:
493
+ _context7.next = 41;
494
+ break;
495
+ case 27:
496
+ _context7.prev = 27;
497
+ _context7.t0 = _context7["catch"](20);
498
+ errString = _context7.t0.toString();
499
+ if (!_lodash["default"].some(['Channel closed', 'Channel closing'], function (el) {
500
+ return _lodash["default"].includes(errString, el);
501
+ })) {
502
+ _context7.next = 33;
413
503
  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;
504
+ }
505
+ _context7.next = 41;
506
+ break;
507
+ case 33:
508
+ if (callbackError && data.retry === 1) {
509
+ callbackError(prefixQueueName, errString);
510
+ }
511
+ if (!(data.retry <= 0)) {
512
+ _context7.next = 39;
420
513
  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:
435
- _context8.next = 18;
514
+ }
515
+ _context7.next = 37;
516
+ return channel.ack(msg);
517
+ case 37:
518
+ _context7.next = 41;
519
+ break;
520
+ case 39:
521
+ _context7.next = 41;
522
+ return channel.nack(msg);
523
+ case 41:
524
+ _context7.next = 46;
525
+ break;
526
+ case 43:
527
+ _context7.prev = 43;
528
+ _context7.t1 = _context7["catch"](5);
529
+ messageQueue._alert('error', _context7.t1);
530
+ case 46:
531
+ _context7.next = 49;
532
+ break;
533
+ case 48:
534
+ channel.ack(msg);
535
+ case 49:
536
+ _context7.next = 55;
537
+ break;
538
+ case 51:
539
+ _context7.prev = 51;
540
+ _context7.t2 = _context7["catch"](2);
541
+ messageQueue._alert('error', _context7.t2);
542
+ channel.ack(msg);
543
+ case 55:
544
+ case "end":
545
+ return _context7.stop();
546
+ }
547
+ }, _callee7, null, [[2, 51], [5, 43], [20, 27]]);
548
+ }));
549
+ return function (_x6) {
550
+ return _ref.apply(this, arguments);
551
+ };
552
+ }());
553
+ case 11:
554
+ _context8.next = 17;
436
555
  break;
437
- case 12:
438
- _context8.prev = 12;
556
+ case 13:
557
+ _context8.prev = 13;
439
558
  _context8.t0 = _context8["catch"](2);
440
- _context8.next = 16;
441
- return (0, _util.timeout)(5000);
442
- case 16:
443
- this._alert('err3', _context8.t0);
559
+ this._alert('error', _context8.t0);
444
560
  if (_lodash["default"].some(['Channel closed'], function (el) {
445
561
  return _lodash["default"].includes(_context8.t0.toString(), el);
446
562
  })) {
447
- messageQueue.close();
563
+ messageQueue.handleDisconnect();
448
564
  }
449
- case 18:
565
+ case 17:
450
566
  case "end":
451
567
  return _context8.stop();
452
568
  }
453
- }, _callee8, this, [[2, 12]]);
569
+ }, _callee8, this, [[2, 13]]);
454
570
  }));
455
- function consume(_x3, _x4) {
571
+ function consume(_x4, _x5) {
456
572
  return _consume.apply(this, arguments);
457
573
  }
458
574
  return consume;
@@ -461,12 +577,10 @@ var RabbitMQAsync = /*#__PURE__*/function () {
461
577
  key: "startListConsume",
462
578
  value: function startListConsume() {
463
579
  var _this3 = this;
464
- if (this.connected) {
465
- // eslint-disable-next-line array-callback-return
466
- this.listConsume.map(function (c) {
580
+ if (this.connected && this.channel) {
581
+ this.listConsume.forEach(function (c) {
467
582
  if (!c.listened) {
468
583
  _this3.consume(c.queueName, c.asyncFunction, c.callbackError);
469
- // eslint-disable-next-line no-param-reassign
470
584
  c.listened = true;
471
585
  }
472
586
  });
@@ -491,6 +605,9 @@ var RabbitMQAsync = /*#__PURE__*/function () {
491
605
  callbackError: callbackError,
492
606
  listened: false
493
607
  });
608
+ if (this.connected) {
609
+ this.startListConsume();
610
+ }
494
611
  }
495
612
  case 2:
496
613
  case "end":
@@ -498,11 +615,262 @@ var RabbitMQAsync = /*#__PURE__*/function () {
498
615
  }
499
616
  }, _callee9, this);
500
617
  }));
501
- function receiving(_x6, _x7) {
618
+ function receiving(_x7, _x8) {
502
619
  return _receiving.apply(this, arguments);
503
620
  }
504
621
  return receiving;
505
622
  }()
623
+ }, {
624
+ key: "subscribe",
625
+ value: function () {
626
+ var _subscribe = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(exchangeName, routingKey, queueName, asyncFunction) {
627
+ var callbackError,
628
+ type,
629
+ messageQueue,
630
+ prefixExchangeName,
631
+ prefixQueueName,
632
+ channel,
633
+ q,
634
+ _args1 = arguments;
635
+ return _regeneratorRuntime().wrap(function _callee1$(_context1) {
636
+ while (1) switch (_context1.prev = _context1.next) {
637
+ case 0:
638
+ callbackError = _args1.length > 4 && _args1[4] !== undefined ? _args1[4] : null;
639
+ type = _args1.length > 5 && _args1[5] !== undefined ? _args1[5] : 'fanout';
640
+ messageQueue = this;
641
+ _context1.prev = 3;
642
+ if (!(!this.connected || !this.channel)) {
643
+ _context1.next = 6;
644
+ break;
645
+ }
646
+ return _context1.abrupt("return");
647
+ case 6:
648
+ prefixExchangeName = this.parsePrefix(exchangeName);
649
+ prefixQueueName = queueName ? this.parsePrefix(queueName) : '';
650
+ channel = messageQueue.channel;
651
+ _context1.next = 11;
652
+ return channel.assertExchange(prefixExchangeName, type, {
653
+ durable: true
654
+ });
655
+ case 11:
656
+ _context1.next = 13;
657
+ return channel.assertQueue(prefixQueueName, {
658
+ exclusive: !queueName,
659
+ durable: !!queueName
660
+ });
661
+ case 13:
662
+ q = _context1.sent;
663
+ _context1.next = 16;
664
+ return channel.bindQueue(q.queue, prefixExchangeName, routingKey);
665
+ case 16:
666
+ _context1.next = 18;
667
+ return channel.consume(q.queue, /*#__PURE__*/function () {
668
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(msg) {
669
+ var data, errString;
670
+ return _regeneratorRuntime().wrap(function _callee0$(_context0) {
671
+ while (1) switch (_context0.prev = _context0.next) {
672
+ case 0:
673
+ if (msg) {
674
+ _context0.next = 2;
675
+ break;
676
+ }
677
+ return _context0.abrupt("return");
678
+ case 2:
679
+ _context0.prev = 2;
680
+ data = JSON.parse(msg.content.toString());
681
+ if (!data) {
682
+ _context0.next = 53;
683
+ break;
684
+ }
685
+ _context0.prev = 5;
686
+ if (!(msg.fields.redelivered && queueName)) {
687
+ _context0.next = 20;
688
+ break;
689
+ }
690
+ // named queue error retry flow
691
+ data.retry = (data.retry || 0) - 1;
692
+ if (!(data.retry > 0)) {
693
+ _context0.next = 13;
694
+ break;
695
+ }
696
+ _context0.next = 11;
697
+ return messageQueue.send(queueName, data);
698
+ case 11:
699
+ _context0.next = 16;
700
+ break;
701
+ case 13:
702
+ if (!(!data.notCheck && messageQueue.retryKey)) {
703
+ _context0.next = 16;
704
+ break;
705
+ }
706
+ _context0.next = 16;
707
+ return messageQueue.send(messageQueue.retryKey, {
708
+ queueName: q.queue,
709
+ data: data,
710
+ retry_time: new Date(),
711
+ ip: messageQueue.serverIp
712
+ });
713
+ case 16:
714
+ _context0.next = 18;
715
+ return channel.ack(msg);
716
+ case 18:
717
+ _context0.next = 46;
718
+ break;
719
+ case 20:
720
+ _context0.prev = 20;
721
+ _context0.next = 23;
722
+ return asyncFunction(data);
723
+ case 23:
724
+ _context0.next = 25;
725
+ return channel.ack(msg);
726
+ case 25:
727
+ _context0.next = 46;
728
+ break;
729
+ case 27:
730
+ _context0.prev = 27;
731
+ _context0.t0 = _context0["catch"](20);
732
+ errString = _context0.t0.toString();
733
+ if (!_lodash["default"].some(['Channel closed', 'Channel closing'], function (el) {
734
+ return _lodash["default"].includes(errString, el);
735
+ })) {
736
+ _context0.next = 33;
737
+ break;
738
+ }
739
+ _context0.next = 46;
740
+ break;
741
+ case 33:
742
+ if (callbackError && data.retry === 1) {
743
+ callbackError(q.queue, errString);
744
+ }
745
+ if (!queueName) {
746
+ _context0.next = 44;
747
+ break;
748
+ }
749
+ if (!(data.retry <= 0)) {
750
+ _context0.next = 40;
751
+ break;
752
+ }
753
+ _context0.next = 38;
754
+ return channel.ack(msg);
755
+ case 38:
756
+ _context0.next = 42;
757
+ break;
758
+ case 40:
759
+ _context0.next = 42;
760
+ return channel.nack(msg);
761
+ case 42:
762
+ _context0.next = 46;
763
+ break;
764
+ case 44:
765
+ _context0.next = 46;
766
+ return channel.nack(msg, false, false);
767
+ case 46:
768
+ _context0.next = 51;
769
+ break;
770
+ case 48:
771
+ _context0.prev = 48;
772
+ _context0.t1 = _context0["catch"](5);
773
+ messageQueue._alert('error', _context0.t1);
774
+ case 51:
775
+ _context0.next = 54;
776
+ break;
777
+ case 53:
778
+ channel.ack(msg);
779
+ case 54:
780
+ _context0.next = 60;
781
+ break;
782
+ case 56:
783
+ _context0.prev = 56;
784
+ _context0.t2 = _context0["catch"](2);
785
+ messageQueue._alert('error', _context0.t2);
786
+ channel.ack(msg);
787
+ case 60:
788
+ case "end":
789
+ return _context0.stop();
790
+ }
791
+ }, _callee0, null, [[2, 56], [5, 48], [20, 27]]);
792
+ }));
793
+ return function (_x11) {
794
+ return _ref2.apply(this, arguments);
795
+ };
796
+ }());
797
+ case 18:
798
+ _context1.next = 24;
799
+ break;
800
+ case 20:
801
+ _context1.prev = 20;
802
+ _context1.t0 = _context1["catch"](3);
803
+ this._alert('error', _context1.t0);
804
+ if (_lodash["default"].some(['Channel closed'], function (el) {
805
+ return _lodash["default"].includes(_context1.t0.toString(), el);
806
+ })) {
807
+ messageQueue.handleDisconnect();
808
+ }
809
+ case 24:
810
+ case "end":
811
+ return _context1.stop();
812
+ }
813
+ }, _callee1, this, [[3, 20]]);
814
+ }));
815
+ function subscribe(_x9, _x0, _x1, _x10) {
816
+ return _subscribe.apply(this, arguments);
817
+ }
818
+ return subscribe;
819
+ }()
820
+ }, {
821
+ key: "startListSub",
822
+ value: function startListSub() {
823
+ var _this4 = this;
824
+ if (this.connected && this.channel) {
825
+ this.listSub.forEach(function (s) {
826
+ if (!s.listened) {
827
+ _this4.subscribe(s.exchangeName, s.routingKey, s.queueName, s.asyncFunction, s.callbackError, s.type);
828
+ s.listened = true;
829
+ }
830
+ });
831
+ }
832
+ }
833
+ }, {
834
+ key: "sub",
835
+ value: function () {
836
+ var _sub = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(exchangeName, routingKey, queueName, asyncFunction) {
837
+ var callbackError,
838
+ type,
839
+ existing,
840
+ _args10 = arguments;
841
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
842
+ while (1) switch (_context10.prev = _context10.next) {
843
+ case 0:
844
+ callbackError = _args10.length > 4 && _args10[4] !== undefined ? _args10[4] : null;
845
+ type = _args10.length > 5 && _args10[5] !== undefined ? _args10[5] : 'fanout';
846
+ existing = queueName ? _lodash["default"].find(this.listSub, function (s) {
847
+ return s.exchangeName === exchangeName && s.routingKey === routingKey && s.queueName === queueName;
848
+ }) : false;
849
+ if (!existing) {
850
+ this.listSub.push({
851
+ exchangeName: exchangeName,
852
+ routingKey: routingKey,
853
+ queueName: queueName,
854
+ asyncFunction: asyncFunction,
855
+ callbackError: callbackError,
856
+ type: type,
857
+ listened: false
858
+ });
859
+ if (this.connected) {
860
+ this.startListSub();
861
+ }
862
+ }
863
+ case 4:
864
+ case "end":
865
+ return _context10.stop();
866
+ }
867
+ }, _callee10, this);
868
+ }));
869
+ function sub(_x12, _x13, _x14, _x15) {
870
+ return _sub.apply(this, arguments);
871
+ }
872
+ return sub;
873
+ }()
506
874
  }, {
507
875
  key: "setAlertCallback",
508
876
  value: function setAlertCallback(callback) {
@@ -514,7 +882,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
514
882
  if (typeof this.alertCallback === 'function') {
515
883
  this.alertCallback(status, msg);
516
884
  } else {
517
- console.error(status, msg);
885
+ console.error("[RabbitMQAsync][".concat(status, "]"), msg);
518
886
  }
519
887
  }
520
888
  }]);