@azteam/rabbitmq-async 1.0.161 → 1.0.163

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.
@@ -169,7 +169,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
169
169
  value: function () {
170
170
  var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(queueName) {
171
171
  var msg,
172
- force,
172
+ limit,
173
173
  prefixQueueName,
174
174
  channel,
175
175
  queueInfo,
@@ -179,7 +179,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
179
179
  switch (_context4.prev = _context4.next) {
180
180
  case 0:
181
181
  msg = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
182
- force = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : true;
182
+ limit = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : false;
183
183
  prefixQueueName = this.parsePrefix(queueName);
184
184
  if (!this.connected) {
185
185
  _context4.next = 33;
@@ -196,7 +196,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
196
196
  });
197
197
  case 10:
198
198
  queueInfo = _context4.sent;
199
- if (!(force || queueInfo.messageCount === 0)) {
199
+ if (!(!limit || queueInfo.messageCount < limit)) {
200
200
  _context4.next = 14;
201
201
  break;
202
202
  }
@@ -247,60 +247,86 @@ var RabbitMQAsync = /*#__PURE__*/function () {
247
247
  return send;
248
248
  }()
249
249
  }, {
250
- key: "getInfo",
250
+ key: "sendNoRetry",
251
251
  value: function () {
252
- var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
253
- var prefixQueueName, channel;
252
+ var _sendNoRetry = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
253
+ var msg,
254
+ limit,
255
+ _args5 = arguments;
254
256
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
255
257
  while (1) {
256
258
  switch (_context5.prev = _context5.next) {
259
+ case 0:
260
+ msg = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
261
+ limit = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : false;
262
+ this.send(queueName, msg, limit);
263
+ case 3:
264
+ case "end":
265
+ return _context5.stop();
266
+ }
267
+ }
268
+ }, _callee5, this);
269
+ }));
270
+ function sendNoRetry(_x2) {
271
+ return _sendNoRetry.apply(this, arguments);
272
+ }
273
+ return sendNoRetry;
274
+ }()
275
+ }, {
276
+ key: "getInfo",
277
+ value: function () {
278
+ var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(queueName) {
279
+ var prefixQueueName, channel;
280
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
281
+ while (1) {
282
+ switch (_context6.prev = _context6.next) {
257
283
  case 0:
258
284
  prefixQueueName = this.parsePrefix(queueName);
259
285
  if (!this.connected) {
260
- _context5.next = 25;
286
+ _context6.next = 25;
261
287
  break;
262
288
  }
263
- _context5.prev = 2;
264
- _context5.next = 5;
289
+ _context6.prev = 2;
290
+ _context6.next = 5;
265
291
  return this.client.createChannel();
266
292
  case 5:
267
- channel = _context5.sent;
268
- _context5.next = 8;
293
+ channel = _context6.sent;
294
+ _context6.next = 8;
269
295
  return channel.assertQueue(prefixQueueName, {
270
296
  durable: true
271
297
  });
272
298
  case 8:
273
- return _context5.abrupt("return", _context5.sent);
299
+ return _context6.abrupt("return", _context6.sent);
274
300
  case 11:
275
- _context5.prev = 11;
276
- _context5.t0 = _context5["catch"](2);
277
- return _context5.abrupt("return", this.getInfo(queueName));
301
+ _context6.prev = 11;
302
+ _context6.t0 = _context6["catch"](2);
303
+ return _context6.abrupt("return", this.getInfo(queueName));
278
304
  case 14:
279
- _context5.prev = 14;
280
- _context5.prev = 15;
281
- _context5.next = 18;
305
+ _context6.prev = 14;
306
+ _context6.prev = 15;
307
+ _context6.next = 18;
282
308
  return channel.close();
283
309
  case 18:
284
- _context5.next = 22;
310
+ _context6.next = 22;
285
311
  break;
286
312
  case 20:
287
- _context5.prev = 20;
288
- _context5.t1 = _context5["catch"](15);
313
+ _context6.prev = 20;
314
+ _context6.t1 = _context6["catch"](15);
289
315
  case 22:
290
- return _context5.finish(14);
316
+ return _context6.finish(14);
291
317
  case 23:
292
- _context5.next = 26;
318
+ _context6.next = 26;
293
319
  break;
294
320
  case 25:
295
- return _context5.abrupt("return", this.getInfo(queueName));
321
+ return _context6.abrupt("return", this.getInfo(queueName));
296
322
  case 26:
297
323
  case "end":
298
- return _context5.stop();
324
+ return _context6.stop();
299
325
  }
300
326
  }
301
- }, _callee5, this, [[2, 11, 14, 23], [15, 20]]);
327
+ }, _callee6, this, [[2, 11, 14, 23], [15, 20]]);
302
328
  }));
