@azteam/rabbitmq-async 1.0.160 → 1.0.162
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.
- package/lib/RabbitMQAsync.js +83 -49
- package/package.json +1 -1
- package/src/RabbitMQAsync.js +18 -3
package/lib/RabbitMQAsync.js
CHANGED
|
@@ -23,6 +23,8 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
23
23
|
this.password = config.password;
|
|
24
24
|
this.host = config.host;
|
|
25
25
|
this.prefix = config.prefix;
|
|
26
|
+
this.serverIp = config.server_ip;
|
|
27
|
+
this.worker = config.worker;
|
|
26
28
|
this.connect();
|
|
27
29
|
}
|
|
28
30
|
_createClass(RabbitMQAsync, [{
|
|
@@ -304,9 +306,9 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
304
306
|
return getInfo;
|
|
305
307
|
}()
|
|
306
308
|
}, {
|
|
307
|
-
key: "
|
|
309
|
+
key: "consume",
|
|
308
310
|
value: function () {
|
|
309
|
-
var
|
|
311
|
+
var _consume = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(queueName, cb) {
|
|
310
312
|
var callbackError,
|
|
311
313
|
prefixQueueName,
|
|
312
314
|
channel,
|
|
@@ -346,17 +348,17 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
346
348
|
switch (_context6.prev = _context6.next) {
|
|
347
349
|
case 0:
|
|
348
350
|
if (!msg) {
|
|
349
|
-
_context6.next =
|
|
351
|
+
_context6.next = 51;
|
|
350
352
|
break;
|
|
351
353
|
}
|
|
352
354
|
data = JSON.parse(msg.content.toString());
|
|
353
355
|
if (!data) {
|
|
354
|
-
_context6.next =
|
|
356
|
+
_context6.next = 48;
|
|
355
357
|
break;
|
|
356
358
|
}
|
|
357
359
|
_context6.prev = 3;
|
|
358
360
|
if (!msg.fields.redelivered) {
|
|
359
|
-
_context6.next =
|
|
361
|
+
_context6.next = 19;
|
|
360
362
|
break;
|
|
361
363
|
}
|
|
362
364
|
if (!data.retry) {
|
|
@@ -364,89 +366,92 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
364
366
|
}
|
|
365
367
|
data.retry += 1;
|
|
366
368
|
if (!(data.retry < 5)) {
|
|
367
|
-
_context6.next =
|
|
369
|
+
_context6.next = 13;
|
|
368
370
|
break;
|
|
369
371
|
}
|
|
370
|
-
|
|
372
|
+
if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
|
|
373
|
+
queueName = "".concat(messageQueue.serverIp, "_").concat(queueName);
|
|
374
|
+
}
|
|
375
|
+
_context6.next = 11;
|
|
371
376
|
return messageQueue.send(queueName, data);
|
|
372
|
-
case
|
|
373
|
-
_context6.next =
|
|
377
|
+
case 11:
|
|
378
|
+
_context6.next = 15;
|
|
374
379
|
break;
|
|
375
|
-
case
|
|
376
|
-
_context6.next =
|
|
380
|
+
case 13:
|
|
381
|
+
_context6.next = 15;
|
|
377
382
|
return messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
378
383
|
queueName: queueName,
|
|
379
384
|
data: data,
|
|
380
385
|
retry_time: new Date()
|
|
381
386
|
});
|
|
382
|
-
case
|
|
383
|
-
_context6.next =
|
|
387
|
+
case 15:
|
|
388
|
+
_context6.next = 17;
|
|
384
389
|
return channel.ack(msg);
|
|
385
|
-
case
|
|
386
|
-
_context6.next =
|
|
390
|
+
case 17:
|
|
391
|
+
_context6.next = 41;
|
|
387
392
|
break;
|
|
388
|
-
case
|
|
389
|
-
_context6.prev =
|
|
390
|
-
_context6.next =
|
|
393
|
+
case 19:
|
|
394
|
+
_context6.prev = 19;
|
|
395
|
+
_context6.next = 22;
|
|
391
396
|
return cb(data);
|
|
392
|
-
case
|
|
393
|
-
_context6.next =
|
|
397
|
+
case 22:
|
|
398
|
+
_context6.next = 24;
|
|
394
399
|
return channel.ack(msg);
|
|
395
|
-
case
|
|
396
|
-
_context6.next =
|
|
400
|
+
case 24:
|
|
401
|
+
_context6.next = 41;
|
|
397
402
|
break;
|
|
398
|
-
case
|
|
399
|
-
_context6.prev =
|
|
400
|
-
_context6.t0 = _context6["catch"](
|
|
403
|
+
case 26:
|
|
404
|
+
_context6.prev = 26;
|
|
405
|
+
_context6.t0 = _context6["catch"](19);
|
|
401
406
|
errString = _context6.t0.toString();
|
|
402
407
|
if (!_lodash["default"].some(['IllegalOperationError: Channel closed'], function (el) {
|
|
403
408
|
return _lodash["default"].includes(errString, el);
|
|
404
409
|
})) {
|
|
405
|
-
_context6.next =
|
|
410
|
+
_context6.next = 33;
|
|
406
411
|
break;
|
|
407
412
|
}
|
|
408
413
|
reject(new Error(errString));
|
|
409
|
-
_context6.next =
|
|
414
|
+
_context6.next = 41;
|
|
410
415
|
break;
|
|
411
|
-
case
|
|
416
|
+
case 33:
|
|
412
417
|
if (callbackError) {
|
|
413
418
|
callbackError(prefixQueueName, _context6.t0);
|
|
414
419
|
}
|
|
415
420
|
if (!data.noRetry) {
|
|
416
|
-
_context6.next =
|
|
421
|
+
_context6.next = 39;
|
|
417
422
|
break;
|
|
418
423
|
}
|
|
419
|
-
_context6.next =
|
|
424
|
+
_context6.next = 37;
|
|
420
425
|
return channel.ack(msg);
|
|
421
|
-
case
|
|
422
|
-
_context6.next =
|
|
426
|
+
case 37:
|
|
427
|
+
_context6.next = 41;
|
|
423
428
|
break;
|
|
424
|
-
case
|
|
425
|
-
_context6.next =
|
|
429
|
+
case 39:
|
|
430
|
+
_context6.next = 41;
|
|
426
431
|
return channel.nack(msg);
|
|
427
|
-
case
|
|
428
|
-
_context6.next =
|
|
432
|
+
case 41:
|
|
433
|
+
_context6.next = 46;
|
|
429
434
|
break;
|
|
430
|
-
case
|
|
431
|
-
_context6.prev =
|
|
435
|
+
case 43:
|
|
436
|
+
_context6.prev = 43;
|
|
432
437
|
_context6.t1 = _context6["catch"](3);
|
|
433
438
|
reject(_context6.t1);
|
|
434
|
-
case
|
|
435
|
-
_context6.next =
|
|
439
|
+
case 46:
|
|
440
|
+
_context6.next = 49;
|
|
436
441
|
break;
|
|
437
|
-
case 47:
|
|
438
|
-
channel.ack(msg);
|
|
439
442
|
case 48:
|
|
440
|
-
|
|
443
|
+
channel.ack(msg);
|
|
444
|
+
case 49:
|
|
445
|
+
_context6.next = 52;
|
|
441
446
|
break;
|
|
442
|
-
case 50:
|
|
443
|
-
reject(new Error('msg null'));
|
|
444
447
|
case 51:
|
|
448
|
+
reject(new Error('msg null'));
|
|
449
|
+
case 52:
|
|
445
450
|
case "end":
|
|
446
451
|
return _context6.stop();
|
|
447
452
|
}
|
|
448
453
|
}
|
|
449
|
-
}, _callee6, null, [[3,
|
|
454
|
+
}, _callee6, null, [[3, 43], [19, 26]]);
|
|
450
455
|
}));
|
|
451
456
|
return function (_x5) {
|
|
452
457
|
return _ref.apply(this, arguments);
|
|
@@ -472,7 +477,7 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
472
477
|
_context7.next = 27;
|
|
473
478
|
return (0, _util.timeout)(5000);
|
|
474
479
|
case 27:
|
|
475
|
-
return _context7.abrupt("return", this.
|
|
480
|
+
return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
|
|
476
481
|
case 28:
|
|
477
482
|
_context7.next = 33;
|
|
478
483
|
break;
|
|
@@ -480,15 +485,44 @@ var RabbitMQAsync = /*#__PURE__*/function () {
|
|
|
480
485
|
_context7.next = 32;
|
|
481
486
|
return (0, _util.timeout)(5000);
|
|
482
487
|
case 32:
|
|
483
|
-
return _context7.abrupt("return", this.
|
|
488
|
+
return _context7.abrupt("return", this.consume(queueName, cb, callbackError));
|
|
484
489
|
case 33:
|
|
490
|
+
return _context7.abrupt("return", true);
|
|
491
|
+
case 34:
|
|
485
492
|
case "end":
|
|
486
493
|
return _context7.stop();
|
|
487
494
|
}
|
|
488
495
|
}
|
|
489
496
|
}, _callee7, this, [[3, 16], [18, 23]]);
|
|
490
497
|
}));
|
|
491
|
-
function
|
|
498
|
+
function consume(_x3, _x4) {
|
|
499
|
+
return _consume.apply(this, arguments);
|
|
500
|
+
}
|
|
501
|
+
return consume;
|
|
502
|
+
}()
|
|
503
|
+
}, {
|
|
504
|
+
key: "receiving",
|
|
505
|
+
value: function () {
|
|
506
|
+
var _receiving = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(queueName, cb) {
|
|
507
|
+
var callbackError,
|
|
508
|
+
_args8 = arguments;
|
|
509
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
510
|
+
while (1) {
|
|
511
|
+
switch (_context8.prev = _context8.next) {
|
|
512
|
+
case 0:
|
|
513
|
+
callbackError = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
|
|
514
|
+
this.consume(queueName, cb, callbackError);
|
|
515
|
+
if (this.worker) {
|
|
516
|
+
this.consume("".concat(this.serverIp, "_").concat(queueName), cb, callbackError);
|
|
517
|
+
}
|
|
518
|
+
case 3:
|
|
519
|
+
case "end":
|
|
520
|
+
return _context8.stop();
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}, _callee8, this);
|
|
524
|
+
}));
|
|
525
|
+
function receiving(_x6, _x7) {
|
|
492
526
|
return _receiving.apply(this, arguments);
|
|
493
527
|
}
|
|
494
528
|
return receiving;
|
package/package.json
CHANGED
package/src/RabbitMQAsync.js
CHANGED
|
@@ -9,6 +9,8 @@ class RabbitMQAsync {
|
|
|
9
9
|
this.password = config.password;
|
|
10
10
|
this.host = config.host;
|
|
11
11
|
this.prefix = config.prefix;
|
|
12
|
+
this.serverIp = config.server_ip;
|
|
13
|
+
this.worker = config.worker;
|
|
12
14
|
this.connect();
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -124,7 +126,7 @@ class RabbitMQAsync {
|
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
async
|
|
129
|
+
async consume(queueName, cb, callbackError = null) {
|
|
128
130
|
const prefixQueueName = this.parsePrefix(queueName);
|
|
129
131
|
|
|
130
132
|
if (this.connected) {
|
|
@@ -154,6 +156,9 @@ class RabbitMQAsync {
|
|
|
154
156
|
data.retry += 1;
|
|
155
157
|
|
|
156
158
|
if (data.retry < 5) {
|
|
159
|
+
if (messageQueue.worker && !queueName.startsWith(messageQueue.serverIp)) {
|
|
160
|
+
queueName = `${messageQueue.serverIp}_${queueName}`;
|
|
161
|
+
}
|
|
157
162
|
await messageQueue.send(queueName, data);
|
|
158
163
|
} else {
|
|
159
164
|
await messageQueue.send(messageQueue.parsePrefix('RETRY'), {
|
|
@@ -201,11 +206,21 @@ class RabbitMQAsync {
|
|
|
201
206
|
} catch (err2) {}
|
|
202
207
|
|
|
203
208
|
await timeout(5000);
|
|
204
|
-
return this.
|
|
209
|
+
return this.consume(queueName, cb, callbackError);
|
|
205
210
|
}
|
|
206
211
|
} else {
|
|
207
212
|
await timeout(5000);
|
|
208
|
-
return this.
|
|
213
|
+
return this.consume(queueName, cb, callbackError);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async receiving(queueName, cb, callbackError = null) {
|
|
220
|
+
this.consume(queueName, cb, callbackError);
|
|
221
|
+
|
|
222
|
+
if (this.worker) {
|
|
223
|
+
this.consume(`${this.serverIp}_${queueName}`, cb, callbackError);
|
|
209
224
|
}
|
|
210
225
|
}
|
|
211
226
|
|