@azteam/rabbitmq-async 1.0.162 → 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 = 51;
377
+ _context7.next = 55;
352
378
  break;
353
379
  }
354
380
  data = JSON.parse(msg.content.toString());
355
381
  if (!data) {
356
- _context6.next = 48;
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 = 19;
387
+ _context7.next = 23;
362
388
  break;
363
389
  }
364
390
  if (!data.retry) {
@@ -366,163 +392,191 @@ var RabbitMQAsync = /*#__PURE__*/function () {
366
392
  }
367
393
  data.retry += 1;
368
394
  if (!(data.retry < 5)) {
369
- _context6.next = 13;
395
+ _context7.next = 17;
370
396
  break;
371
397
  }
372
- if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
373
- queueName = "".concat(messageQueue.serverIp, "_").concat(queueName);
398
+ if (!(messageQueue.worker && !queueName.startsWith(messageQueue.serverIp))) {
399
+ _context7.next = 13;
400
+ break;
374
401
  }
375
- _context6.next = 11;
376
- return messageQueue.send(queueName, data);
402
+ _context7.next = 11;
403
+ return messageQueue.send("".concat(messageQueue.serverIp, "_").concat(queueName), data);
377
404
  case 11:
378
- _context6.next = 15;
405
+ _context7.next = 15;
379
406
  break;
380
407
  case 13:
381
- _context6.next = 15;
408
+ _context7.next = 15;
409
+ return messageQueue.send(queueName, data);
410
+ case 15:
411
+ _context7.next = 19;
412
+ break;
413
+ case 17:
414
+ _context7.next = 19;
382
415
  return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
383
416
  queueName: queueName,
384
417
  data: data,
385
418
  retry_time: new Date()
386
419
  });
387
- case 15:
388
- _context6.next = 17;
420
+ case 19:
421
+ _context7.next = 21;
389
422
  return channel.ack(msg);
390
- case 17:
391
- _context6.next = 41;
423
+ case 21:
424
+ _context7.next = 45;
392
425
  break;
393
- case 19:
394
- _context6.prev = 19;
395
- _context6.next = 22;
426
+ case 23:
427
+ _context7.prev = 23;
428
+ _context7.next = 26;
396
429
  return cb(data);
397
- case 22:
398
- _context6.next = 24;
430
+ case 26:
431
+ _context7.next = 28;
399
432
  return channel.ack(msg);
400
- case 24:
401
- _context6.next = 41;
433
+ case 28:
434
+ _context7.next = 45;
402
435
  break;
403
- case 26:
404
- _context6.prev = 26;
405
- _context6.t0 = _context6["catch"](19);
406
- errString = _context6.t0.toString();
436
+ case 30:
437
+ _context7.prev = 30;
438
+ _context7.t0 = _context7["catch"](23);
439
+ errString = _context7.t0.toString();
407
440
  if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
408
441
  return _lodash["default"].includes(errString, el);
409
442
  })) {
410
- _context6.next = 33;
443
+ _context7.next = 37;
411
444
  break;
412
445
  }
413
446
  reject(new Error(errString));
414
- _context6.next = 41;
447
+ _context7.next = 45;
415
448
  break;
416
- case 33:
449
+ case 37:
417
450
  if (callbackError) {
418
- callbackError(prefixQueueName, _context6.t0);
451
+ callbackError(prefixQueueName, _context7.t0);
419
452
  }
420
453
  if (!data.noRetry) {
421
- _context6.next = 39;
454
+ _context7.next = 43;
422
455
  break;
423
456
  }
424
- _context6.next = 37;
457
+ _context7.next = 41;
425
458
  return channel.ack(msg);
426
- case 37:
427
- _context6.next = 41;
428
- break;
429
- case 39:
430
- _context6.next = 41;
431
- return channel.nack(msg);
432
459
  case 41:
433
- _context6.next = 46;
460
+ _context7.next = 45;
434
461
  break;
435
462
  case 43:
436
- _context6.prev = 43;
437
- _context6.t1 = _context6["catch"](3);
438
- reject(_context6.t1);
439
- case 46:
440
- _context6.next = 49;
463
+ _context7.next = 45;
464
+ return channel.nack(msg);
465
+ case 45:
466
+ _context7.next = 50;
441
467
  break;
442
- case 48:
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:
443
476
  channel.ack(msg);
444
- case 49:
445
- _context6.next = 52;
477
+ case 53:
478
+ _context7.next = 56;
446
479
  break;
447
- case 51:
480
+ case 55:
448
481
  reject(new Error('msg null'));
449
- case 52:
482
+ case 56:
450
483
  case "end":
451
- return _context6.stop();
484
+ return _context7.stop();
452
485
  }
