-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
373 lines (274 loc) · 10.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const amqplib = require( 'amqplib' );
const CryptoJS = require( 'crypto-js' );
const _ = require( 'lodash' );
let countRetryQueue = 0;
class MQSupport {
static NODE_ENV;
static sequelizeConnection;
static connection;
static channels;
static isAlive;
static MQ_CONFIGS = {};
static MQ_TOPICS = {};
static encodingKeys;
static DLX_EXCHANGE = 'DLX';
static TTL_EXCHANGE = 'TTL';
static DELAY_QUEUE_TIME = 30000;
static MAX_RETRY_QUEUE = 3;
static MESSAGE_TTL_TIME = 50000;
static config(configs) {
MQSupport.MQ_CONFIGS = _.reduce( configs, ( memo, config, key ) => {
memo[ key ] = config;
return memo;
}, MQSupport.MQ_CONFIGS );
}
static init(topics, sequelizeConnection) {
MQSupport.sequelizeConnection = sequelizeConnection;
MQSupport.NODE_ENV = MQSupport.MQ_CONFIGS.NODE_ENV || 'development';
MQSupport.MQ_TOPICS = _.reduce( topics, ( memo, topic ) => {
memo[ topic ] = `${MQSupport.NODE_ENV}.${ topic }`;
return memo;
}, MQSupport.MQ_TOPICS );
MQSupport.encodingKeys = _.reduce( topics, ( memo, topic ) => {
memo[ topic ] = CryptoJS.SHA256(topic).toString().slice(-10);
return memo;
}, {} );
}
static sendFunction = (connectedChannel, topic) => async msg => {
const encodedMsg = MQSupport.MQ_CONFIGS.MQ_ENCRYPT_MESSAGES
? CryptoJS.AES.encrypt(JSON.stringify(msg), MQSupport.encodingKeys[ topic ]).toString()
: JSON.stringify(msg);
await connectedChannel.channel.assertQueue(MQSupport.MQ_TOPICS[ topic ]);
await connectedChannel.channel.bindQueue(MQSupport.MQ_TOPICS[ topic ], `${MQSupport.NODE_ENV}.${MQSupport.DLX_EXCHANGE}.${topic}`, '');
await connectedChannel.channel.publish(`${MQSupport.NODE_ENV}.${MQSupport.DLX_EXCHANGE}.${topic}`, '', Buffer.from(encodedMsg), { persistent: true });
};
static registerNewHookFunction = (connectedChannel, topic) => async callback => {
if (_.isArray(callback)) {
connectedChannel.hooks = callback;
await connectedChannel.channel.consume(MQSupport.MQ_TOPICS[ topic ], MQSupport.consumeFunction(connectedChannel, topic));
return;
}
if (connectedChannel.hooks.length) {
if (!_.find(connectedChannel.hooks, callback)) {
connectedChannel.hooks.push(callback);
console.log( `Topic "${ topic }" had been hooked with '${ callback.name }'` );
}
return;
}
connectedChannel.hooks = [ callback ];
await connectedChannel.channel.consume(MQSupport.MQ_TOPICS[ topic ], MQSupport.consumeFunction(connectedChannel, topic));
};
static consumeFunction = (connectedChannel, topic) => {
console.log( `Topic "${ topic }" had been hooked with:\n${ _.map( connectedChannel.hooks, fn => `'${ fn.name }'` ).join( '\n' ) }` );
return async msg => {
let transaction;
try {
let decodedMsg;
try {
const rawMsg = msg.content.toString();
decodedMsg = JSON.parse(
MQSupport.MQ_CONFIGS.MQ_ENCRYPT_MESSAGES
? CryptoJS.AES.decrypt(rawMsg, MQSupport.encodingKeys[ topic ]).toString(CryptoJS.enc.Utf8)
: rawMsg
);
} catch (error) {
decodedMsg = { error };
}
transaction = MQSupport.sequelizeConnection && await MQSupport.sequelizeConnection.transaction();
for (let index = 0; index < connectedChannel.hooks.length; index++) {
await connectedChannel.hooks[ index ](decodedMsg, transaction);
}
(MQSupport?.MQ_CONFIGS?.MQ_AUTO_ACK === false)
? await MQSupport.nack(connectedChannel, msg, transaction)
: await MQSupport.ack(connectedChannel, msg, transaction);
countRetryQueue = 0;
} catch (error) {
transaction && await transaction.rollback();
countRetryQueue++;
if ( countRetryQueue <= MQSupport.MAX_RETRY_QUEUE ) {
console.log(`${topic} retry ${countRetryQueue}`);
await MQSupport.pushToDelayedQueueRetry(connectedChannel, topic, msg, countRetryQueue);
await connectedChannel.channel.ack(msg);
} else {
await connectedChannel.channel.nack(msg);
await connectedChannel.channel.close();
countRetryQueue = 0;
console.error( topic, error );
}
}
};
}
static async retryConnection() {
MQSupport.isAlive = undefined;
await MQSupport.initConnection();
if (!_.keys(MQSupport.channels).length) return;
// recover all channels and hooks connected to each receiving channel
await Promise.all(
_.map(MQSupport.channels, async (oldChannel, key) => {
const { mode, topic, hooks } = oldChannel;
await MQSupport.getConnectedChannel(mode, topic);
if (mode === 'receiving') MQSupport.channels[ key ].registerNewHook(hooks);
})
);
}
static async initConnection() {
try {
if (!MQSupport.isAlive) {
MQSupport.connection = await amqplib
.connect({
protocol: MQSupport.MQ_CONFIGS.MQ_PROTOCOL,
hostname: MQSupport.MQ_CONFIGS.MQ_HOST,
port: MQSupport.MQ_CONFIGS.MQ_PORT,
username: MQSupport.MQ_CONFIGS.MQ_USER,
password: MQSupport.MQ_CONFIGS.MQ_PASSWORD,
vhost: MQSupport.MQ_CONFIGS.MQ_VIRTUAL_PATH,
});
MQSupport.connection.on('error', async () => {
await MQSupport.retryConnection();
});
MQSupport.connection.on('close', async () => {
await MQSupport.retryConnection();
});
MQSupport.isAlive = true;
}
} catch (error) {
await MQSupport.retryConnection();
}
}
static async retryChannel(mode, topic) {
const encodedTopic = MQSupport.MQ_TOPICS[ topic ];
const channelKey = `${mode}_${encodedTopic}`;
MQSupport.channels[ channelKey ].isAlive = undefined;
const hooks = MQSupport.channels[ channelKey ].hooks;
await MQSupport.getConnectedChannel(mode, topic);
if (mode === 'receiving') MQSupport.channels[ channelKey ].registerNewHook(hooks);
}
static async closeChannel(mode, topic) {
const encodedTopic = MQSupport.MQ_TOPICS[ topic ];
const channelKey = `${mode}_${encodedTopic}`;
if (!MQSupport.channels[ channelKey ]) throw new Error('Invalid input');
await MQSupport.channels[ channelKey ].channel.close();
delete MQSupport.channels[ channelKey ];
}
static async getConnectedChannel(mode, topic, options) {
const encodedTopic = MQSupport.MQ_TOPICS[ topic ];
const channelKey = `${mode}_${encodedTopic}`;
await MQSupport.initConnection();
if (!MQSupport.channels) MQSupport.channels = {};
if (!MQSupport.channels[ channelKey ]) MQSupport.channels[ channelKey ] = { mode, topic, hooks: [] };
try {
if (!MQSupport.channels[ channelKey ].isAlive) {
MQSupport.channels[ channelKey ].channel = await MQSupport.connection.createChannel();
MQSupport.channels[ channelKey ].channel.on('error', async () => {
await MQSupport.retryChannel(mode, topic);
});
MQSupport.channels[ channelKey ].mode = mode;
MQSupport.channels[ channelKey ].topic = topic;
MQSupport.channels[ channelKey ].isAlive = true;
if ( !options || !_.has( options, 'prefetch' ) || options.prefetch === true ) {
MQSupport.channels[ channelKey ].channel.prefetch( 1 );
}
if (mode === 'sending') {
MQSupport.channels[ channelKey ].send = MQSupport.sendFunction(MQSupport.channels[ channelKey ], topic);
} else if (mode === 'receiving') {
MQSupport.channels[ channelKey ].registerNewHook = MQSupport.registerNewHookFunction(MQSupport.channels[ channelKey ], topic);
}
}
const TTL_PREFIX_EXCHANGE = `${MQSupport.NODE_ENV}.${MQSupport.TTL_EXCHANGE}.${topic}`;
const DLX_PREFIX_EXCHANGE = `${MQSupport.NODE_ENV}.${MQSupport.DLX_EXCHANGE}.${topic}`;
await MQSupport.channels[ channelKey ].channel.assertExchange(TTL_PREFIX_EXCHANGE, 'direct', { durable: true } );
await MQSupport.channels[ channelKey ].channel.assertExchange(DLX_PREFIX_EXCHANGE, 'direct', { durable: true } );
await MQSupport.channels[ channelKey ].channel.assertQueue(encodedTopic, { durable: true });
await MQSupport.channels[ channelKey ].channel.assertQueue(`${encodedTopic}-RETRY`, { durable: true, deadLetterExchange: DLX_PREFIX_EXCHANGE, messageTtl: MQSupport.MESSAGE_TTL_TIME });
await MQSupport.channels[ channelKey ].channel.bindQueue(encodedTopic, DLX_PREFIX_EXCHANGE);
await MQSupport.channels[ channelKey ].channel.bindQueue(`${encodedTopic}-RETRY`, TTL_PREFIX_EXCHANGE);
return MQSupport.channels[ channelKey ];
} catch (error) {
await MQSupport.retryChannel(mode, topic);
}
}
static async recvMQMess(topic, callback) {
try {
if (
!MQSupport.MQ_TOPICS[ topic ]
|| ( !_.isFunction( callback ) && !_.isArray( callback ) )
) throw new Error('Invalid input');
const cachedChannel = await MQSupport.getConnectedChannel('receiving', topic);
await cachedChannel.registerNewHook(callback);
} catch (error) {
throw error;
}
}
static async ack(connectedChannel, msg, transaction = undefined) {
await connectedChannel.channel.ack(msg);
transaction && await transaction.commit();
}
static async nack(connectedChannel, msg, transaction = undefined) {
await connectedChannel.channel.nack(msg);
transaction && await transaction.rollback();
}
static async pushToDelayedQueueRetry(connectedChannel, topic, msg, countRetryQueue) {
await connectedChannel.channel.publish(`${MQSupport.NODE_ENV}.${MQSupport.TTL_EXCHANGE}.${topic}`, '', Buffer.from(msg.content.toString()), {
persistent: true,
headers: {
'x-delay': MQSupport.DELAY_QUEUE_TIME,
'x-retry-count': countRetryQueue
}
} );
}
}
class MQService {
static config(configs) {
try {
MQSupport.config(configs);
} catch (error) {
throw error;
}
}
static init(topics, sequelizeConnection) {
try {
if (!_.keys(topics).length || _.keys(MQSupport.MQ_TOPICS).length) throw new Error('Invalid input');
MQSupport.init(topics, sequelizeConnection);
} catch (error) {
throw error;
}
}
static async hookListeners(listeners) {
try {
const groupByTopicObj = _.chain(listeners)
.reduce((memo, listener) => {
if ( !memo[ listener[0] ] ) memo[ listener[0] ] = [];
memo[ listener[0] ].push(listener);
return memo;
}, {})
.values()
.value();
for (let index = 0; index < groupByTopicObj.length; index++) {
await MQSupport.recvMQMess(groupByTopicObj[index][0][0], _.chain(groupByTopicObj[index]).sortBy(i=>i[1]).map(i=>i[2]).value());
}
} catch (error) {
throw error;
}
}
static async sendMQMess(topic, msg, options = {}) {
try {
if (!MQSupport.MQ_TOPICS[ topic ] || !_.isObject(msg)) throw new Error('Invalid input');
const cachedChannel = await MQSupport.getConnectedChannel('sending', topic, options);
await cachedChannel.send(msg);
} catch (error) {
throw error;
}
}
static async closeChannel(topic) {
try {
if (!MQSupport.MQ_TOPICS[ topic ]) throw new Error('Invalid input');
await MQSupport.closeChannel('receiving', topic);
} catch (error) {
throw error;
}
}
}
module.exports = {
MQService,
MQ_TOPICS: MQSupport.MQ_TOPICS,
};