@autofleet/rabbit 2.0.3 → 2.0.4-beta

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/dist/index.js CHANGED
@@ -45,6 +45,17 @@ const defaultOptions = {
45
45
  deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
46
46
  };
47
47
  const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
48
+ const callbackWithTimeout = (callback, args, timeout, timeoutMessage = 'Timeout while ack message') => __awaiter(void 0, void 0, void 0, function* () {
49
+ yield new bluebird.Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
50
+ try {
51
+ yield callback(...args);
52
+ return resolve();
53
+ }
54
+ catch (e) {
55
+ return reject(e);
56
+ }
57
+ })).timeout(timeout, timeoutMessage);
58
+ });
48
59
  const connectionCreatedConst = 'connectionCreated';
49
60
  class RabbitMq {
50
61
  constructor(options) {
@@ -61,6 +72,7 @@ class RabbitMq {
61
72
  && (!msg.content['x-retry-count']
62
73
  || msg.content['x-retry-count'] < options.retries)) {
63
74
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
75
+ // msg.content.nackCount = msg.content.nackCount ? msg.content.nackCount + 1 : 1;
64
76
  yield this.sendToQueue(queue, msg.content);
65
77
  }
66
78
  else {
@@ -194,7 +206,17 @@ class RabbitMq {
194
206
  return Promise.all([
195
207
  c.consume(queue, (msg) => {
196
208
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
197
- callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
209
+ if (optionsWithDefaults.timeout) {
210
+ const args = [
211
+ RabbitMq.parseMsg(msg),
212
+ this.ack(channel),
213
+ this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
214
+ ];
215
+ callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
216
+ }
217
+ else {
218
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
219
+ }
198
220
  }),
199
221
  ]);
200
222
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.0.3",
3
+ "version": "2.0.4-beta",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -37,6 +37,17 @@ const defaultOptions = {
37
37
 
38
38
  const assertExchangeFanout = (c: any, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
39
39
 
40
+ const callbackWithTimeout = async (callback: any, args: any[], timeout: number, timeoutMessage = 'Timeout while ack message') => {
41
+ await new bluebird.Promise(async (resolve: any, reject: any) => {
42
+ try {
43
+ await callback(...args);
44
+ return resolve();
45
+ } catch (e) {
46
+ return reject(e);
47
+ }
48
+ }).timeout(timeout, timeoutMessage);
49
+ };
50
+
40
51
  const connectionCreatedConst = 'connectionCreated';
41
52
  class RabbitMq implements IAfRabbitMq {
42
53
  static parseMsg(msg: any) {
@@ -110,6 +121,7 @@ class RabbitMq implements IAfRabbitMq {
110
121
  || msg.content['x-retry-count'] < options.retries)
111
122
  ) {
112
123
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
124
+ // msg.content.nackCount = msg.content.nackCount ? msg.content.nackCount + 1 : 1;
113
125
  await this.sendToQueue(queue, msg.content);
114
126
  } else {
115
127
  const deadQueue = `${queue}-dead`;
@@ -216,7 +228,16 @@ class RabbitMq implements IAfRabbitMq {
216
228
  queue,
217
229
  (msg: any) => {
218
230
  newTrace(traceTypes.RABBIT);
219
- callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
231
+ if (optionsWithDefaults.timeout) {
232
+ const args = [
233
+ RabbitMq.parseMsg(msg),
234
+ this.ack(channel),
235
+ this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
236
+ ];
237
+ callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
238
+ } else {
239
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
240
+ }
220
241
  },
221
242
  ),
222
243
  ]);