303
- function getInfo(_x2) {
329
+ function getInfo(_x3) {
304
330
  return _getInfo.apply(this, arguments);
305
331
  }
306
332
  return getInfo;
@@ -308,57 +334,57 @@ var RabbitMQAsync = /*#__PURE__*/function () {
308
334
  }, {
309
335
  key: "consume",
310
336
  value: function () {
311
- var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(queueName, cb) {
337
+ var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
312
338
  var callbackError,
313
339
  prefixQueueName,
314
340
  channel,
315
341
  messageQueue,
316
- _args7 = arguments;
317
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
342
+ _args8 = arguments;
343
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
318
344
  while (1) {
319
- switch (_context7.prev = _context7.next) {
345
+ switch (_context8.prev = _context8.next) {
320
346
  case 0:
321
- callbackError = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : null;
347
+ callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
322
348
  prefixQueueName = this.parsePrefix(queueName);
323
349
  if (!this.connected) {
324
- _context7.next = 30;
350
+ _context8.next = 30;
325
351
  break;
326
352
  }
327
- _context7.prev = 3;
328
- _context7.next = 6;
353
+ _context8.prev = 3;
354
+ _context8.next = 6;
329
355
  return this.client.createChannel();
330
356
  case 6:
331
- channel = _context7.sent;
332
- _context7.next = 9;
357
+ channel = _context8.sent;
358
+ _context8.next = 9;
333
359
  return channel.assertQueue(prefixQueueName, {
334
360
  durable: true
335
361
  });
336
362
  case 9:
337
- _context7.next = 11;
363
+ _context8.next = 11;
338
364
  return channel.prefetch(1);
339
365
  case 11:
340
366
  messageQueue = this;
341
- _context7.next = 14;
367
+ _context8.next = 14;
342
368
  return new Promise(function (resolve, reject) {
343
369
  channel.consume(prefixQueueName, /*#__PURE__*/function () {
344
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(msg) {
370
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
345
371
  var data, errString;
346
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
372
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
347
373
  while (1) {
348
- switch (_context6.prev = _context6.next) {
374
+ switch (_context7.prev = _context7.next) {
349
375
  case 0:
350
376
  if (!msg) {
351
- _context6.next = 50;
377
+ _context7.next = 55;
352
378
  break;
353
379
  }
354
380
  data = JSON.parse(msg.content.toString());
355
381
  if (!data) {
356
- _context6.next = 47;
382
+ _context7.next = 52;
357
383
  break;
358
384
  }
359
- _context6.prev = 3;
385
+ _context7.prev = 3;
360
386
  if (!msg.fields.redelivered) {
361
- _context6.next = 18;
387
+ _context7.next = 23;
362
388
  break;
363
389
  }
364
390
  if (!data.retry) {
@@ -366,160 +392,191 @@ var RabbitMQAsync = /*#__PURE__*/function () {
366
392
  }
367
393
  data.retry += 1;
368
394
  if (!(data.retry < 5)) {
369
- _context6.next = 12;
395
+ _context7.next = 17;
370
396
  break;
371
397
  }
372
- _context6.next = 10;
398
+ if (!(messageQueue.worker && !queueName.startsWith(messageQueue.serverIp))) {
399
+ _context7.next = 13;
400
+ break;
401
+ }
402
+ _context7.next = 11;
403
+ return messageQueue.send("".concat(messageQueue.serverIp, "_").concat(queueName), data);
404
+ case 11:
405
+ _context7.next = 15;
406
+ break;
407
+ case 13:
408
+ _context7.next = 15;
373
409
  return messageQueue.send(queueName, data);
374
- case 10:
375
- _context6.next = 14;
410
+ case 15:
411
+ _context7.next = 19;
376
412
  break;
377
- case 12:
378
- _context6.next = 14;
413
+ case 17:
414
+ _context7.next = 19;
379
415
  return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
380
416
  queueName: queueName,
381
417
  data: data,
382
418
  retry_time: new Date()
383
419
  });
384
- case 14:
385
- _context6.next = 16;
420
+ case 19:
421
+ _context7.next = 21;
386
422
  return channel.ack(msg);
387
- case 16:
388
- _context6.next = 40;
423
+ case 21:
424
+ _context7.next = 45;
389
425
  break;
390
- case 18:
391
- _context6.prev = 18;
392
- _context6.next = 21;
426
+ case 23:
427
+ _context7.prev = 23;
428
+ _context7.next = 26;
393
429
  return cb(data);
394
- case 21:
395
- _context6.next = 23;
430
+ case 26:
431
+ _context7.next = 28;
396
432
  return channel.ack(msg);
397
- case 23:
398
- _context6.next = 40;
433
+ case 28:
434
+ _context7.next = 45;
399
435
  break;
400
- case 25:
401
- _context6.prev = 25;
402
- _context6.t0 = _context6["catch"](18);
403
- errString = _context6.t0.toString();
436
+ case 30:
437
+ _context7.prev = 30;
438
+ _context7.t0 = _context7["catch"](23);
439
+ errString = _context7.t0.toString();
404
440
  if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
405
441
  return _lodash["default"].includes(errString, el);
406
442
  })) {
407
- _context6.next = 32;
443
+ _context7.next = 37;
408
444
  break;
409
445
  }
410
446
  reject(new Error(errString));
411
- _context6.next = 40;
447
+ _context7.next = 45;
412
448
  break;
413
- case 32:
449
+ case 37:
414
450
  if (callbackError) {
415
- callbackError(prefixQueueName, _context6.t0);
451
+ callbackError(prefixQueueName, _context7.t0);
416
452
  }
417
453
  if (!data.noRetry) {
418
- _context6.next = 38;
454
+ _context7.next = 43;
419
455
  break;
420
456
  }
421
- _context6.next = 36;
457
+ _context7.next = 41;
422
458
  return channel.ack(msg);
423
- case 36:
424
- _context6.next = 40;
459
+ case 41:
460
+ _context7.next = 45;
425
461
  break;
426
- case 38:
427
- _context6.next = 40;
462
+ case 43:
463
+ _context7.next = 45;
428
464
  return channel.nack(msg);
429
- case 40:
430
- _context6.next = 45;
431
- break;
432
- case 42:
433
- _context6.prev = 42;
434
- _context6.t1 = _context6["catch"](3);
435
- reject(_context6.t1);
436
465
  case 45:
437
- _context6.next = 48;
466
+ _context7.next = 50;
438
467
  break;
439
468
  case 47:
469
+ _context7.prev = 47;
470
+ _context7.t1 = _context7["catch"](3);
471
+ reject(_context7.t1);
472
+ case 50:
473
+ _context7.next = 53;
474
+ break;
475
+ case 52:
440
476
  channel.ack(msg);
441
- case 48:
442
- _context6.next = 51;
477
+ case 53:
478
+ _context7.next = 56;
443
479
  break;
444
- case 50:
480
+ case 55:
445
481
  reject(new Error('msg null'));
446
- case 51:
482
+ case 56:
447
483
  case "end":
448
- return _context6.stop();
484
+ return _context7.stop();
449
485
  }
450
486
  }
451
- }, _callee6, null, [[3, 42], [18, 25]]);
487
+ }, _callee7, null, [[3, 47], [23, 30]]);
452
488
  }));
453
- return function (_x5) {
489
+ return function (_x6) {
454
490
  return _ref.apply(this, arguments);
455
491
  };
456
492
  }());
457
493
  });
