@autofleet/rabbit 2.0.4-beta4 → 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 +3 -0
- package/dist/index.js +11 -34
- package/package.json +3 -2
- package/src/index.ts +11 -32
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;
|
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,19 +46,6 @@ 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
|
-
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
|
-
]);
|
|
60
|
-
});
|
|
61
49
|
const connectionCreatedConst = 'connectionCreated';
|
|
62
50
|
class RabbitMq {
|
|
63
51
|
constructor(options) {
|
|
@@ -74,7 +62,6 @@ class RabbitMq {
|
|
|
74
62
|
&& (!msg.content['x-retry-count']
|
|
75
63
|
|| msg.content['x-retry-count'] < options.retries)) {
|
|
76
64
|
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
65
|
yield this.sendToQueue(queue, msg.content);
|
|
79
66
|
}
|
|
80
67
|
else {
|
|
@@ -106,6 +93,11 @@ class RabbitMq {
|
|
|
106
93
|
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
107
94
|
}
|
|
108
95
|
}
|
|
96
|
+
static getPublishOptions() {
|
|
97
|
+
return {
|
|
98
|
+
timestamp: moment_1.default().unix() * 1000,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
109
101
|
getConnection() {
|
|
110
102
|
return __awaiter(this, void 0, void 0, function* () {
|
|
111
103
|
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -206,25 +198,10 @@ class RabbitMq {
|
|
|
206
198
|
yield c.assertQueue(queue);
|
|
207
199
|
yield c.prefetch(limit, true);
|
|
208
200
|
return Promise.all([
|
|
209
|
-
c.consume(queue, (msg) =>
|
|
201
|
+
c.consume(queue, (msg) => {
|
|
210
202
|
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
RabbitMq.parseMsg(msg),
|
|
214
|
-
this.ack(channel),
|
|
215
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
|
|
216
|
-
];
|
|
217
|
-
try {
|
|
218
|
-
yield callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
|
|
219
|
-
}
|
|
220
|
-
catch (e) {
|
|
221
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
226
|
-
}
|
|
227
|
-
})),
|
|
203
|
+
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
204
|
+
}),
|
|
228
205
|
]);
|
|
229
206
|
}));
|
|
230
207
|
});
|
|
@@ -258,7 +235,7 @@ class RabbitMq {
|
|
|
258
235
|
yield this.assertExchange(exchange);
|
|
259
236
|
return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
260
237
|
try {
|
|
261
|
-
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
238
|
+
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
262
239
|
return resolve();
|
|
263
240
|
}
|
|
264
241
|
catch (e) {
|
|
@@ -272,7 +249,7 @@ class RabbitMq {
|
|
|
272
249
|
RabbitMq.validateName('queue', queue);
|
|
273
250
|
const channel = yield this.assertChannel();
|
|
274
251
|
yield this.assertQueue(queue, options);
|
|
275
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
252
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
276
253
|
});
|
|
277
254
|
}
|
|
278
255
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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": {
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"@types/amqplib": "^0.5.16",
|
|
20
20
|
"amqp-connection-manager": "^3.2.1",
|
|
21
21
|
"amqplib": "^0.6.0",
|
|
22
|
-
"bluebird": "^3.7.2"
|
|
22
|
+
"bluebird": "^3.7.2",
|
|
23
|
+
"moment": "^2.29.1"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
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,20 +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
|
-
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
|
-
]);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
41
|
const connectionCreatedConst = 'connectionCreated';
|
|
55
42
|
class RabbitMq implements IAfRabbitMq {
|
|
56
43
|
static parseMsg(msg: any) {
|
|
@@ -73,6 +60,12 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
73
60
|
}
|
|
74
61
|
}
|
|
75
62
|
|
|
63
|
+
static getPublishOptions() {
|
|
64
|
+
return {
|
|
65
|
+
timestamp: moment().unix() * 1000,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
76
69
|
PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
|
|
77
70
|
|
|
78
71
|
PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
|
|
@@ -124,7 +117,6 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
124
117
|
|| msg.content['x-retry-count'] < options.retries)
|
|
125
118
|
) {
|
|
126
119
|
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
120
|
await this.sendToQueue(queue, msg.content);
|
|
129
121
|
} else {
|
|
130
122
|
const deadQueue = `${queue}-dead`;
|
|
@@ -229,22 +221,9 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
229
221
|
return Promise.all([
|
|
230
222
|
c.consume(
|
|
231
223
|
queue,
|
|
232
|
-
|
|
224
|
+
(msg: any) => {
|
|
233
225
|
newTrace(traceTypes.RABBIT);
|
|
234
|
-
|
|
235
|
-
const args = [
|
|
236
|
-
RabbitMq.parseMsg(msg),
|
|
237
|
-
this.ack(channel),
|
|
238
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
|
|
239
|
-
];
|
|
240
|
-
try {
|
|
241
|
-
await callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
|
|
242
|
-
} catch (e) {
|
|
243
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
|
|
244
|
-
}
|
|
245
|
-
} else {
|
|
246
|
-
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
247
|
-
}
|
|
226
|
+
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
248
227
|
},
|
|
249
228
|
),
|
|
250
229
|
]);
|
|
@@ -282,7 +261,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
282
261
|
await this.assertExchange(exchange);
|
|
283
262
|
return new bluebird.Promise(async (resolve, reject) => {
|
|
284
263
|
try {
|
|
285
|
-
await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
264
|
+
await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
286
265
|
return resolve();
|
|
287
266
|
} catch (e) {
|
|
288
267
|
reject(e);
|
|
@@ -294,7 +273,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
294
273
|
RabbitMq.validateName('queue', queue);
|
|
295
274
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
296
275
|
await this.assertQueue(queue, options);
|
|
297
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
276
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions());
|
|
298
277
|
}
|
|
299
278
|
}
|
|
300
279
|
|