@autofleet/rabbit 1.5.7-1 → 1.5.7-2
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/.idea/workspace.xml +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +13 -2
- package/package.json +1 -1
- package/src/index.ts +18 -2
package/.idea/workspace.xml
CHANGED
|
@@ -204,7 +204,7 @@
|
|
|
204
204
|
<workItem from="1609106672294" duration="594000" />
|
|
205
205
|
<workItem from="1609146731453" duration="21000" />
|
|
206
206
|
<workItem from="1609193609209" duration="1486000" />
|
|
207
|
-
<workItem from="1609629219943" duration="
|
|
207
|
+
<workItem from="1609629219943" duration="5228000" />
|
|
208
208
|
</task>
|
|
209
209
|
<task id="LOCAL-00001" summary="validate name before usage">
|
|
210
210
|
<created>1606375684964</created>
|
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,12 +18,14 @@ 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
30
|
nack: (queue: string, options?: Options.AssertQueue | undefined) => (msg: any, retries: number) => Promise<void>;
|
|
26
31
|
getConnection(): Promise<AmqpConnectionManager>;
|
package/dist/index.js
CHANGED
|
@@ -41,10 +41,11 @@ 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);
|
|
@@ -72,6 +73,7 @@ class RabbitMq {
|
|
|
72
73
|
this.connection = null;
|
|
73
74
|
this.creatingConnection = false;
|
|
74
75
|
this.exchanges = {};
|
|
76
|
+
this.options = options;
|
|
75
77
|
}
|
|
76
78
|
static parseMsg(msg) {
|
|
77
79
|
let { content } = msg;
|
|
@@ -104,7 +106,16 @@ class RabbitMq {
|
|
|
104
106
|
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
105
107
|
const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
106
108
|
this.connection = connection;
|
|
107
|
-
this.connection.on('disconnect', ({ err }) =>
|
|
109
|
+
this.connection.on('disconnect', ({ err }) => {
|
|
110
|
+
var _a;
|
|
111
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.reconnect) {
|
|
112
|
+
console.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
|
|
113
|
+
this.connection = null;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
108
119
|
this.creatingConnection = false;
|
|
109
120
|
this.em.emit(connectionCreatedConst, connection);
|
|
110
121
|
resolve(connection);
|
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) => {
|
|
@@ -104,7 +113,14 @@ class RabbitMq {
|
|
|
104
113
|
const connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
|
|
105
114
|
this.connection = connection;
|
|
106
115
|
this.connection.on('disconnect',
|
|
107
|
-
({ err }) =>
|
|
116
|
+
({ err }) => {
|
|
117
|
+
if (this.options?.reconnect) {
|
|
118
|
+
console.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
|
|
119
|
+
this.connection = null;
|
|
120
|
+
} else {
|
|
121
|
+
console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
108
124
|
this.creatingConnection = false;
|
|
109
125
|
this.em.emit(connectionCreatedConst, connection);
|
|
110
126
|
resolve(connection);
|