@autofleet/rabbit 2.0.4-beta4 → 2.0.4-beta5

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.d.ts CHANGED
@@ -32,7 +32,11 @@ declare class RabbitMq implements IAfRabbitMq {
32
32
  creatingConnection: boolean;
33
33
  exchanges: ExchangesCache;
34
34
  options: AfRabbitOptions | undefined;
35
- constructor(options?: AfRabbitOptions);
35
+ logger: any;
36
+ constructor({ options, logger }?: {
37
+ options?: AfRabbitOptions;
38
+ logger?: any;
39
+ });
36
40
  ack: (channel: ChannelWrapper) => (msg: any) => Promise<void>;
37
41
  nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
38
42
  getConnection(): Promise<AmqpConnectionManager>;
package/dist/index.js CHANGED
@@ -46,21 +46,19 @@ const defaultOptions = {
46
46
  };
47
47
  const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
48
48
  const callbackWithTimeout = (callback, args, timeout, timeoutMessage = 'Timeout while ack message') => __awaiter(void 0, void 0, void 0, function* () {
49
- const callbackPromise = new bluebird.Promise(callback(...args));
50
- Promise.race([
51
- callbackPromise,
52
- new Promise((resolve, reject) => {
53
- setTimeout(() => {
54
- callbackPromise.cancel();
55
- console.log('callbackPromise', callbackPromise.isCancelled());
56
- reject(timeoutMessage);
57
- }, timeout);
58
- }),
59
- ]);
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);
60
58
  });
61
59
  const connectionCreatedConst = 'connectionCreated';
62
60
  class RabbitMq {
63
- constructor(options) {
61
+ constructor({ options, logger } = {}) {
64
62
  this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
65
63
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
66
64
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
@@ -74,7 +72,6 @@ class RabbitMq {
74
72
  && (!msg.content['x-retry-count']
75
73
  || msg.content['x-retry-count'] < options.retries)) {
76
74
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
77
- // msg.content.nackCount = msg.content.nackCount ? msg.content.nackCount + 1 : 1;
78
75
  yield this.sendToQueue(queue, msg.content);
79
76
  }
80
77
  else {
@@ -91,6 +88,7 @@ class RabbitMq {
91
88
  this.creatingConnection = false;
92
89
  this.exchanges = {};
93
90
  this.options = options;
91
+ this.logger = logger;
94
92
  }
95
93
  static parseMsg(msg) {
96
94
  let { content } = msg;
@@ -207,6 +205,7 @@ class RabbitMq {
207
205
  yield c.prefetch(limit, true);
208
206
  return Promise.all([
209
207
  c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
208
+ var _a;
210
209
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
211
210
  if (optionsWithDefaults.timeout) {
212
211
  const args = [
@@ -218,6 +217,10 @@ class RabbitMq {
218
217
  yield callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
219
218
  }
220
219
  catch (e) {
220
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error('message nacked because of error in callbackWithTimeout', {
221
+ error: e,
222
+ msg,
223
+ });
221
224
  this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
222
225
  }
223
226
  }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.0.4-beta4",
3
+ "version": "2.0.4-beta5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "postinstall": "echo '\\033[0;31m\nWARNING: in case of migrating to new version of @autofleet/rabbit \nyou might have to delete existing queues or the deployment will fail.\\033[0m\n'",
8
8
  "start": "ts-node src/index.ts",
9
9
  "example": "ts-node src/example.ts",
10
- "build": "tsc",
10
+ "build": "rm -rf dist && tsc",
11
+ "prepublish": "npm run build",
11
12
  "linter": "./node_modules/.bin/eslint .",
12
13
  "test": "jest --forceExit",
13
14
  "test-local": "RABBITMQ_SERVICE_HOST=10.132.0.98:5672 jest --forceExit",
package/src/index.ts CHANGED
@@ -38,17 +38,14 @@ const defaultOptions = {
38
38
  const assertExchangeFanout = (c: any, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
39
39
 
40
40
  const callbackWithTimeout = async (callback: any, args: any[], timeout: number, timeoutMessage = 'Timeout while ack message') => {
41
- const callbackPromise = new bluebird.Promise(callback(...args));
42
- Promise.race([
43
- callbackPromise,
44
- new Promise((resolve, reject) => {
45
- setTimeout(() => {
46
- callbackPromise.cancel();
47
- console.log('callbackPromise', callbackPromise.isCancelled());
48
- reject(timeoutMessage);
49
- }, timeout);
50
- }),
51
- ]);
41
+ await new bluebird.Promise(async (resolve, reject) => {
42
+ try {
43
+ await callback(...args);
44
+ return resolve();
45
+ } catch (e) {
46
+ return reject(e);
47
+ }
48
+ }).timeout(timeout, timeoutMessage);
52
49
  };
53
50
 
54
51
  const connectionCreatedConst = 'connectionCreated';
@@ -93,13 +90,16 @@ class RabbitMq implements IAfRabbitMq {
93
90
 
94
91
  options: AfRabbitOptions | undefined;
95
92
 
96
- constructor(options?: AfRabbitOptions) {
93
+ logger: any;
94
+
95
+ constructor({ options, logger }: { options?: AfRabbitOptions, logger?: any} = {}) {
97
96
  this.em = new EventEmitter();
98
97
  this.channel = null;
99
98
  this.connection = null;
100
99
  this.creatingConnection = false;
101
100
  this.exchanges = {};
102
101
  this.options = options;
102
+ this.logger = logger;
103
103
  }
104
104
 
105
105
  public ack = (channel: ChannelWrapper) => async (msg: any) => {
@@ -124,7 +124,6 @@ class RabbitMq implements IAfRabbitMq {
124
124
  || msg.content['x-retry-count'] < options.retries)
125
125
  ) {
126
126
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
127
- // msg.content.nackCount = msg.content.nackCount ? msg.content.nackCount + 1 : 1;
128
127
  await this.sendToQueue(queue, msg.content);
129
128
  } else {
130
129
  const deadQueue = `${queue}-dead`;
@@ -240,6 +239,10 @@ class RabbitMq implements IAfRabbitMq {
240
239
  try {
241
240
  await callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
242
241
  } catch (e) {
242
+ this.logger?.error('message nacked because of error in callbackWithTimeout', {
243
+ error: e,
244
+ msg,
245
+ });
243
246
  this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
244
247
  }
245
248
  } else {