@azteam/rabbitmq-async 1.0.162 → 1.0.164

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.
@@ -9,6 +9,9 @@ var _lodash = _interopRequireDefault(require("lodash"));
9
9
  var _amqplib = _interopRequireDefault(require("amqplib"));
10
10
  var _util = require("@azteam/util");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
15
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
13
16
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
14
17
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
@@ -169,7 +172,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
169
172
  value: function () {
170
173
  var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(queueName) {
171
174
  var msg,
172
- force,
175
+ limit,
173
176
  prefixQueueName,
174
177
  channel,
175
178
  queueInfo,
@@ -179,7 +182,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
179
182
  switch (_context4.prev = _context4.next) {
180
183
  case 0:
181
184
  msg = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
182
- force = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : true;
185
+ limit = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : 0;
183
186
  prefixQueueName = this.parsePrefix(queueName);
184
187
  if (!this.connected) {
185
188
  _context4.next = 33;
@@ -196,7 +199,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
196
199
  });
197
200
  case 10:
198
201
  queueInfo = _context4.sent;
199
- if (!(force || queueInfo.messageCount === 0)) {
202
+ if (!(limit === 0 || queueInfo.messageCount < limit)) {
200
203
  _context4.next = 14;
201
204
  break;
202
205
  }
@@ -247,60 +250,88 @@ var RabbitMQAsync = /*#__PURE__*/function () {
247
250
  return send;
248
251
  }()
249
252
  }, {
250
- key: "getInfo",
253
+ key: "sendNoRetry",
251
254
  value: function () {
252
- var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
253
- var prefixQueueName, channel;
255
+ var _sendNoRetry = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(queueName) {
256
+ var msg,
257
+ limit,
258
+ _args5 = arguments;
254
259
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
255
260
  while (1) {
256
261
  switch (_context5.prev = _context5.next) {
262
+ case 0:
263
+ msg = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
264
+ limit = _args5.length > 2 && _args5[2] !== undefined ? _args5[2] : 0;
265
+ this.send(queueName, _objectSpread(_objectSpread({}, msg), {}, {
266
+ noRetry: true
267
+ }), limit);
268
+ case 3:
269
+ case "end":
270
+ return _context5.stop();
271
+ }
272
+ }
273
+ }, _callee5, this);
274
+ }));
275
+ function sendNoRetry(_x2) {
276
+ return _sendNoRetry.apply(this, arguments);
277
+ }
278
+ return sendNoRetry;
279
+ }()
280
+ }, {
281
+ key: "getInfo",
282
+ value: function () {
283
+ var _getInfo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(queueName) {
284
+ var prefixQueueName, channel;
285
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
286
+ while (1) {
287
+ switch (_context6.prev = _context6.next) {
257
288
  case 0:
258
289
  prefixQueueName = this.parsePrefix(queueName);
259
290
  if (!this.connected) {
260
- _context5.next = 25;
291
+ _context6.next = 25;
261
292
  break;
262
293
  }
263
- _context5.prev = 2;
264
- _context5.next = 5;
294
+ _context6.prev = 2;
295
+ _context6.next = 5;
265
296
  return this.client.createChannel();
266
297
  case 5:
267
- channel = _context5.sent;
268
- _context5.next = 8;
298
+ channel = _context6.sent;
299
+ _context6.next = 8;
269
300
  return channel.assertQueue(prefixQueueName, {
270
301
  durable: true
271
302
  });
272
303
  case 8:
273
- return _context5.abrupt("return", _context5.sent);
304
+ return _context6.abrupt("return", _context6.sent);
274
305
  case 11:
275
- _context5.prev = 11;
276
- _context5.t0 = _context5["catch"](2);
277
- return _context5.abrupt("return", this.getInfo(queueName));
306
+ _context6.prev = 11;
307
+ _context6.t0 = _context6["catch"](2);
308
+ return _context6.abrupt("return", this.getInfo(queueName));
278
309
  case 14:
279
- _context5.prev = 14;
280
- _context5.prev = 15;
281
- _context5.next = 18;
310
+ _context6.prev = 14;
311
+ _context6.prev = 15;
312
+ _context6.next = 18;
282
313
  return channel.close();
283
314
  case 18:
284
- _context5.next = 22;
315
+ _context6.next = 22;
285
316
  break;
286
317
  case 20:
287
- _context5.prev = 20;
288
- _context5.t1 = _context5["catch"](15);
318
+ _context6.prev = 20;
319
+ _context6.t1 = _context6["catch"](15);
289
320
  case 22:
290
- return _context5.finish(14);
321
+ return _context6.finish(14);
291
322
  case 23:
292
- _context5.next = 26;
323
+ _context6.next = 26;
293
324
  break;
294
325
  case 25:
295
- return _context5.abrupt("return", this.getInfo(queueName));
326
+ return _context6.abrupt("return", this.getInfo(queueName));
296
327
  case 26:
297
328
  case "end":
298
- return _context5.stop();
329
+ return _context6.stop();
299
330
  }
300
331
  }
301
- }, _callee5, this, [[2, 11, 14, 23], [15, 20]]);
332
+ }, _callee6, this, [[2, 11, 14, 23], [15, 20]]);
302
333
  }));
