forked from WaveringAna/equihash-solomining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
131 lines (118 loc) · 3.79 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
const fs = require('fs');
const os = require('os');
const cluster = require('cluster');
const Website = require('./lib/workers/websiteListener.js');
const logging = require('./lib/modules/logging.js');
const PoolWorker = require('./lib/workers/poolWorker.js');
const CliListener = require('./lib/workers/cliListener.js');
var config = (process.argv[3] ? require(`./${process.argv[3]}_config.json`) : require('./config.json'));
var coinFilePath = `coins/${config.coin}`;
if (!fs.existsSync(coinFilePath))
{
console.log('Master', config.coin, `could not find file: ${coinFilePath}`);
return;
}
config.coin = JSON.parse(fs.readFileSync(coinFilePath, { encoding: 'utf8' }));
config.coin.explorer = (config.coin.nonDexstatsExplorer ? config.coin.nonDexstatsExplorer : `https://${config.coin.symbol}.explorer.dexstats.info`);
if (cluster.isWorker)
{
switch (process.env.workerType) {
case 'pool':
new PoolWorker();
break;
case 'website':
new Website();
break;
}
return;
}
function spawnPoolWorkers()
{
var numForks = (() => {
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', (code, signal) => {
logging('Pool', 'error', `Fork ${forkId} died, spawning replacement worker...`, forkId)
setTimeout(() => { createPoolWorker(forkId); }, 2000);
});
}
var i = 0;
var spawnInterval = setInterval(() => {
createPoolWorker(i);
i++;
if (i == numForks) {
clearInterval(spawnInterval);
logging(' Init ', 'debug', `Spawned pool on ${numForks} threads(s)`)
}
}, 250);
}
function startCliListener()
{
var cliPort = config.cliPort;
var 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',
workid: cluster.workers[id],
coin: params[0],
hash: params[1]
});
});
reply('Pool 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', (code, signal) => {
logging('Website', 'error', 'Website process died, spawning replacement...')
setTimeout(() => {
startWebsite(config);
}, 2000);
});
}
function createEmptyLogs()
{
try {
fs.readFileSync(`./logs/${config.coin.symbol}_blocks.json`)
} catch (err) {
if (err.code === "ENOENT") {
fs.writeFileSync(`./logs/${config.coin.symbol}_blocks.json`, '[]');
} else {
throw err;
}
}
}
(function init(){
createEmptyLogs();
spawnPoolWorkers();
startCliListener();
startWebsite();
})();