453
486
  }
454
- }, _callee6, null, [[3, 43], [19, 26]]);
487
+ }, _callee7, null, [[3, 47], [23, 30]]);
455
488
  }));
456
- return function (_x5) {
489
+ return function (_x6) {
457
490
  return _ref.apply(this, arguments);
458
491
  };
459
492
  }());
460
493
  });
461
494
  case 14:
462
- _context7.next = 28;
495
+ _context8.next = 28;
463
496
  break;
464
497
  case 16:
465
- _context7.prev = 16;
466
- _context7.t0 = _context7["catch"](3);
467
- _context7.prev = 18;
468
- _context7.next = 21;
498
+ _context8.prev = 16;
499
+ _context8.t0 = _context8["catch"](3);
500
+ _context8.prev = 18;
501
+ _context8.next = 21;
469
502
  return channel.close();
470
503
  case 21:
471
- _context7.next = 25;
504
+ _context8.next = 25;
472
505
  break;
473
506
  case 23:
474
- _context7.prev = 23;
475
- _context7.t1 = _context7["catch"](18);
507
+ _context8.prev = 23;
508
+ _context8.t1 = _context8["catch"](18);
476
509
  case 25:
477
- _context7.next = 27;
510
+ _context8.next = 27;
478
511
  return (0, _util.timeout)(5000);
479
512
  case 27:
480
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
513
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
481
514
  case 28:
482
- _context7.next = 33;
515
+ _context8.next = 33;
483
516
  break;
484
517
  case 30:
485
- _context7.next = 32;
518
+ _context8.next = 32;
486
519
  return (0, _util.timeout)(5000);
487
520
  case 32:
488
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
521
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
489
522
  case 33:
490
- return _context7.abrupt("return", true);
523
+ return _context8.abrupt("return", true);
491
524
  case 34:
492
525
  case "end":
493
- return _context7.stop();
526
+ return _context8.stop();
494
527
  }
495
528
  }
496
- }, _callee7, this, [[3, 16], [18, 23]]);
529
+ }, _callee8, this, [[3, 16], [18, 23]]);
497
530
  }));
498
- function consume(_x3, _x4) {
531
+ function consume(_x4, _x5) {
499
532
  return _consume.apply(this, arguments);
500
533
  }
501
534
  return consume;
502
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
+ }()
503
557
  }, {
504
558
  key: "receiving",
505
559
  value: function () {
506
- var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
560
+ var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(queueName, cb) {
507
561
  var callbackError,
508
- _args8 = arguments;
509
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
562
+ _args10 = arguments;
563
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
510
564
  while (1) {
511
- switch (_context8.prev = _context8.next) {
565
+ switch (_context10.prev = _context10.next) {
512
566
  case 0:
513
- callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
567
+ callbackError = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : null;
514
568
  this.consume(queueName, cb, callbackError);
515
569
  if (this.worker) {
516
- this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
570
+ this.selfReceiving(queueName, cb, callbackError);
517
571
  }
518
572
  case 3:
519
573
  case "end":
520
- return _context8.stop();
574
+ return _context10.stop();
521
575
  }
522
576
  }
523
- }, _callee8, this);
577
+ }, _callee10, this);
524
578
  }));
525
- function receiving(_x6, _x7) {
579
+ function receiving(_x10, _x11) {
526
580
  return _receiving.apply(this, arguments);
527
581
  }
528
582
  return receiving;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/rabbitmq-async",
3
- "version": "1.0.162",
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
 
@@ -157,9 +161,10 @@ class RabbitMQAsync {
157
161
 
158
162
  if (data.retry < 5) {
159
163
  if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
160
- queueName = `${messageQueue.serverIp}_${queueName}`;
164
+ await messageQueue.send(`${messageQueue.serverIp}_${queueName}`, data);
165
+ } else {
166
+ await messageQueue.send(queueName, data);
161
167
  }
162
- await messageQueue.send(queueName, data);
163
168
  } else {
164
169
  await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
165
170
  queueName,
@@ -216,11 +221,15 @@ class RabbitMQAsync {
216
221
  return true;
217
222
  }
218
223
 
224
+ async selfReceiving(queueName, cb, callbackError) {
225
+ this.consume(`${this.serverIp}_${queueName}`, cb, callbackError);
226
+ }
227
+
219
228
  async receiving(queueName, cb, callbackError = null) {
220
229
  this.consume(queueName, cb, callbackError);
221
230
 
222
231
  if (this.worker) {
223
- this.consume(`${this.serverIp}_${queueName}`, cb, callbackError);
232
+ this.selfReceiving(queueName, cb, callbackError);
224
233
  }
225
234
  }
226
235