1
0
forked from circlecloud/tera
tera/src/run-client.ts
MiaoWoo b05a00f87d refactor: rewrite rpc server client
Signed-off-by: MiaoWoo <admin@yumc.pw>
2019-07-20 13:12:21 +08:00

82 lines
2.3 KiB
TypeScript

let thread = parseInt(process.env.TERA_THREAD) || 1;
let Memory = thread * 4 * 1024 * 1024 * 1024
let ProcessMemorySize = Math.trunc(Memory / thread);
import { fork } from 'child_process'
import './core/constant'
import './core/library'
function Fork(Path, ArrArgs?): TeraChildProcess {
ArrArgs = ArrArgs || [];
if (global.LOCAL_RUN)
ArrArgs.push("LOCALRUN");
else
if (global.TEST_NETWORK)
ArrArgs.push("TESTRUN");
ArrArgs.push("PATH:" + global.DATA_PATH);
ArrArgs.push("HOSTING:" + global.HTTP_HOSTING_PORT);
if (!global.USE_PARAM_JS)
ArrArgs.push("NOPARAMJS");
if (global.NWMODE)
ArrArgs.push("NWMODE");
if (global.NOALIVE)
ArrArgs.push("NOALIVE");
if (global.DEV_MODE)
ArrArgs.push("DEV_MODE");
var execArgv = [];
var Worker = fork(Path, ArrArgs, { execArgv: execArgv });
return Worker as any;
};
let ArrMiningWrk = []
function RunProcess() {
console.log(`Start add ${thread} pow process`)
for (var R = 0; R < thread; R++) {
AddPowProcess()
}
}
function AddPowProcess() {
let Worker = Fork('./process/pow-process');
ArrMiningWrk.push(Worker);
Worker.Num = ArrMiningWrk.length;
Worker.on('message', function(msg) {
switch (msg.cmd) {
case "log":
console.log(msg.message)
break;
}
});
Worker.on('error', function(err) {
if (!ArrMiningWrk.length)
return;
console.log('ERROR IN PROCESS: ' + err);
});
Worker.on('close', function(code) {
for (var i = 0; i < ArrMiningWrk.length; i++) {
if (ArrMiningWrk[i].pid === Worker.pid) {
console.log(`STOP PROCESS: ${Worker.Num} pid: ${Worker.pid} code: ${code} delete wrk`);
ArrMiningWrk.splice(i, 1);
}
}
});
}
console.log("START MINER PROCESS COUNT: " + thread + " Memory: " + ProcessMemorySize / 1024 / 1024 + " Mb for each process");
async function CheckAlive() {
if (!ArrMiningWrk.length) {
console.log(`ArrMiningWrk.length == ${ArrMiningWrk.length} Starting all node...`)
RunProcess();
}
if (ArrMiningWrk.length < thread) {
AddPowProcess()
}
setTimeout(CheckAlive, 1000)
}
async function main() {
await CheckAlive()
}
main()