@autofleet/rabbit 1.5.7 → 1.6.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 +8 -3
- package/dist/index.js +26 -11
- package/package.json +1 -1
- package/src/index.ts +31 -11
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ import { EventEmitter } from 'events';
|
|
|
5
5
|
interface ExchangesCache {
|
|
6
6
|
[key: string]: any;
|
|
7
7
|
}
|
|
8
|
+
export interface AfRabbitOptions {
|
|
9
|
+
reconnect: boolean;
|
|
10
|
+
}
|
|
8
11
|
declare class RabbitMq {
|
|
9
12
|
static parseMsg(msg: any): any;
|
|
10
13
|
static validateName(type: string, name: string): void;
|
|
@@ -15,14 +18,16 @@ declare class RabbitMq {
|
|
|
15
18
|
PUBLISH_TIMEOUT: number;
|
|
16
19
|
PUBLISH_ERROR_MSG: string;
|
|
17
20
|
DISCONNECT_MSG: string;
|
|
21
|
+
RESCONNECT_MSG: string;
|
|
18
22
|
channel: ChannelWrapper | null;
|
|
19
23
|
connection: AmqpConnectionManager | null;
|
|
20
24
|
em: EventEmitter;
|
|
21
25
|
creatingConnection: boolean;
|
|
22
26
|
exchanges: ExchangesCache;
|
|
23
|
-
|
|
27
|
+
options: AfRabbitOptions | undefined;
|
|
28
|
+
constructor(options?: AfRabbitOptions);
|
|
24
29
|
ack: (msg: any) => Promise<void>;
|
|
25
|
-
nack: (queue: string,
|
|
30
|
+
nack: (queue: string, deadQueueOptions?: Options.AssertQueue | undefined) => (msg: any, retries: number) => Promise<void>;
|
|
26
31
|
getConnection(): Promise<AmqpConnectionManager>;
|
|
27
32
|
assertChannel(): Promise<ChannelWrapper>;
|
|
28
33
|
assertExchange(exchangeName: string, options?: any): Promise<any>;
|
|
@@ -32,6 +37,6 @@ declare class RabbitMq {
|
|
|
32
37
|
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
33
38
|
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
34
39
|
publish(exchange: string, content: any): Promise<unknown>;
|
|
35
|
-
sendToQueue(queue: string, content: any): Promise<void>;
|
|
40
|
+
sendToQueue(queue: string, content: any, options?: any): Promise<void>;
|
|
36
41
|
}
|
|
37
42
|
export default RabbitMq;
|
package/dist/index.js
CHANGED
|
@@ -41,16 +41,17 @@ const rabbitError_1 = __importDefault(require("./rabbitError"));
|
|
|
41
41
|
const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
|
|
42
42
|
const connectionCreatedConst = 'connectionCreated';
|
|
43
43
|
class RabbitMq {
|
|
44
|
-
constructor() {
|
|
44
|
+
constructor(options) {
|
|
45
45
|
this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
|
|
46
46
|
this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
|
|
47
47
|
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
48
|
+
this.RESCONNECT_MSG = 'rabbit: reconnecting';
|
|
48
49
|
this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
49
50
|
if (this.channel) {
|
|
50
51
|
yield this.channel.ack(msg);
|
|
51
52
|
}
|
|
52
53
|
});
|
|
53
|
-
this.nack = (queue,
|
|
54
|
+
this.nack = (queue, deadQueueOptions) => (msg, retries) => __awaiter(this, void 0, void 0, function* () {
|
|
54
55
|
if (this.channel) {
|
|
55
56
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
56
57
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
@@ -58,8 +59,8 @@ class RabbitMq {
|
|
|
58
59
|
}
|
|
59
60
|
else {
|
|
60
61
|
const deadQueue = `${queue}-dead`;
|
|
61
|
-
yield this.assertQueue(deadQueue,
|
|
62
|
-
yield this.sendToQueue(deadQueue, msg.content);
|
|
62
|
+
yield this.assertQueue(deadQueue, deadQueueOptions);
|
|
63
|
+
yield this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
|
|
63
64
|
}
|
|
64
65
|
yield this.channel.ack(msg);
|
|
65
66
|
}
|
|
@@ -69,6 +70,7 @@ class RabbitMq {
|
|
|
69
70
|
this.connection = null;
|
|
70
71
|
this.creatingConnection = false;
|
|
71
72
|
this.exchanges = {};
|
|
73
|
+
this.options = options;
|
|
72
74
|
}
|
|
73
75
|
static parseMsg(msg) {
|
|
74
76
|
let { content } = msg;
|
|
@@ -101,7 +103,16 @@ class RabbitMq {
|
|
|
101
103
|
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
102
104
|
const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
103
105
|
this.connection = connection;
|
|
104
|
-
this.connection.on('disconnect', ({ err }) =>
|
|
106
|
+
this.connection.on('disconnect', ({ err }) => {
|
|
107
|
+
var _a;
|
|
108
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.reconnect) {
|
|
109
|
+
console.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
|
|
110
|
+
this.connection = null;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
105
116
|
this.creatingConnection = false;
|
|
106
117
|
this.em.emit(connectionCreatedConst, connection);
|
|
107
118
|
resolve(connection);
|
|
@@ -172,8 +183,10 @@ class RabbitMq {
|
|
|
172
183
|
yield c.prefetch(limit, true);
|
|
173
184
|
return Promise.all([
|
|
174
185
|
c.consume(queue, (msg) => {
|
|
175
|
-
|
|
176
|
-
|
|
186
|
+
setImmediate(() => {
|
|
187
|
+
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
188
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, { messageTtl: deadMessageTtl }));
|
|
189
|
+
});
|
|
177
190
|
}),
|
|
178
191
|
]);
|
|
179
192
|
}));
|
|
@@ -193,8 +206,10 @@ class RabbitMq {
|
|
|
193
206
|
return Promise.all([
|
|
194
207
|
c.bindQueue(queue, exchange, ''),
|
|
195
208
|
c.consume(queue, (msg) => {
|
|
196
|
-
|
|
197
|
-
|
|
209
|
+
setImmediate(() => {
|
|
210
|
+
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
211
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, { messageTtl: deadMessageTtl }));
|
|
212
|
+
});
|
|
198
213
|
}),
|
|
199
214
|
]);
|
|
200
215
|
}));
|
|
@@ -211,11 +226,11 @@ class RabbitMq {
|
|
|
211
226
|
})).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
|
|
212
227
|
});
|
|
213
228
|
}
|
|
214
|
-
sendToQueue(queue, content) {
|
|
229
|
+
sendToQueue(queue, content, options) {
|
|
215
230
|
return __awaiter(this, void 0, void 0, function* () {
|
|
216
231
|
RabbitMq.validateName('queue', queue);
|
|
217
232
|
const channel = yield this.assertChannel();
|
|
218
|
-
yield this.assertQueue(queue);
|
|
233
|
+
yield this.assertQueue(queue, options);
|
|
219
234
|
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
220
235
|
});
|
|
221
236
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,6 +11,10 @@ interface ExchangesCache {
|
|
|
11
11
|
[key: string]: any
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
export interface AfRabbitOptions {
|
|
15
|
+
reconnect: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
const assertExchangeFanout = (c: any, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
|
|
15
19
|
|
|
16
20
|
const connectionCreatedConst = 'connectionCreated';
|
|
@@ -49,6 +53,8 @@ class RabbitMq {
|
|
|
49
53
|
|
|
50
54
|
DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
51
55
|
|
|
56
|
+
RESCONNECT_MSG = 'rabbit: reconnecting';
|
|
57
|
+
|
|
52
58
|
channel: ChannelWrapper | null;
|
|
53
59
|
|
|
54
60
|
connection: AmqpConnectionManager | null;
|
|
@@ -59,12 +65,15 @@ class RabbitMq {
|
|
|
59
65
|
|
|
60
66
|
exchanges: ExchangesCache;
|
|
61
67
|
|
|
62
|
-
|
|
68
|
+
options: AfRabbitOptions | undefined;
|
|
69
|
+
|
|
70
|
+
constructor(options?: AfRabbitOptions) {
|
|
63
71
|
this.em = new EventEmitter();
|
|
64
72
|
this.channel = null;
|
|
65
73
|
this.connection = null;
|
|
66
74
|
this.creatingConnection = false;
|
|
67
75
|
this.exchanges = {};
|
|
76
|
+
this.options = options;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
79
|
public ack = async (msg: any) => {
|
|
@@ -73,15 +82,15 @@ class RabbitMq {
|
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
84
|
|
|
76
|
-
public nack = (queue: string,
|
|
85
|
+
public nack = (queue: string, deadQueueOptions?: Options.AssertQueue) => async (msg: any, retries: number) => {
|
|
77
86
|
if (this.channel) {
|
|
78
87
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
79
88
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
80
89
|
await this.sendToQueue(queue, msg.content);
|
|
81
90
|
} else {
|
|
82
91
|
const deadQueue = `${queue}-dead`;
|
|
83
|
-
await this.assertQueue(deadQueue,
|
|
84
|
-
await this.sendToQueue(deadQueue, msg.content);
|
|
92
|
+
await this.assertQueue(deadQueue, deadQueueOptions);
|
|
93
|
+
await this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
|
|
85
94
|
}
|
|
86
95
|
await this.channel.ack(msg);
|
|
87
96
|
}
|
|
@@ -101,7 +110,14 @@ class RabbitMq {
|
|
|
101
110
|
const connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
|
|
102
111
|
this.connection = connection;
|
|
103
112
|
this.connection.on('disconnect',
|
|
104
|
-
({ err }) =>
|
|
113
|
+
({ err }) => {
|
|
114
|
+
if (this.options?.reconnect) {
|
|
115
|
+
console.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
|
|
116
|
+
this.connection = null;
|
|
117
|
+
} else {
|
|
118
|
+
console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
105
121
|
this.creatingConnection = false;
|
|
106
122
|
this.em.emit(connectionCreatedConst, connection);
|
|
107
123
|
resolve(connection);
|
|
@@ -169,8 +185,10 @@ class RabbitMq {
|
|
|
169
185
|
c.consume(
|
|
170
186
|
queue,
|
|
171
187
|
(msg: any) => {
|
|
172
|
-
|
|
173
|
-
|
|
188
|
+
setImmediate(() => {
|
|
189
|
+
newTrace(traceTypes.RABBIT);
|
|
190
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, { messageTtl: deadMessageTtl }));
|
|
191
|
+
})
|
|
174
192
|
},
|
|
175
193
|
),
|
|
176
194
|
]);
|
|
@@ -193,8 +211,10 @@ class RabbitMq {
|
|
|
193
211
|
c.consume(
|
|
194
212
|
queue,
|
|
195
213
|
(msg: any) => {
|
|
196
|
-
|
|
197
|
-
|
|
214
|
+
setImmediate(() => {
|
|
215
|
+
newTrace(traceTypes.RABBIT);
|
|
216
|
+
callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, { messageTtl: deadMessageTtl }));
|
|
217
|
+
})
|
|
198
218
|
},
|
|
199
219
|
),
|
|
200
220
|
]);
|
|
@@ -211,10 +231,10 @@ class RabbitMq {
|
|
|
211
231
|
}).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
|
|
212
232
|
}
|
|
213
233
|
|
|
214
|
-
async sendToQueue(queue: string, content: any) {
|
|
234
|
+
async sendToQueue(queue: string, content: any, options?: any) {
|
|
215
235
|
RabbitMq.validateName('queue', queue);
|
|
216
236
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
217
|
-
await this.assertQueue(queue);
|
|
237
|
+
await this.assertQueue(queue, options);
|
|
218
238
|
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
219
239
|
}
|
|
220
240
|
}
|