This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.js
134 lines (116 loc) · 3.7 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
var fs = require('fs');
var os = require('os');
var cluster = require('cluster');
var config = require('./config.json');
var Website = require('./lib/workers/website.js');
var logging = require('./lib/modules/logging.js');
var PoolWorker = require('./lib/workers/poolWorker.js');
var CliListener = require('./lib/workers/cliListener.js');
var coinFilePath = 'coins/' + config.coin;
if (!fs.existsSync(coinFilePath)) {
console.log('Master', config.coin, 'could not find file: ' + coinFilePath);
return;
}
var coinProfile = JSON.parse(fs.readFileSync(coinFilePath, {
encoding: 'utf8'
}));
config.coin = coinProfile;
if (cluster.isWorker) {
switch (process.env.workerType) {
case 'pool':
new PoolWorker();
break;
case 'website':
new Website();
break;
}
return;
}
function spawnPoolWorkers() {
var numForks = (function () {
if (!config.clustering || !config.clustering.enabled)
return 1;
if (config.clustering.forks === 'auto')
return os.cpus().length;
if (!config.clustering.forks || isNaN(config.clustering.forks))
return 1;
return config.clustering.forks;
})();
var poolWorkers = {};
function createPoolWorker(forkId) {
var worker = cluster.fork({
workerType: 'pool',
forkId: forkId,
config: JSON.stringify(config)
});
worker.forkId = forkId;
worker.type = 'pool';
poolWorkers[forkId] = worker;
worker.on('exit', function (code, signal) {
logging('Pool', 'error', 'Fork ' + forkId + ' died, spawning replacement worker...', forkId)
setTimeout(function () {
createPoolWorker(forkId);
}, 2000);
}).on('message', function (msg) {
logging('MSG', 'error', 'Fork ' + forkId + ' ' + msg, forkId)
});
}
var i = 0;
var spawnInterval = setInterval(function () {
createPoolWorker(i);
i++;
if (i == numForks) {
clearInterval(spawnInterval);
logging('Init', 'debug', 'Spawned proxy on ' + numForks + ' threads(s)')
}
}, 250);
}
function startCliListener() {
var cliPort = config.cliPort;
var listener = new CliListener(cliPort);
listener.on('log', function (text) {
console.log('CLI: ' + text);
}).on('command', function (command, params, options, reply) {
switch (command) {
case 'blocknotify':
Object.keys(cluster.workers).forEach(function (id) {
cluster.workers[id].send({
type: 'blocknotify',
coin: params[0],
hash: params[1]
});
});
reply('Workers notified');
break;
default:
reply('unrecognized command "' + command + '"');
break;
}
}).start();
}
function startWebsite() {
if (!config.website.enabled) return;
var worker = cluster.fork({
workerType: 'website',
config: JSON.stringify(config)
});
worker.on('exit', function (code, signal) {
logging('Website', 'error', 'Website process died, spawning replacement...')
setTimeout(function () {
startWebsite(config);
}, 2000);
});
}
function createEmptyLogs() {
try {
fs.readFileSync('./logs/blocks.json')
} catch (err) {
err.code === "ENOENT" ? fs.writeFileSync('./logs/blocks.json', '[]') : (function () { throw err }());
}
}
(function init() {
createEmptyLogs();
spawnPoolWorkers();
startCliListener();
startWebsite();
})();