303
- function getInfo(_x2) {
334
+ function getInfo(_x3) {
304
335
  return _getInfo.apply(this, arguments);
305
336
  }
306
337
  return getInfo;
@@ -308,57 +339,57 @@ var RabbitMQAsync = /*#__PURE__*/function () {
308
339
  }, {
309
340
  key: "consume",
310
341
  value: function () {
311
- var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(queueName, cb) {
342
+ var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
312
343
  var callbackError,
313
344
  prefixQueueName,
314
345
  channel,
315
346
  messageQueue,
316
- _args7 = arguments;
317
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
347
+ _args8 = arguments;
348
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
318
349
  while (1) {
319
- switch (_context7.prev = _context7.next) {
350
+ switch (_context8.prev = _context8.next) {
320
351
  case 0:
321
- callbackError = _args7.length > 2 && _args7[2] !== undefined ? _args7[2] : null;
352
+ callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
322
353
  prefixQueueName = this.parsePrefix(queueName);
323
354
  if (!this.connected) {
324
- _context7.next = 30;
355
+ _context8.next = 30;
325
356
  break;
326
357
  }
327
- _context7.prev = 3;
328
- _context7.next = 6;
358
+ _context8.prev = 3;
359
+ _context8.next = 6;
329
360
  return this.client.createChannel();
330
361
  case 6:
331
- channel = _context7.sent;
332
- _context7.next = 9;
362
+ channel = _context8.sent;
363
+ _context8.next = 9;
333
364
  return channel.assertQueue(prefixQueueName, {
334
365
  durable: true
335
366
  });
336
367
  case 9:
337
- _context7.next = 11;
368
+ _context8.next = 11;
338
369
  return channel.prefetch(1);
339
370
  case 11:
340
371
  messageQueue = this;
341
- _context7.next = 14;
372
+ _context8.next = 14;
342
373
  return new Promise(function (resolve, reject) {
343
374
  channel.consume(prefixQueueName, /*#__PURE__*/function () {
344
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(msg) {
375
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(msg) {
345
376
  var data, errString;
346
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
377
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
347
378
  while (1) {
348
- switch (_context6.prev = _context6.next) {
379
+ switch (_context7.prev = _context7.next) {
349
380
  case 0:
350
381
  if (!msg) {
351
- _context6.next = 51;
382
+ _context7.next = 55;
352
383
  break;
353
384
  }
354
385
  data = JSON.parse(msg.content.toString());
355
386
  if (!data) {
356
- _context6.next = 48;
387
+ _context7.next = 52;
357
388
  break;
358
389
  }
359
- _context6.prev = 3;
390
+ _context7.prev = 3;
360
391
  if (!msg.fields.redelivered) {
361
- _context6.next = 19;
392
+ _context7.next = 23;
362
393
  break;
363
394
  }
364
395
  if (!data.retry) {
@@ -366,163 +397,191 @@ var RabbitMQAsync = /*#__PURE__*/function () {
366
397
  }
367
398
  data.retry += 1;
368
399
  if (!(data.retry < 5)) {
369
- _context6.next = 13;
400
+ _context7.next = 17;
370
401
  break;
371
402
  }
372
- if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
373
- queueName = "".concat(messageQueue.serverIp, "_").concat(queueName);
403
+ if (!(messageQueue.worker && !queueName.startsWith(messageQueue.serverIp))) {
404
+ _context7.next = 13;
405
+ break;
374
406
  }
375
- _context6.next = 11;
376
- return messageQueue.send(queueName, data);
407
+ _context7.next = 11;
408
+ return messageQueue.send("".concat(messageQueue.serverIp, "_").concat(queueName), data);
377
409
  case 11:
378
- _context6.next = 15;
410
+ _context7.next = 15;
379
411
  break;
380
412
  case 13:
381
- _context6.next = 15;
413
+ _context7.next = 15;
414
+ return messageQueue.send(queueName, data);
415
+ case 15:
416
+ _context7.next = 19;
417
+ break;
418
+ case 17:
419
+ _context7.next = 19;
382
420
  return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
383
421
  queueName: queueName,
384
422
  data: data,
385
423
  retry_time: new Date()
386
424
  });
387
- case 15:
388
- _context6.next = 17;
425
+ case 19:
426
+ _context7.next = 21;
389
427
  return channel.ack(msg);
390
- case 17:
391
- _context6.next = 41;
428
+ case 21:
429
+ _context7.next = 45;
392
430
  break;
393
- case 19:
394
- _context6.prev = 19;
395
- _context6.next = 22;
431
+ case 23:
432
+ _context7.prev = 23;
433
+ _context7.next = 26;
396
434
  return cb(data);
397
- case 22:
398
- _context6.next = 24;
435
+ case 26:
436
+ _context7.next = 28;
399
437
  return channel.ack(msg);
400
- case 24:
401
- _context6.next = 41;
438
+ case 28:
439
+ _context7.next = 45;
402
440
  break;
403
- case 26:
404
- _context6.prev = 26;
405
- _context6.t0 = _context6["catch"](19);
406
- errString = _context6.t0.toString();
441
+ case 30:
442
+ _context7.prev = 30;
443
+ _context7.t0 = _context7["catch"](23);
444
+ errString = _context7.t0.toString();
407
445
  if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
408
446
  return _lodash["default"].includes(errString, el);
409
447
  })) {
410
- _context6.next = 33;
448
+ _context7.next = 37;
411
449
  break;
412
450
  }
413
451
  reject(new Error(errString));
414
- _context6.next = 41;
452
+ _context7.next = 45;
415
453
  break;
416
- case 33:
454
+ case 37:
417
455
  if (callbackError) {
418
- callbackError(prefixQueueName, _context6.t0);
456
+ callbackError(prefixQueueName, _context7.t0);
419
457
  }
420
458
  if (!data.noRetry) {
421
- _context6.next = 39;
459
+ _context7.next = 43;
422
460
  break;
423
461
  }
424
- _context6.next = 37;
462
+ _context7.next = 41;
425
463
  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
464
  case 41:
433
- _context6.next = 46;
465
+ _context7.next = 45;
434
466
  break;
435
467
  case 43:
436
- _context6.prev = 43;
437
- _context6.t1 = _context6["catch"](3);
438
- reject(_context6.t1);
439
- case 46:
440
- _context6.next = 49;
468
+ _context7.next = 45;
469
+ return channel.nack(msg);
470
+ case 45:
471
+ _context7.next = 50;
441
472
  break;
442
- case 48:
473
+ case 47:
474
+ _context7.prev = 47;
475
+ _context7.t1 = _context7["catch"](3);
476
+ reject(_context7.t1);
477
+ case 50:
478
+ _context7.next = 53;
479
+ break;
480
+ case 52:
443
481
  channel.ack(msg);
444
- case 49:
445
- _context6.next = 52;
482
+ case 53:
483
+ _context7.next = 56;
446
484
  break;
447
- case 51:
485
+ case 55:
448
486
  reject(new Error('msg null'));
449
- case 52:
487
+ case 56:
450
488
  case "end":
451
- return _context6.stop();
489
+ return _context7.stop();
452
490
  }
453
491
  }
454
- }, _callee6, null, [[3, 43], [19, 26]]);
492
+ }, _callee7, null, [[3, 47], [23, 30]]);
455
493
  }));
456
- return function (_x5) {
494
+ return function (_x6) {
457
495
  return _ref.apply(this, arguments);
458
496
  };
459
497
  }());
460
498
  });