458
494
  case 14:
459
- _context7.next = 28;
495
+ _context8.next = 28;
460
496
  break;
461
497
  case 16:
462
- _context7.prev = 16;
463
- _context7.t0 = _context7["catch"](3);
464
- _context7.prev = 18;
465
- _context7.next = 21;
498
+ _context8.prev = 16;
499
+ _context8.t0 = _context8["catch"](3);
500
+ _context8.prev = 18;
501
+ _context8.next = 21;
466
502
  return channel.close();
467
503
  case 21:
468
- _context7.next = 25;
504
+ _context8.next = 25;
469
505
  break;
470
506
  case 23:
471
- _context7.prev = 23;
472
- _context7.t1 = _context7["catch"](18);
507
+ _context8.prev = 23;
508
+ _context8.t1 = _context8["catch"](18);
473
509
  case 25:
474
- _context7.next = 27;
510
+ _context8.next = 27;
475
511
  return (0, _util.timeout)(5000);
476
512
  case 27:
477
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
513
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
478
514
  case 28:
479
- _context7.next = 33;
515
+ _context8.next = 33;
480
516
  break;
481
517
  case 30:
482
- _context7.next = 32;
518
+ _context8.next = 32;
483
519
  return (0, _util.timeout)(5000);
484
520
  case 32:
485
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
521
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
486
522
  case 33:
487
- return _context7.abrupt("return", true);
523
+ return _context8.abrupt("return", true);
488
524
  case 34:
489
525
  case "end":
