-
Notifications
You must be signed in to change notification settings - Fork 6
/
connections.js
126 lines (83 loc) · 2.47 KB
/
connections.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
var lolrmtp = require('./lolrmtp/main').client;
// Connections to different regions
global.connections = {};
global.regions = {
euw: {
region: 'euw',
username: 'lolfireeuw',
password: 'password',
version: '3.10.13_07_26_19_59'
}/*, na: {
region: 'na',
username: 'lolfirena',
password: 'password',
version: '3.10.13_07_26_19_59'
}, eune: {
region: 'eune',
username: 'lolfireeune',
password: 'password',
version: '3.10.13_07_26_19_59'
}*/
};
// Connects to each region in regions and handles reconnection
async.map(Object.keys(regions), function (region, callback){
connections[region] = new lolrmtp(regions[region]);
connections[region].connect(function (err){
if(err){
console.log('Couldnt connect to ' + region);
callback(err);
}
});
connections[region].on('connection', function (){
regions[region].connected = true;
mixpanel.track('connection', {
region: region,
date: new Date()
});
// Handles reconnection and error supperponing connection
// Why here? Cause it has to wait connection to listen on this stream events
connections[region].stream.on('close', function (){
regions[region].connected = false;
mixpanel.track('disconnection', {
region: region,
date: new Date()
});
connections[region].connect(function (err){
regions[region].connected = true;
mixpanel.track('reconnection', {
region: region,
date: new Date()
});
if(err){
console.log('Couldnt reconnect to ' + region);
callback(err);
}
});
});
});
}, function (err){
trackErr(err);
});
// Cron task that reconnects when all is down
setInterval(function (){
async.map(Object.keys(regions), function (region, callback){
if(regions[region].connected){
callback();
} else{
connections[region].connect(function (err){
mixpanel.track('reconnection', {
region: region,
date: new Date()
});
if(err){
console.log('Couldnt reconnect to ' + region);
callback(err);
}
});
}
}, function (err){
if(err){
trackErr(err);
}
});
}, 5000);