-
Notifications
You must be signed in to change notification settings - Fork 1
/
PoolUtils.js
312 lines (278 loc) · 9.63 KB
/
PoolUtils.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
'use strict';
var Connections = [],
DeadConnections = [],
retry = true,
logger,
_ = require('lodash');
module.exports = function (_logger) {
logger = _logger;
return {
service: {
getCount: getCount,
getConnectionDisplayData: getConnectionDisplayData,
publishMessageToExchange: publishMessageToExchange,
ackMessage: ackMessage,
nackMessage: nackMessage
},
pool: {
Connections: Connections,
DeadConnections: DeadConnections,
retry: retry,
removeConnection: removeConnection,
addHandlerConnPool: addHandlerConnPool,
addConnection: addConnection,
addDeadConnection: addDeadConnection,
addPublisherConnections: addPublisherConnections,
getConnectionByExchange: getConnectionByExchange,
clearPools: clearPools,
addChannel: addChannel,
getConnection: getConnection,
flushPool: flushPool,
reviveConnection: reviveConnection
}
}
};
// Array Watches
Connections.push = function() { Array.prototype.push.apply(this, arguments); ConnectionPoolChanged(true);};
Connections.splice = function() { Array.prototype.splice.apply(this, arguments); ConnectionPoolChanged(false);};
DeadConnections.push = function() { Array.prototype.push.apply(this, arguments); ConnectionPoolChanged(true);};
DeadConnections.splice = function() { Array.prototype.splice.apply(this, arguments); ConnectionPoolChanged(false);};
function getCount(status) {
status = (status === 'alive');
return status ? Connections.length : DeadConnections.length
}
function getConnectionDisplayData() {
var friendlyObjArray = [];
var i = 0;
if (Connections.length > 0) {
for ( i = 0; i < Connections.length; i++) {
if (Connections[i].registeredHandlers) {
Connections[i].registeredHandlers.forEach(function (handler) {
var friendlyObj = {
guid: Connections[i].guid,
queueConfig: handler.queueConfig,
messageRate: handler.messageRate,
status: 'Alive'
};
friendlyObjArray.push(friendlyObj);
});
}
}
}
if (Connections.length > 0) {
for ( i = 0; i < Connections.length; i++) {
if (Connections[i].registeredHandlers.length < 1) {
Connections[i].registeredPublishers.forEach(function (publisher) {
console.log('looping through registered publishers');
console.log(publisher);
var friendlyObj = {
guid: Connections[i].guid,
queueConfig: publisher,
messageRate: 0,
status: 'Alive'
};
friendlyObjArray.push(friendlyObj);
});
}
}
}
if (DeadConnections.length > 0) {
for ( i = 0; i < DeadConnections.length; i++) {
if (DeadConnections[i].registeredHandlers) {
DeadConnections[i].registeredHandlers.forEach(function (handler) {
var friendlyObj = {
guid: DeadConnections[i].guid,
queueConfig: handler.queueConfig,
messageRate: handler.messageRate,
status: 'Dead'
};
friendlyObjArray.push(friendlyObj);
});
}
}
}
if (DeadConnections.length > 0) {
for (i = 0; i < DeadConnections.length; i++) {
if (DeadConnections[i].registeredHandlers.length < 1) {
DeadConnections[i].registeredPublishers.forEach(function (publisher) {
console.log('looping through registered publishers');
console.log(publisher);
var friendlyObj = {
guid: DeadConnections[i].guid,
queueConfig: publisher,
messageRate: 0,
status: 'Dead'
};
friendlyObjArray.push(friendlyObj);
});
}
}
}
return friendlyObjArray;
}
function removeConnection(guid) {
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
console.log('found guid removing connection');
Connections[i].channels = [];
Connections[i].connection.close();
DeadConnections.push(Connections[i]);
_.remove(Connections, {guid:guid});
} else {
console.log('no matching guid found, not removing connection');
}
}
function addHandlerConnPool(guid, handler) {
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
console.log('found guid adding handler to conn pool');
Connections[i].registeredHandler = handler;
} else {
console.log('no matching guid found, not setting handler');
}
}
// DONE
function addConnection(client) {
clearPools(client.guid);
Connections.push(client);
}
// DONE
function addDeadConnection(client) {
clearPools(client.guid);
console.log('[AMQP] DeadConnection added to pool');
DeadConnections.push(client);
}
// DONE
function clearPools(guid){
if (_.findIndex(Connections, guid) >= 0){
console.log('found live connection to pull');
_.pull(DeadConnections, client.guid);
}
if (_.findIndex(DeadConnections, guid) >= 0){
console.log('found dead connection to pull');
_.pull(DeadConnections, client.guid);
}
}
// DONE
function addPublisherConnections(guid, publishers, ch) {
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
Connections[i].registeredPublishers = publishers;
Connections[i].publisherChannel = ch;
}
}
// DONE
function getConnectionByExchange(exchange) {
var i = _.findIndex(Connections, {exchange:exchange});
if (i >= 0){
Connections[i].connection.close();
DeadConnections.push(Connections[i]);
_.remove(Connections, {exchange:exchange});
}
}
// DONE
function addChannel(guid, ch) {
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
console.log('found guid adding channel to connection');
Connections[i].channels.push(ch);
} else {
console.log('no matching guid found, not setting handler');
}
}
// DONE
function getConnection(guid) {
// return the connection
return _.find(Connections, {guid:guid}) || _.find(DeadConnections, {guid:guid});
}
// DONE
function switchConnection(client, type){
if (type === 'livetodead'){
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
console.log('found guid changing live conn to dead');
DeadConnections.push(client);
_.remove(DeadConnections, client);
} else {
console.log('no matching guid found, not setting handler');
}
} else if (type === 'deadtolive'){
}
}
// DONE
function flushConn(guid, retry) {
console.log('flush pool with retry called for guid');
retry = retry || true;
console.log('remove connection started ' + guid);
var i = _.findIndex(Connections, {guid:guid});
if (i >= 0){
console.log('found guid removing live connection, retry', retry);
Connections[i].connection.close();
switchConnection(Connections[i]);
} else {
console.log('no matching guid found, not setting handler');
}
}
// DONE
function flushPool(retry) {
console.log('flush pool with retry called');
_(Connections).forEach(function(conn) {
flushConn(conn.guid, retry);
});
}
// DONE
function reviveConnection(guid) {
var i = _.findIndex(DeadConnections, {guid:guid});
if (i >= 0){
var connToRevive = DeadConnections[i];
connToRevive.connect(connToRevive.configInternal).then(function(ch){
connToRevive.registerHandlers(connToRevive.registerHandlers);
connToRevive.registerPublishers(connToRevive.registerPublishers);
});
}
}
// NOT DONE
function publishMessageToExchange(exchange, auditkey, message) {
console.log('publishing to exchange started');
var ok;
_.forEach(Connections, function (conn){
_.forEach(conn.registeredPublishers, function(pub){
if (pub === exchange) {
console.log('found publisher on this connection, beginning publishing');
ok = conn.publisherChannel.publish(exchange, auditkey, new Buffer(message));
console.log('message publish ' + ok);
}
});
});
return ok;
}
// INTERNAL DONE
function uAck(msg, ackStatus, queue, type){
_.forEach(Connections, function(conn){
_.forEach(conn.channels, function(ch){
if (queue === ch.queueConfig){
if (type === 'ack'){
ch.ack(msg, ackStatus);
} else {
ch.reject(msg, ackStatus);
}
}
});
});
}
// DONE
function ackMessage(msg, ackStatus, queue) {
uAck(msg, ackStatus, queue, 'ack');
}
// DONE
function nackMessage(msg, ackStatus, queue) {
uAck(msg, ackStatus, queue, 'nack');
}
// DONE
function ConnectionPoolChanged(added){
if (added) {
console.log('Pool change detected - Connections : ' + Connections.length + ' DeadConnections : ' + DeadConnections.length);
}else {
console.log('Pool change detected - Connections : ' + Connections.length + ' DeadConnections : ' + DeadConnections.length);
}
}