@autofleet/rabbit 2.1.8-beta10 → 2.1.8-beta3
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 +8 -31
- package/dist/index.js +26 -82
- package/dist/mock.d.ts +10 -11
- package/dist/mock.js +0 -1
- package/package.json +1 -1
- package/src/index.ts +1 -9
- package/dist/logger.d.ts +0 -2
- package/dist/logger.js +0 -5
- package/dist/redis.d.ts +0 -7
- package/dist/redis.js +0 -11
package/dist/index.d.ts
CHANGED
|
@@ -2,35 +2,15 @@
|
|
|
2
2
|
import { Options } from 'amqplib';
|
|
3
3
|
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
|
-
import { RedisConfig } from './redis';
|
|
6
|
-
export interface IAfRabbitMq {
|
|
7
|
-
ack: any;
|
|
8
|
-
nack: any;
|
|
9
|
-
assertChannel: any;
|
|
10
|
-
assertExchange: any;
|
|
11
|
-
assertQueue: any;
|
|
12
|
-
consume: any;
|
|
13
|
-
consumeFromExchange: any;
|
|
14
|
-
publish: any;
|
|
15
|
-
sendToQueue: any;
|
|
16
|
-
redisClient?: any;
|
|
17
|
-
}
|
|
18
5
|
interface ExchangesCache {
|
|
19
6
|
[key: string]: any;
|
|
20
7
|
}
|
|
21
|
-
declare type CustomMessageHeaders = {
|
|
22
|
-
redisTimestampValidationKey?: string;
|
|
23
|
-
};
|
|
24
8
|
export interface AfRabbitOptions {
|
|
25
9
|
reconnect: boolean;
|
|
26
10
|
}
|
|
27
|
-
declare class RabbitMq
|
|
11
|
+
declare class RabbitMq {
|
|
28
12
|
static parseMsg(msg: any): any;
|
|
29
13
|
static validateName(type: string, name: string): void;
|
|
30
|
-
static getPublishOptions(customHeaders?: CustomMessageHeaders): {
|
|
31
|
-
timestamp: number;
|
|
32
|
-
headers: CustomMessageHeaders;
|
|
33
|
-
};
|
|
34
14
|
PUBLISH_TIMEOUT: number;
|
|
35
15
|
PUBLISH_ERROR_MSG: string;
|
|
36
16
|
DISCONNECT_MSG: string;
|
|
@@ -41,22 +21,19 @@ declare class RabbitMq implements IAfRabbitMq {
|
|
|
41
21
|
creatingConnection: boolean;
|
|
42
22
|
exchanges: ExchangesCache;
|
|
43
23
|
options: AfRabbitOptions | undefined;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
ack: (channel: ChannelWrapper, shouldUpdateRedisTimestamp?: boolean) => (msg: any) => Promise<void>;
|
|
48
|
-
nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
|
|
24
|
+
constructor(options?: AfRabbitOptions);
|
|
25
|
+
ack: (msg: any) => Promise<void>;
|
|
26
|
+
nack: (queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
|
|
49
27
|
getConnection(): Promise<AmqpConnectionManager>;
|
|
50
28
|
getNewChannel(): Promise<ChannelWrapper>;
|
|
51
29
|
assertChannel(): Promise<ChannelWrapper>;
|
|
52
30
|
assertExchange(exchangeName: string, options?: any): Promise<any>;
|
|
53
|
-
getQueueLength(queue: string): Promise<
|
|
54
|
-
bindQueue(queue: string, exchange: string): Promise<
|
|
31
|
+
getQueueLength(queue: string): Promise<any>;
|
|
32
|
+
bindQueue(queue: string, exchange: string): Promise<any>;
|
|
55
33
|
assertQueue(queue: string, options?: Options.AssertQueue): Promise<void>;
|
|
56
34
|
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
57
35
|
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
58
|
-
publish(exchange: string, content: any
|
|
59
|
-
sendToQueue(queue: string, content: any, options?: any
|
|
60
|
-
isConnected(): Promise<boolean>;
|
|
36
|
+
publish(exchange: string, content: any): Promise<unknown>;
|
|
37
|
+
sendToQueue(queue: string, content: any, options?: any): Promise<void>;
|
|
61
38
|
}
|
|
62
39
|
export default RabbitMq;
|
package/dist/index.js
CHANGED
|
@@ -34,49 +34,31 @@ 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"));
|
|
38
37
|
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
39
38
|
const events_1 = require("events");
|
|
40
|
-
|
|
41
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
39
|
+
const outbreak_1 = require("@autofleet/outbreak");
|
|
42
40
|
const rabbitError_1 = __importDefault(require("./rabbitError"));
|
|
43
|
-
const redis_1 = __importDefault(require("./redis"));
|
|
44
41
|
const DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 48;
|
|
45
42
|
const defaultOptions = {
|
|
46
43
|
limit: 1,
|
|
47
44
|
retries: 1,
|
|
48
45
|
deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
|
|
49
46
|
};
|
|
50
|
-
const SECONDS_TO_MILLISECONDS = 1000;
|
|
51
47
|
const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
|
|
52
48
|
const connectionCreatedConst = 'connectionCreated';
|
|
53
49
|
class RabbitMq {
|
|
54
|
-
constructor(options
|
|
50
|
+
constructor(options) {
|
|
55
51
|
this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
|
|
56
52
|
this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
|
|
57
53
|
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
58
54
|
this.RESCONNECT_MSG = 'rabbit: reconnecting';
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const lastMessageTimestamp = yield this.redisClient.getAsync(headers.redisTimestampValidationKey);
|
|
63
|
-
logger_1.default.info('rabbit: checking if should consume params', {
|
|
64
|
-
msg, timestamp, headers, lastMessageTimestamp,
|
|
65
|
-
});
|
|
66
|
-
return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
|
|
67
|
-
}
|
|
68
|
-
return true;
|
|
69
|
-
});
|
|
70
|
-
this.ack = (channel, shouldUpdateRedisTimestamp = false) => (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
yield channel.ack(msg);
|
|
72
|
-
const { properties: { timestamp, headers } } = msg;
|
|
73
|
-
if (shouldUpdateRedisTimestamp && timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
|
|
74
|
-
const parsedTimestamp = parseInt(timestamp, 10);
|
|
75
|
-
yield this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
|
|
55
|
+
this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
if (this.channel) {
|
|
57
|
+
yield this.channel.ack(msg);
|
|
76
58
|
}
|
|
77
59
|
});
|
|
78
|
-
this.nack = (
|
|
79
|
-
if (channel) {
|
|
60
|
+
this.nack = (queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
if (this.channel) {
|
|
80
62
|
if (!skipRetry
|
|
81
63
|
&& (!msg.content['x-retry-count']
|
|
82
64
|
|| msg.content['x-retry-count'] < options.retries)) {
|
|
@@ -88,7 +70,7 @@ class RabbitMq {
|
|
|
88
70
|
yield this.assertQueue(deadQueue, deadQueueOptions);
|
|
89
71
|
yield this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
|
|
90
72
|
}
|
|
91
|
-
yield channel.ack(msg);
|
|
73
|
+
yield this.channel.ack(msg);
|
|
92
74
|
}
|
|
93
75
|
});
|
|
94
76
|
this.em = new events_1.EventEmitter();
|
|
@@ -97,7 +79,6 @@ class RabbitMq {
|
|
|
97
79
|
this.creatingConnection = false;
|
|
98
80
|
this.exchanges = {};
|
|
99
81
|
this.options = options;
|
|
100
|
-
this.redisClient = redisConfig && redis_1.default(redisConfig);
|
|
101
82
|
}
|
|
102
83
|
static parseMsg(msg) {
|
|
103
84
|
let { content } = msg;
|
|
@@ -113,27 +94,19 @@ class RabbitMq {
|
|
|
113
94
|
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
114
95
|
}
|
|
115
96
|
}
|
|
116
|
-
static getPublishOptions(customHeaders = {}) {
|
|
117
|
-
return {
|
|
118
|
-
timestamp: moment_1.default().unix(),
|
|
119
|
-
headers: customHeaders,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
97
|
getConnection() {
|
|
123
98
|
return __awaiter(this, void 0, void 0, function* () {
|
|
124
99
|
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
125
100
|
if (this.creatingConnection) {
|
|
126
|
-
this.em.
|
|
101
|
+
this.em.on(connectionCreatedConst, resolve);
|
|
127
102
|
return;
|
|
128
103
|
}
|
|
129
104
|
if (this.connection !== null) {
|
|
130
|
-
return
|
|
105
|
+
return this.connection;
|
|
131
106
|
}
|
|
132
107
|
this.creatingConnection = true;
|
|
133
|
-
const username = process.env.RABBITMQ_USERNAME || 'guest';
|
|
134
|
-
const password = process.env.RABBITMQ_PASSWORD || 'guest';
|
|
135
108
|
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
136
|
-
const connection = yield amqp_connection_manager_1.connect([`amqp://${
|
|
109
|
+
const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
137
110
|
this.connection = connection;
|
|
138
111
|
this.connection.on('disconnect', ({ err }) => {
|
|
139
112
|
var _a;
|
|
@@ -163,8 +136,8 @@ class RabbitMq {
|
|
|
163
136
|
if (this.channel) {
|
|
164
137
|
return resolve(this.channel);
|
|
165
138
|
}
|
|
139
|
+
const connection = yield this.getConnection();
|
|
166
140
|
try {
|
|
167
|
-
const connection = yield this.getConnection();
|
|
168
141
|
if (this.channel === null) {
|
|
169
142
|
this.channel = connection.createChannel({});
|
|
170
143
|
}
|
|
@@ -221,21 +194,10 @@ class RabbitMq {
|
|
|
221
194
|
yield c.assertQueue(queue);
|
|
222
195
|
yield c.prefetch(limit, true);
|
|
223
196
|
return Promise.all([
|
|
224
|
-
c.consume(queue, (msg) =>
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
logger_1.default.info('rabbit: trying to consume', { queue, shouldConsume, msg });
|
|
229
|
-
if (!shouldConsume) {
|
|
230
|
-
return this.ack(channel)(msg);
|
|
231
|
-
}
|
|
232
|
-
try {
|
|
233
|
-
yield callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
234
|
-
}
|
|
235
|
-
catch (e) {
|
|
236
|
-
yield this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
|
|
237
|
-
}
|
|
238
|
-
})),
|
|
197
|
+
c.consume(queue, (msg) => {
|
|
198
|
+
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
199
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
200
|
+
}),
|
|
239
201
|
]);
|
|
240
202
|
}));
|
|
241
203
|
});
|
|
@@ -254,49 +216,31 @@ class RabbitMq {
|
|
|
254
216
|
yield c.prefetch(limit, true);
|
|
255
217
|
return Promise.all([
|
|
256
218
|
c.bindQueue(queue, exchange, ''),
|
|
257
|
-
c.consume(queue, (msg) =>
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
logger_1.default.info('rabbit: trying to consume', { queue, shouldConsume, msg });
|
|
262
|
-
if (!shouldConsume) {
|
|
263
|
-
return this.ack(channel)(msg);
|
|
264
|
-
}
|
|
265
|
-
callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
266
|
-
})),
|
|
219
|
+
c.consume(queue, (msg) => {
|
|
220
|
+
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
221
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
222
|
+
}),
|
|
267
223
|
]);
|
|
268
224
|
}));
|
|
269
225
|
});
|
|
270
226
|
}
|
|
271
|
-
publish(exchange, content
|
|
227
|
+
publish(exchange, content) {
|
|
272
228
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
229
|
RabbitMq.validateName('exchange', exchange);
|
|
274
230
|
const channel = yield this.assertChannel();
|
|
275
231
|
yield this.assertExchange(exchange);
|
|
276
|
-
return new bluebird.Promise((resolve
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return resolve();
|
|
280
|
-
}
|
|
281
|
-
catch (e) {
|
|
282
|
-
reject(e);
|
|
283
|
-
}
|
|
232
|
+
return new bluebird.Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
233
|
+
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
234
|
+
return resolve();
|
|
284
235
|
})).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
|
|
285
236
|
});
|
|
286
237
|
}
|
|
287
|
-
sendToQueue(queue, content, options
|
|
238
|
+
sendToQueue(queue, content, options) {
|
|
288
239
|
return __awaiter(this, void 0, void 0, function* () {
|
|
289
240
|
RabbitMq.validateName('queue', queue);
|
|
290
241
|
const channel = yield this.assertChannel();
|
|
291
242
|
yield this.assertQueue(queue, options);
|
|
292
|
-
|
|
293
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
isConnected() {
|
|
297
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
298
|
-
const connection = yield this.getConnection();
|
|
299
|
-
return connection.isConnected();
|
|
243
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
300
244
|
});
|
|
301
245
|
}
|
|
302
246
|
}
|
package/dist/mock.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import 'jest';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
sendToQueue: any;
|
|
2
|
+
declare class RabbitMq {
|
|
3
|
+
ack: jest.Mock<unknown>;
|
|
4
|
+
nack: jest.Mock<unknown>;
|
|
5
|
+
assertChannel: jest.Mock<unknown>;
|
|
6
|
+
assertExchange: jest.Mock<unknown>;
|
|
7
|
+
assertQueue: jest.Mock<unknown>;
|
|
8
|
+
consume: jest.Mock<unknown>;
|
|
9
|
+
consumeFromExchange: jest.Mock<unknown>;
|
|
10
|
+
publish: jest.Mock<unknown>;
|
|
11
|
+
sendToQueue: jest.Mock<unknown>;
|
|
13
12
|
}
|
|
14
13
|
export default RabbitMq;
|
package/dist/mock.js
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -259,15 +259,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
259
259
|
if (!shouldConsume) {
|
|
260
260
|
return this.ack(channel)(msg);
|
|
261
261
|
}
|
|
262
|
-
|
|
263
|
-
await callback(
|
|
264
|
-
parsedMessage,
|
|
265
|
-
this.ack(channel, true),
|
|
266
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
|
|
267
|
-
);
|
|
268
|
-
} catch (e) {
|
|
269
|
-
await this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
|
|
270
|
-
}
|
|
262
|
+
callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
271
263
|
},
|
|
272
264
|
),
|
|
273
265
|
]);
|
package/dist/logger.d.ts
DELETED
package/dist/logger.js
DELETED
package/dist/redis.d.ts
DELETED
package/dist/redis.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const bluebird_1 = __importDefault(require("bluebird"));
|
|
7
|
-
const redis = require('redis');
|
|
8
|
-
bluebird_1.default.promisifyAll(redis.RedisClient.prototype);
|
|
9
|
-
bluebird_1.default.promisifyAll(redis.Multi.prototype);
|
|
10
|
-
const getRedisInstance = (config) => redis.createClient(config);
|
|
11
|
-
exports.default = getRedisInstance;
|