-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinit.js
More file actions
140 lines (122 loc) · 3.93 KB
/
init.js
File metadata and controls
140 lines (122 loc) · 3.93 KB
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
// Copyright (c) 2018-2025 TheComputerGenie
// Distributed under the GNU GENERAL PUBLIC LICENSE software license, see the accompanying
// file LICENSE or https://www.gnu.org/licenses/gpl-3.0.en.html
const fs = require('fs');
const os = require('os');
const cluster = require('cluster');
const systemConfig = require('./system-config.json');
const Website = require('./lib/workers/website.js');
const logging = require('./lib/modules/logging.js');
const PoolWorker = require('./lib/workers/poolWorker.js');
const CliListener = require('./lib/workers/cliListener.js');
// Get coin symbol from command line arguments, default to KMD
const coinSymbol = process.argv[2] || 'KMD';
const coinFilePath = `coin_configs/${coinSymbol}.json`;
if (!fs.existsSync(coinFilePath)) {
console.log('Master', coinSymbol, `could not find file: ${coinFilePath}`);
return;
}
const coinProfile = JSON.parse(fs.readFileSync(coinFilePath, { encoding: 'utf8' }));
// Merge system config with coin config
const config = Object.assign({}, systemConfig, coinProfile);
config.coin = coinProfile;
exports.cconfig = coinProfile;
if (cluster.isWorker) {
switch (process.env.workerType) {
case 'pool':
new PoolWorker();
break;
case 'website':
new Website();
break;
}
return;
}
function spawnPoolWorkers() {
let numForks;
if (!config.clustering || !config.clustering.enabled) {
numForks = 1;
} else if (config.clustering.forks === 'auto') {
numForks = os.cpus().length;
} else if (!config.clustering.forks || isNaN(config.clustering.forks)) {
numForks = 1;
} else {
numForks = config.clustering.forks;
}
const poolWorkers = {};
function createPoolWorker(forkId) {
const worker = cluster.fork({
workerType: 'pool',
forkId: forkId,
config: JSON.stringify(config)
});
worker.forkId = forkId;
worker.type = 'pool';
poolWorkers[forkId] = worker;
worker.on('exit', (code, signal) => {
logging('Pool', 'error', `\tFork ${forkId} died, spawning replacement worker...`, forkId);
setTimeout(() => {
createPoolWorker(forkId);
}, 2000);
}).on('message', (msg) => { });
}
let i = 0;
const spawnInterval = setInterval(() => {
createPoolWorker(i);
i++;
if (i == numForks) {
clearInterval(spawnInterval);
logging('Init', 'debug', `\tSpawned proxy on ${numForks} threads(s)`);
}
}, 250);
}
function startCliListener() {
const cliPort = config.cliPort;
const listener = new CliListener(cliPort);
listener.on('log', (text) => {
console.log(`CLI: ${text}`);
}).on('command', (command, params, options, reply) => {
switch (command) {
case 'blocknotify':
Object.keys(cluster.workers).forEach((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;
}
const worker = cluster.fork({
workerType: 'website',
config: JSON.stringify(config)
});
worker.on('exit', (code, signal) => {
logging('Website', 'error', 'Website process died, spawning replacement...');
setTimeout(() => {
startWebsite(config);
}, 2000);
});
}
function createEmptyLogs() {
const logPath = `./block_logs/${coinSymbol}_blocks.json`;
if (!fs.existsSync(logPath)) {
fs.writeFileSync(logPath, '[]');
}
}
(function init() {
createEmptyLogs();
spawnPoolWorkers();
startCliListener();
startWebsite();
})();