490
- return _context7.stop();
526
+ return _context8.stop();
491
527
  }
492
528
  }
493
- }, _callee7, this, [[3, 16], [18, 23]]);
529
+ }, _callee8, this, [[3, 16], [18, 23]]);
494
530
  }));
495
- function consume(_x3, _x4) {
531
+ function consume(_x4, _x5) {
496
532
  return _consume.apply(this, arguments);
497
533
  }
498
534
  return consume;
499
535
  }()
536
+ }, {
537
+ key: "selfReceiving",
538
+ value: function () {
539
+ var _selfReceiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(queueName, cb, callbackError) {
540
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
541
+ while (1) {
542
+ switch (_context9.prev = _context9.next) {
543
+ case 0:
544
+ this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
545
+ case 1:
546
+ case "end":
547
+ return _context9.stop();
548
+ }
549
+ }
550
+ }, _callee9, this);
551
+ }));
552
+ function selfReceiving(_x7, _x8, _x9) {
553
+ return _selfReceiving.apply(this, arguments);
554
+ }
555
+ return selfReceiving;
556
+ }()
500
557
  }, {
501
558
  key: "receiving",
502
559
  value: function () {
503
- var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
560
+ var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(queueName, cb) {
504
561
  var callbackError,
505
- _args8 = arguments;
506
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
562
+ _args10 = arguments;
563
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
507
564
  while (1) {
508
- switch (_context8.prev = _context8.next) {
565
+ switch (_context10.prev = _context10.next) {
509
566
  case 0:
510
- callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
567
+ callbackError = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : null;
511
568
  this.consume(queueName, cb, callbackError);
512
569
  if (this.worker) {
513
- this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
570
+ this.selfReceiving(queueName, cb, callbackError);
514
571
  }
515
572
  case 3:
516
573
  case "end":
517
- return _context8.stop();
574
+ return _context10.stop();
518
575
  }
519
576
  }
520
- }, _callee8, this);
577
+ }, _callee10, this);
521
578
  }));
522
- function receiving(_x6, _x7) {
579
+ function receiving(_x10, _x11) {
523
580
  return _receiving.apply(this, arguments);
524
581
  }
525
582
  return receiving;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/rabbitmq-async",
3
- "version": "1.0.161",
3
+ "version": "1.0.163",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./src/index.js",
@@ -72,7 +72,7 @@ class RabbitMQAsync {
72
72
  return name;
73
73
  }
74
74
 
75
- async send(queueName, msg = {}, force = true) {
75
+ async send(queueName, msg = {}, limit = false) {
76
76
  const prefixQueueName = this.parsePrefix(queueName);
77
77
 
78
78
  if (this.connected) {
@@ -83,7 +83,7 @@ class RabbitMQAsync {
83
83
  durable: true,
84
84
  });
85
85
 
86
- if (force || queueInfo.messageCount === 0) {
86
+ if (!limit || queueInfo.messageCount < limit) {
87
87
  await channel.sendToQueue(prefixQueueName, Buffer.from(JSON.stringify(msg)), {
88
88
  persistent: true,
89
89
  });
@@ -104,6 +104,10 @@ class RabbitMQAsync {
104
104
  }
105
105
  }
106
106
 
107
+ async sendNoRetry(queueName, msg = {}, limit = false) {
108
+ this.send(queueName, msg, limit);
109
+ }
110
+
107
111
  async getInfo(queueName) {
108
112
  const prefixQueueName = this.parsePrefix(queueName);
109
113
 
@@ -156,7 +160,11 @@ class RabbitMQAsync {
156
160
  data.retry += 1;
157
161
 
158
162
  if (data.retry < 5) {
159
- await messageQueue.send(queueName, data);
163
+ if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
164
+ await messageQueue.send(`${messageQueue.serverIp}_${queueName}`, data);
165
+ } else {
166
+ await messageQueue.send(queueName, data);
167
+ }
160
168
  } else {
161
169
  await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
162
170
  queueName,
@@ -213,11 +221,15 @@ class RabbitMQAsync {
213
221
  return true;
214
222
  }
215
223
 
224
+ async selfReceiving(queueName, cb, callbackError) {
225
+ this.consume(`${this.serverIp}_${queueName}`, cb, callbackError);
226
+ }
227
+
216
228
  async receiving(queueName, cb, callbackError = null) {
217
229
  this.consume(queueName, cb, callbackError);
218
230
 
219
231
  if (this.worker) {
220
- this.consume(`${this.serverIp}_${queueName}`, cb, callbackError);
232
+ this.selfReceiving(queueName, cb, callbackError);
221
233
  }
222
234
  }
223
235