@autofleet/rabbit 2.0.4-beta5 → 2.1.0

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
@@ -22,6 +22,9 @@ export interface AfRabbitOptions {
22
22
  declare class RabbitMq implements IAfRabbitMq {
23
23
  static parseMsg(msg: any): any;
24
24
  static validateName(type: string, name: string): void;
25
+ static getPublishOptions(): {
26
+ timestamp: number;
27
+ };
25
28
  PUBLISH_TIMEOUT: number;
26
29
  PUBLISH_ERROR_MSG: string;
27
30
  DISCONNECT_MSG: string;
@@ -32,11 +35,7 @@ declare class RabbitMq implements IAfRabbitMq {
32
35
  creatingConnection: boolean;
33
36
  exchanges: ExchangesCache;
34
37
  options: AfRabbitOptions | undefined;
35
- logger: any;
36
- constructor({ options, logger }?: {
37
- options?: AfRabbitOptions;
38
- logger?: any;
39
- });
38
+ constructor(options?: AfRabbitOptions);
40
39
  ack: (channel: ChannelWrapper) => (msg: any) => Promise<void>;
41
40
  nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
42
41
  getConnection(): Promise<AmqpConnectionManager>;
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
34
34
  /* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/ban-types,@typescript-eslint/no-unused-vars,consistent-return,no-async-promise-executor,no-param-reassign,no-empty */
35
35
  // eslint-disable-next-line max-classes-per-file
36
36
  const bluebird = __importStar(require("bluebird"));
37
+ const moment_1 = __importDefault(require("moment"));
37
38
  const amqp_connection_manager_1 = require("amqp-connection-manager");
38
39
  const events_1 = require("events");
39
40
  const outbreak_1 = require("@autofleet/outbreak");
@@ -45,20 +46,9 @@ const defaultOptions = {
45
46
  deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
46
47
  };
47
48
  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
- });
59
49
  const connectionCreatedConst = 'connectionCreated';
60
50
  class RabbitMq {
61
- constructor({ options, logger } = {}) {
51
+ constructor(options) {
62
52
  this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
63
53
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
64
54
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
@@ -88,7 +78,6 @@ class RabbitMq {
88
78
  this.creatingConnection = false;
89
79
  this.exchanges = {};
90
80
  this.options = options;
91
- this.logger = logger;
92
81
  }
93
82
  static parseMsg(msg) {
94
83
  let { content } = msg;
@@ -104,6 +93,11 @@ class RabbitMq {
104
93
  throw new rabbitError_1.default(`error while using ${type} with no name`);
105
94
  }
106
95
  }
96
+ static getPublishOptions() {
97
+ return {
98
+ timestamp: moment_1.default().unix() * 1000,
99
+ };
100
+ }
107
101
  getConnection() {
108
102
  return __awaiter(this, void 0, void 0, function* () {
109
103
  return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
@@ -115,8 +109,10 @@ class RabbitMq {
115
109
  return resolve(this.connection);
116
110
  }
117
111
  this.creatingConnection = true;
112
+ const username = process.env.RABBITMQ_USERNAME || 'guest';
113
+ const password = process.env.RABBITMQ_PASSWORD || 'guest';
118
114
  const host = process.env.RABBITMQ_SERVICE_HOST || '';
119
- const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
115
+ const connection = yield amqp_connection_manager_1.connect([`amqp://${username}:${password}@${host}`]);
120
116
  this.connection = connection;
121
117
  this.connection.on('disconnect', ({ err }) => {
122
118
  var _a;
@@ -204,30 +200,10 @@ class RabbitMq {
204
200
  yield c.assertQueue(queue);
205
201
  yield c.prefetch(limit, true);
206
202
  return Promise.all([
207
- c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
208
- var _a;
203
+ c.consume(queue, (msg) => {
209
204
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
210
- if (optionsWithDefaults.timeout) {
211
- const args = [
212
- RabbitMq.parseMsg(msg),
213
- this.ack(channel),
214
- this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
215
- ];
216
- try {
217
- yield callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
218
- }
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
- });
224
- this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
225
- }
226
- }
227
- else {
228
- callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
229
- }
230
- })),
205
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
206
+ }),
231
207
  ]);
232
208
  }));
233
209
  });
@@ -261,7 +237,7 @@ class RabbitMq {
261
237
  yield this.assertExchange(exchange);
262
238
  return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
263
239
  try {
264
- yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
240
+ yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
265
241
  return resolve();
266
242
  }
267
243
  catch (e) {
@@ -275,7 +251,7 @@ class RabbitMq {
275
251
  RabbitMq.validateName('queue', queue);
276
252
  const channel = yield this.assertChannel();
277
253
  yield this.assertQueue(queue, options);
278
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
254
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
279
255
  });
280
256
  }
281
257
  }
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.0.4-beta5",
3
+ "version": "2.1.0",
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": "rm -rf dist && tsc",
11
- "prepublish": "npm run build",
10
+ "build": "tsc",
12
11
  "linter": "./node_modules/.bin/eslint .",
