forked from fireice-uk/cryptonote-xmr-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
195 lines (163 loc) · 5.69 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
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
var fs = require('fs');
var cluster = require('cluster');
var redis = require('redis');
////simplewallet --wallet-file=wallet.bin --pass=test --rpc-bind-port=8082
//./simplewallet --wallet-file=wallet.bin --pass=test --rpc-bind-port=8342 --daemon-port=32837
if (cluster.isWorker){
switch(process.env.workerType){
case 'pool':
require('./lib/pool.js');
break;
case 'blockUnlocker':
require('./lib/blockUnlocker.js');
break;
case 'paymentProcessor':
require('./lib/paymentProcessor.js');
break;
case 'api':
require('./lib/api.js');
break;
case 'cli':
require('./lib/cli.js');
break
}
return;
}
var config = JSON.parse(fs.readFileSync('config.json'));
var logger = require('./lib/logUtil.js')({
logLevel: config.logLevel,
logColors: config.logColors
});
var logSystem = 'Master';
var logSubsystem = null;
var os = require('os');
(function init(){
checkRedisVersion(function(){
spawnPoolWorkers();
spawnBlockUnlocker();
spawnPaymentProcessor();
spawnApi();
spawnCli();
});
})();
function checkRedisVersion(callback){
var redisClient = redis.createClient(config.redis.port, config.redis.host);
redisClient.info(function(error, response){
if (error){
logger.error(logSystem, logComponent, logSubCat, '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){
logger.error(logSystem, logSubsystem, null, 'Could not detect redis version - but be super old or broken');
return;
}
else if (version < 2.6){
logger.error(logSystem, logSubsystem, null, "You're using redis version " + versionString + " the minimum required version is 2.6. Follow the damn usage instructions...");
return;
}
callback();
});
}
function spawnPoolWorkers(){
if (!config.poolServer || !config.poolServer.enabled || !config.poolServer.ports || config.poolServer.ports.length === 0) return;
if (config.poolServer.ports.filter(function(portData){return portData.protocol === 'tcp' || portData.protocol === 'http'}).length === 0){
logger.error(logSystem, logSubsystem, null, 'Pool server enabled but not tcp or http ports specified');
return;
}
var numForks = (function(){
if (!config.poolServer.clusterForks)
return 1;
if (config.poolServer.clusterForks === 'auto')
return os.cpus().length;
if (isNaN(config.poolServer.clusterForks))
return 1;
return config.poolServer.clusterForks;
})();
var poolWorkers = {};
var createPoolWorker = function(forkId){
var worker = cluster.fork({
workerType: 'pool',
forkId: forkId
});
worker.forkId = forkId;
worker.type = 'pool';
poolWorkers[forkId] = worker;
worker.on('exit', function(code, signal){
//severity, system, subsystem, component, text
logger.error(logSystem, logSubsystem, 'Pool Spawner', 'Fork ' + forkId + ' died, spawning replacement worker...');
setTimeout(function(){
createPoolWorker(forkId);
}, 2000);
}).on('message', function(msg){
switch(msg.type){
case 'banIP':
Object.keys(cluster.workers).forEach(function(id) {
if (cluster.workers[id].type === 'pool'){
cluster.workers[id].send({type: 'banIP', ip: msg.ip});
}
});
break;
}
});
};
var i = 0;
var spawnInterval = setInterval(function(){
createPoolWorker(i);
i++;
if (i === numForks){
clearInterval(spawnInterval);
logger.debug(logSystem, logSubsystem, 'Pool Spawner', 'Spawned pool on ' + numForks + ' thread(s)');
}
}, 10);
}
function spawnBlockUnlocker(){
if (!config.blockUnlocker || !config.blockUnlocker.enabled) return;
var worker = cluster.fork({
workerType: 'blockUnlocker'
});
worker.on('exit', function(code, signal){
logger.error(logSystem, logSubsystem, 'Block Unlocker', 'Block unlocker died, spawning replacement...');
setTimeout(function(){
spawnBlockUnlocker();
}, 2000);
});
}
function spawnPaymentProcessor(){
if (!config.payments || !config.payments.enabled) return;
var worker = cluster.fork({
workerType: 'paymentProcessor'
});
worker.on('exit', function(code, signal){
logger.error(logSystem, logSubsystem, 'Payment Processor', 'Payment processor died, spawning replacement...');
setTimeout(function(){
spawnPaymentProcessor();
}, 2000);
});
}
function spawnApi(){
if (!config.api || !config.api.enabled) return;
var worker = cluster.fork({
workerType: 'api'
});
worker.on('exit', function(code, signal){
logger.error(logSystem, logSubsystem, 'API', 'API died, spawning replacement...');
setTimeout(function(){
spawnApi();
}, 2000);
});
}
function spawnCli(){
}