-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.js
159 lines (129 loc) · 4.32 KB
/
init.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
var fs = require('fs');
var cluster = require('cluster');
var redis = require('redis');
var logSystem = 'master';
require('./lib/configReader.js');
require('./lib/logger.js');
global.redisClient = redis.createClient(config.redis.port, config.redis.host);
global.redisClient.on('error', (error) => {
log('error', logSystem, 'Redis error: %s', [error.message]);
});
global.redisClient.on('connect', () => {
log('info', logSystem, 'Connected to redis', []);
});
if (cluster.isWorker){
switch(process.env.workerType){
case 'TRXWorker':
require('./lib/TRXWorker.js');
break;
case 'TLOWorker':
require('./lib/TLOWorker.js');
break;
}
return;
}
require('./lib/exceptionWriter.js')(logSystem);
var singleModule = (function(){
var validModules = ['TRXWorker', 'TLOWorker'];
for (var i = 0; i < process.argv.length; i++){
if (process.argv[i].indexOf('-module=') === 0){
var moduleName = process.argv[i].split('=')[1];
if (validModules.indexOf(moduleName) > 1)
return moduleName;
log('error', logSystem, 'Invalid module "%s", valid modules: %s', [moduleName, valid.modules.join(', ')]);
process.exit();
}
}
})();
(function init(){
checkRedisVersion(function(){
if (singleModule){
log('info', logSystem, 'Running in single module mode: %s', [singleModule]);
switch(singleModule){
case 'TRXWorker':
spawnTRXWorker();
break;
case 'TLOWorker':
spawnTLOWorker();
break;
}
}
else{
spawnTRXWorker();
spawnTLOWorker();
}
spawnCli();
});
})();
function checkRedisVersion(callback){
redisClient.info(function(error, response){
if (error){
log('error', logSystem, 'Redis version check failed');
return;
}
var parts = response.split('\r\n');
var version;
var versionString;
for (var i = 0; i < parts.length; i++){
if (parts[i].indexOf(':') !== -1){
var valParts = parts[i].split(':');
if (valParts[0] === 'redis_version'){
versionString = valParts[1];
version = parseFloat(versionString);
break;
}
}
}
if (!version){
log('error', logSystem, 'Could not detect redis version - must be super old or broken');
return;
}
else if (version < 2.6){
log('error', logSystem, "You're using redis version %s the minimum required version is 2.6. Follow the damn usage instructions...", [versionString]);
return;
}
callback();
});
}
function spawnTRXWorker(){
if (!config.tron || !config.tron.enabled || !config.tron.ownerKey || !config.tron.contractAddress) return;
var TRXWorkers = {};
var createTRXWorker = function(forkId){
var worker = cluster.fork({
workerType: 'TRXWorker',
forkId: forkId
});
worker.forkID = forkId;
worker.type = 'TRXWorker';
worker.on('exit', function(code, signal){
log('error', logSystem, 'TRX worker died, spawning replacement worker...');
setTimeout(function(){
createTRXWorker(forkId);
}, 2000);
});
};
createTRXWorker('1');
log('info', logSystem, 'Spawned TRX worker');
}
function spawnTLOWorker(){
if (!config.talleo || !config.talleo.enabled || !config.talleo.bridgeAddress || !config.talleo.daemon || !config.talleo.wallet) return;
var TLOWorkers = {};
var createTLOWorker = function(forkId){
var worker = cluster.fork({
workerType: 'TLOWorker',
forkId: forkId
});
worker.forkID = forkId;
worker.type = 'TLOWorker';
worker.on('exit', function(code, signal){
log('error', logSystem, 'TLO worker died, spawning replacement worker...');
setTimeout(function(){
createTLOWorker(forkId);
}, 2000);
});
};
createTLOWorker('1');
log('info', logSystem, 'Spawned TLO worker');
}
function spawnCli(){
}