461
499
  case 14:
462
- _context7.next = 28;
500
+ _context8.next = 28;
463
501
  break;
464
502
  case 16:
465
- _context7.prev = 16;
466
- _context7.t0 = _context7["catch"](3);
467
- _context7.prev = 18;
468
- _context7.next = 21;
503
+ _context8.prev = 16;
504
+ _context8.t0 = _context8["catch"](3);
505
+ _context8.prev = 18;
506
+ _context8.next = 21;
469
507
  return channel.close();
470
508
  case 21:
471
- _context7.next = 25;
509
+ _context8.next = 25;
472
510
  break;
473
511
  case 23:
474
- _context7.prev = 23;
475
- _context7.t1 = _context7["catch"](18);
512
+ _context8.prev = 23;
513
+ _context8.t1 = _context8["catch"](18);
476
514
  case 25:
477
- _context7.next = 27;
515
+ _context8.next = 27;
478
516
  return (0, _util.timeout)(5000);
479
517
  case 27:
480
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
518
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
481
519
  case 28:
482
- _context7.next = 33;
520
+ _context8.next = 33;
483
521
  break;
484
522
  case 30:
485
- _context7.next = 32;
523
+ _context8.next = 32;
486
524
  return (0, _util.timeout)(5000);
487
525
  case 32:
488
- return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
526
+ return _context8.abrupt("return", this.consume(queueName, cb, callbackError));
489
527
  case 33:
490
- return _context7.abrupt("return", true);
528
+ return _context8.abrupt("return", true);
491
529
  case 34:
492
530
  case "end":
493
- return _context7.stop();
531
+ return _context8.stop();
494
532
  }
495
533
  }
496
- }, _callee7, this, [[3, 16], [18, 23]]);
534
+ }, _callee8, this, [[3, 16], [18, 23]]);
497
535
  }));
498
- function consume(_x3, _x4) {
536
+ function consume(_x4, _x5) {
499
537
  return _consume.apply(this, arguments);
500
538
  }
501
539
  return consume;
502
540
  }()
541
+ }, {
542
+ key: "selfReceiving",
543
+ value: function () {
544
+ var _selfReceiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(queueName, cb, callbackError) {
545
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
546
+ while (1) {
547
+ switch (_context9.prev = _context9.next) {
548
+ case 0:
549
+ this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
550
+ case 1:
551
+ case "end":
552
+ return _context9.stop();
553
+ }
554
+ }
555
+ }, _callee9, this);
556
+ }));
557
+ function selfReceiving(_x7, _x8, _x9) {
558
+ return _selfReceiving.apply(this, arguments);
559
+ }
560
+ return selfReceiving;
561
+ }()
503
562
  }, {
504
563
  key: "receiving",
505
564
  value: function () {
506
- var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
565
+ var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(queueName, cb) {
507
566
  var callbackError,
508
- _args8 = arguments;
509
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
567
+ _args10 = arguments;
568
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
510
569
  while (1) {
511
- switch (_context8.prev = _context8.next) {
570
+ switch (_context10.prev = _context10.next) {
512
571
  case 0:
513
- callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
572
+ callbackError = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : null;
514
573
  this.consume(queueName, cb, callbackError);
515
574
  if (this.worker) {
516
- this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
575
+ this.selfReceiving(queueName, cb, callbackError);
517
576
  }
518
577
  case 3:
519
578
  case "end":
520
- return _context8.stop();
579
+ return _context10.stop();
521
580
  }
522
581
  }
523
- }, _callee8, this);
582
+ }, _callee10, this);
524
583
  }));
525
- function receiving(_x6, _x7) {
584
+ function receiving(_x10, _x11) {
526
585
  return _receiving.apply(this, arguments);
527
586
  }
528
587
  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.164",
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 = 0) {
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 === 0 || 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 = 0) {
108
+ this.send(queueName, {...msg, noRetry: true}, 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