13
12
  "test": "jest --forceExit",
14
13
  "test-local": "RABBITMQ_SERVICE_HOST=10.132.0.98:5672 jest --forceExit",
@@ -20,7 +19,8 @@
20
19
  "@types/amqplib": "^0.5.16",
21
20
  "amqp-connection-manager": "^3.2.1",
22
21
  "amqplib": "^0.6.0",
23
- "bluebird": "^3.7.2"
22
+ "bluebird": "^3.7.2",
23
+ "moment": "^2.29.1"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@autofleet/outbreak": "*",
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/ban-types,@typescript-eslint/no-unused-vars,consistent-return,no-async-promise-executor,no-param-reassign,no-empty */
2
2
  // eslint-disable-next-line max-classes-per-file
3
3
  import * as bluebird from 'bluebird';
4
+ import moment from 'moment';
4
5
  import { ConfirmChannel, Options } from 'amqplib';
5
6
  import { AmqpConnectionManager, ChannelWrapper, connect } from 'amqp-connection-manager';
6
7
  import { EventEmitter } from 'events';
@@ -37,17 +38,6 @@ const defaultOptions = {
37
38
 
38
39
  const assertExchangeFanout = (c: any, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
39
40
 
40
- const callbackWithTimeout = async (callback: any, args: any[], timeout: number, timeoutMessage = 'Timeout while ack message') => {
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);
49
- };
50
-
51
41
  const connectionCreatedConst = 'connectionCreated';
52
42
  class RabbitMq implements IAfRabbitMq {
53
43
  static parseMsg(msg: any) {
@@ -70,6 +60,12 @@ class RabbitMq implements IAfRabbitMq {
70
60
  }
71
61
  }
72
62
 
63
+ static getPublishOptions() {
64
+ return {
65
+ timestamp: moment().unix() * 1000,
66
+ };
67
+ }
68
+
73
69
  PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
74
70
 
75
71
  PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
@@ -90,16 +86,13 @@ class RabbitMq implements IAfRabbitMq {
90
86
 
91
87
  options: AfRabbitOptions | undefined;
92
88
 
93
- logger: any;
94
-
95
- constructor({ options, logger }: { options?: AfRabbitOptions, logger?: any} = {}) {
89
+ constructor(options?: AfRabbitOptions) {
96
90
  this.em = new EventEmitter();
97
91
  this.channel = null;
98
92
  this.connection = null;
99
93
  this.creatingConnection = false;
100
94
  this.exchanges = {};
101
95
  this.options = options;
102
- this.logger = logger;
103
96
  }
104
97
 
105
98
  public ack = (channel: ChannelWrapper) => async (msg: any) => {
@@ -144,8 +137,10 @@ class RabbitMq implements IAfRabbitMq {
144
137
  return resolve(this.connection);
145
138
  }
146
139
  this.creatingConnection = true;
140
+ const username: string = process.env.RABBITMQ_USERNAME || 'guest';
141
+ const password: string = process.env.RABBITMQ_PASSWORD || 'guest';
147
142
  const host: string = process.env.RABBITMQ_SERVICE_HOST || '';
148
- const connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
143
+ const connection: AmqpConnectionManager = await connect([`amqp://${username}:${password}@${host}`]);
149
144
  this.connection = connection;
150
145
  this.connection.on('disconnect',
151
146
  ({ err }) => {
@@ -228,26 +223,9 @@ class RabbitMq implements IAfRabbitMq {
228
223
  return Promise.all([
229
224
  c.consume(
230
225
  queue,
231
- async (msg: any) => {
226
+ (msg: any) => {
232
227
  newTrace(traceTypes.RABBIT);
233
- if (optionsWithDefaults.timeout) {
234
- const args = [
235
- RabbitMq.parseMsg(msg),
236
- this.ack(channel),
237
- this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
238
- ];
239
- try {
240
- await callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
241
- } catch (e) {
242
- this.logger?.error('message nacked because of error in callbackWithTimeout', {
243
- error: e,
244
- msg,
245
- });
246
- this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
247
- }
248
- } else {
249
- callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
250
- }
228
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
251
229
  },
252
230
  ),
253
231
  ]);
@@ -285,7 +263,7 @@ class RabbitMq implements IAfRabbitMq {
285
263
  await this.assertExchange(exchange);
286
264
  return new bluebird.Promise(async (resolve, reject) => {
287
265
  try {
288
- await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
266
+ await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
289
267
  return resolve();
290
268
  } catch (e) {
291
269
  reject(e);
@@ -297,7 +275,7 @@ class RabbitMq implements IAfRabbitMq {
297
275
  RabbitMq.validateName('queue', queue);
298
276
  const channel: ChannelWrapper = await this.assertChannel();
299
277
  await this.assertQueue(queue, options);
300
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
278
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
301
279
  }
302
280
  }
303
281