@autofleet/rabbit 2.0.4-beta5 → 2.0.4
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 +4 -5
- package/dist/index.js +12 -38
- package/package.json +4 -4
- package/src/index.ts +12 -36
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
|
-
|
|
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(
|
|
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* () {
|
|
@@ -204,30 +198,10 @@ class RabbitMq {
|
|
|
204
198
|
yield c.assertQueue(queue);
|
|
205
199
|
yield c.prefetch(limit, true);
|
|
206
200
|
return Promise.all([
|
|
207
|
-
c.consume(queue, (msg) =>
|
|
208
|
-
var _a;
|
|
201
|
+
c.consume(queue, (msg) => {
|
|
209
202
|
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
})),
|
|
203
|
+
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
204
|
+
}),
|
|
231
205
|
]);
|
|
232
206
|
}));
|
|
233
207
|
});
|
|
@@ -261,7 +235,7 @@ class RabbitMq {
|
|
|
261
235
|
yield this.assertExchange(exchange);
|
|
262
236
|
return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
263
237
|
try {
|
|
264
|
-
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
238
|
+
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
265
239
|
return resolve();
|
|
266
240
|
}
|
|
267
241
|
catch (e) {
|
|
@@ -275,7 +249,7 @@ class RabbitMq {
|
|
|
275
249
|
RabbitMq.validateName('queue', queue);
|
|
276
250
|
const channel = yield this.assertChannel();
|
|
277
251
|
yield this.assertQueue(queue, options);
|
|
278
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
252
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
279
253
|
});
|
|
280
254
|
}
|
|
281
255
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "2.0.4
|
|
3
|
+
"version": "2.0.4",
|
|
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": "
|
|
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
|
-
|
|
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) => {
|
|
@@ -228,26 +221,9 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
228
221
|
return Promise.all([
|
|
229
222
|
c.consume(
|
|
230
223
|
queue,
|
|
231
|
-
|
|
224
|
+
(msg: any) => {
|
|
232
225
|
newTrace(traceTypes.RABBIT);
|
|
233
|
-
|
|
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
|
-
}
|
|
226
|
+
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
251
227
|
},
|
|
252
228
|
),
|
|
253
229
|
]);
|
|
@@ -285,7 +261,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
285
261
|
await this.assertExchange(exchange);
|
|
286
262
|
return new bluebird.Promise(async (resolve, reject) => {
|
|
287
263
|
try {
|
|
288
|
-
await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
264
|
+
await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
289
265
|
return resolve();
|
|
290
266
|
} catch (e) {
|
|
291
267
|
reject(e);
|
|
@@ -297,7 +273,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
297
273
|
RabbitMq.validateName('queue', queue);
|
|
298
274
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
299
275
|
await this.assertQueue(queue, options);
|
|
300
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
276
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
301
277
|
}
|
|
302
278
|
}
|
|
303
279
|
|