Compare commits

...

7 commits

2 changed files with 126 additions and 92 deletions

View file

@ -50,22 +50,22 @@ serverErrorHandler.resetAttempts = (isRedirect) => {
process.messageEventListeners.push((worker, serverconsole) => {
return (message) => {
if (worker.id == Object.keys(cluster.workers)[0]) {
if (message.indexOf("\x12ERRLIST") == 0) {
const tries = parseInt(message.substring(8, 9));
const errCode = message.substring(9);
serverconsole.locerrmessage(
serverErrorDescs[errCode]
? serverErrorDescs[errCode]
: serverErrorDescs["UNKNOWN"],
);
serverconsole.locmessage(tries + " attempts left.");
}
if (message.length >= 9 && message.indexOf("\x12ERRCRASH") == 0) {
const errno = os.constants.errno[message.substring(9)];
process.exit(errno !== undefined ? errno : 1);
}
if (worker.id == Object.keys(cluster.workers)[0]) {
if (message.indexOf("\x12ERRLIST") == 0) {
const tries = parseInt(message.substring(8, 9));
const errCode = message.substring(9);
serverconsole.locerrmessage(
serverErrorDescs[errCode]
? serverErrorDescs[errCode]
: serverErrorDescs["UNKNOWN"],
);
serverconsole.locmessage(tries + " attempts left.");
}
if (message.length >= 9 && message.indexOf("\x12ERRCRASH") == 0) {
const errno = os.constants.errno[message.substring(9)];
process.exit(errno !== undefined ? errno : 1);
}
}
};
});

View file

@ -1093,9 +1093,10 @@ let commands = {
}
},
restart: (args, log) => {
if (cluster.isPrimary === undefined) log("This command is not supported on single-threaded " + name + ".");
if (cluster.isPrimary === undefined)
log("This command is not supported on single-threaded " + name + ".");
else log("This command need to be run in " + name + " master.");
}
},
};
// Load commands from middleware
@ -1104,9 +1105,11 @@ middleware.forEach((middlewareO) => {
Object.keys(middlewareO.commands).forEach((command) => {
const prevCommand = commands[command];
if (prevCommand) {
commands[command] = (args, log) => middlewareO.commands[command](args, log, prevCommand);
commands[command] = (args, log) =>
middlewareO.commands[command](args, log, prevCommand);
} else {
commands[command] = (args, log) => middlewareO.commands[command](args, log, () => {});
commands[command] = (args, log) =>
middlewareO.commands[command](args, log, () => {});
}
});
}
@ -1259,10 +1262,14 @@ process.messageEventListeners.push((worker, serverconsole) => {
};
});
let isWorkerHungUpBuff2 = true;
function msgListener(message) {
if (message == "\x12END") {
for (let i = 0; i < Object.keys(cluster.workers).length; i++) {
cluster.workers[Object.keys(cluster.workers)[i]].removeAllListeners("message");
cluster.workers[Object.keys(cluster.workers)[i]].removeAllListeners(
"message",
);
addListenersToWorker(cluster.workers[Object.keys(cluster.workers)[i]]);
}
}
@ -1270,6 +1277,19 @@ function msgListener(message) {
// Do nothing
} else if (message == "\x12CLOSE") {
closedMaster = true;
} else if (message == "\x12KILLOK") {
if (typeof isWorkerHungUpBuff != "undefined") isWorkerHungUpBuff = false;
} else if (message == "\x12PINGOK") {
if (typeof isWorkerHungUpBuff2 != "undefined") isWorkerHungUpBuff2 = false;
} else if (message == "\x12KILLTERMMSG") {
serverconsole.locmessage("Terminating unused worker process...");
} else if (message == "\x12SAVEGOOD") {
serverconsole.locmessage("Configuration saved.");
} else if (message.indexOf("\x12SAVEERR") == 0) {
serverconsole.locwarnmessage(
"There was a problem while saving configuration file. Reason: " +
message.substring(8),
);
} else if (message[0] == "\x12") {
console.log("RECEIVED CONTROL MESSAGE: " + message.substr(1));
} else {
@ -1277,6 +1297,11 @@ function msgListener(message) {
}
}
function saveConfig() {
// TODO: saveConfig() function
throw new Error("Configuration saving not yet implemented.");
}
// Starting function
function start(init) {
init = Boolean(init);
@ -1581,13 +1606,10 @@ function start(init) {
}
}
// TODO: implement clustering and commands
if (init) {
let workersToFork = 1;
// TODO: saveConfig
/*if (cluster.isPrimary === undefined) {
if (cluster.isPrimary === undefined) {
setInterval(function () {
try {
saveConfig();
@ -1601,7 +1623,7 @@ function start(init) {
var allWorkers = Object.keys(cluster.workers);
var goodWorkers = [];
function checkWorker(callback, _id) {
const checkWorker = (callback, _id) => {
if (typeof _id === "undefined") _id = 0;
if (_id >= allWorkers.length) {
callback();
@ -1626,14 +1648,13 @@ function start(init) {
} catch (err) {
if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allWorkers[_id]].on("message", listenConnListener);
addListenersToWorker(cluster.workers[allWorkers[_id]]);
}
checkWorker(callback, _id + 1);
}
}
};
checkWorker(function () {
var wN = Math.floor(Math.random() * goodWorkers.length); //Send a configuration saving message to a random worker.
const wN = Math.floor(Math.random() * goodWorkers.length); //Send a configuration saving message to a random worker.
try {
if (cluster.workers[goodWorkers[wN]]) {
isWorkerHungUpBuff2 = true;
@ -1643,14 +1664,16 @@ function start(init) {
} catch (err) {
if (cluster.workers[goodWorkers[wN]]) {
cluster.workers[goodWorkers[wN]].removeAllListeners("message");
cluster.workers[goodWorkers[wN]].on("message", bruteForceListenerWrapper(cluster.workers[goodWorkers[wN]]));
cluster.workers[goodWorkers[wN]].on("message", listenConnListener);
addListenersToWorker(cluster.workers[goodWorkers[wN]]);
}
serverconsole.locwarnmessage("There was a problem while saving configuration file. Reason: " + err.message);
serverconsole.locwarnmessage(
"There was a problem while saving configuration file. Reason: " +
err.message,
);
}
});
}, 300000);
}*/
}
if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
process.on("message", function (line) {
@ -1658,7 +1681,7 @@ function start(init) {
if (line == "") {
// Does Nothing
process.send("\x12END");
} /*else if (line == "\x14SAVECONF") {
} else if (line == "\x14SAVECONF") {
// Save configuration file
try {
saveConfig();
@ -1668,36 +1691,39 @@ function start(init) {
}
process.send("\x12END");
} else if (line == "\x14KILLPING") {
if (!reallyExiting) {
process.send("\x12KILLOK");
process.send("\x12END");
}
// Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway...
process.send("\x12KILLOK");
process.send("\x12END");
} else if (line == "\x14PINGPING") {
if (!reallyExiting) {
process.send("\x12PINGOK");
process.send("\x12END");
}
// Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway...
process.send("\x12PINGOK");
process.send("\x12END");
} else if (line == "\x14KILLREQ") {
if (reqcounter - reqcounterKillReq < 2) {
if (process.reqcounter - reqcounterKillReq < 2) {
process.send("\x12KILLTERMMSG");
process.nextTick(commands.stop);
process.nextTick(() => {
commands.stop([], () => {});
});
} else {
reqcounterKillReq = reqcounter;
reqcounterKillReq = process.reqcounter;
}
}*/ else if (commands[line.split(" ")[0]] !== undefined && commands[line.split(" ")[0]] !== null) {
} else if (message[0] == "\x14") {
// Discard unrecognized control messages
} else if (
commands[line.split(" ")[0]] !== undefined &&
commands[line.split(" ")[0]] !== null
) {
var argss = line.split(" ");
var command = argss.shift();
commands[command](argss, (msg) => process.send(msg));
process.send("\x12END");
} else {
process.send("Unrecognized command \"" + line.split(" ")[0] + "\".");
process.send('Unrecognized command "' + line.split(" ")[0] + '".');
process.send("\x12END");
}
} catch (err) {
if (line != "") {
process.send("Can't execute command \"" + line.split(" ")[0] + "\".");
process.send(
"Can't execute command \"" + line.split(" ")[0] + '".',
);
process.send("\x12END");
}
}
@ -1706,7 +1732,7 @@ function start(init) {
const rla = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: ""
prompt: "",
});
rla.prompt();
rla.on("line", function (line) {
@ -1716,8 +1742,10 @@ function start(init) {
if (line != "") {
if (cluster.isPrimary !== undefined) {
var allWorkers = Object.keys(cluster.workers);
if (command == "block") commands.block(argss, serverconsole.climessage);
if (command == "unblock") commands.unblock(argss, serverconsole.climessage);
if (command == "block")
commands.block(argss, serverconsole.climessage);
if (command == "unblock")
commands.unblock(argss, serverconsole.climessage);
if (command == "restart") {
var stopError = false;
exiting = true;
@ -1730,7 +1758,10 @@ function start(init) {
stopError = true;
}
}
if (stopError) serverconsole.climessage("Some " + name + " workers might not be stopped.");
if (stopError)
serverconsole.climessage(
"Some " + name + " workers might not be stopped.",
);
SVRJSInitialized = false;
closedMaster = true;
@ -1747,7 +1778,7 @@ function start(init) {
exiting = true;
allWorkers = Object.keys(cluster.workers);
}
allWorkers.forEach((clusterID)=> {
allWorkers.forEach((clusterID) => {
try {
if (cluster.workers[clusterID]) {
cluster.workers[clusterID].on("message", msgListener);
@ -1758,7 +1789,9 @@ function start(init) {
cluster.workers[clusterID].removeAllListeners("message");
addListenersToWorker(cluster.workers[clusterID]);
}
serverconsole.climessage("Can't run command \"" + command + "\".");
serverconsole.climessage(
"Can't run command \"" + command + '".',
);
}
});
if (command == "stop") {
@ -1770,7 +1803,9 @@ function start(init) {
try {
commands[command](argss, serverconsole.climessage);
} catch (err) {
serverconsole.climessage("Unrecognized command \"" + command + "\".");
serverconsole.climessage(
'Unrecognized command "' + command + '".',
);
}
}
}
@ -1786,19 +1821,6 @@ function start(init) {
SVRJSInitialized = true;
});
/*cluster.workers[Object.keys(cluster.workers)[0]].on("message", function (msg) {
if (msg.length >= 8 && msg.indexOf("\x12ERRLIST") == 0) {
var tries = parseInt(msg.substring(8, 9));
var errCode = msg.substring(9);
serverconsole.locerrmessage(serverErrorDescs[errCode] ? serverErrorDescs[errCode] : serverErrorDescs["UNKNOWN"]);
serverconsole.locmessage(tries + " attempts left.");
}
if (msg.length >= 9 && msg.indexOf("\x12ERRCRASH") == 0) {
var errno = errors[msg.substring(9)];
process.exit(errno ? errno : 1);
}
});*/
// Hangup check and restart
setInterval(function () {
if (!closedMaster && !exiting) {
@ -1939,22 +1961,24 @@ function start(init) {
}
}, 4550);
/*
// Termination of unused good workers
if (!disableUnusedWorkerTermination && cluster.isPrimary !== undefined) {
if (
!process.serverConfig.disableUnusedWorkerTermination &&
cluster.isPrimary !== undefined
) {
setTimeout(function () {
setInterval(function () {
if (!closedMaster && !exiting) {
var allWorkers = Object.keys(cluster.workers);
const allWorkers = Object.keys(cluster.workers);
var minWorkers = 0;
let minWorkers = 0;
minWorkers = Math.ceil(workersToFork * 0.625);
if (minWorkers < 2) minWorkers = 2;
if (minWorkers > 12) minWorkers = 12;
var goodWorkers = [];
let goodWorkers = [];
function checkWorker(callback, _id) {
const checkWorker = (callback, _id) => {
if (typeof _id === "undefined") _id = 0;
if (_id >= allWorkers.length) {
callback();
@ -1963,7 +1987,10 @@ function start(init) {
try {
if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff = true;
cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allWorkers[_id]].on(
"message",
msgListener,
);
cluster.workers[allWorkers[_id]].send("\x14KILLPING");
setTimeout(function () {
if (isWorkerHungUpBuff) {
@ -1978,37 +2005,45 @@ function start(init) {
}
} catch (err) {
if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allWorkers[_id]].removeAllListeners("message");
cluster.workers[allWorkers[_id]].on("message", bruteForceListenerWrapper(cluster.workers[allWorkers[_id]]));
cluster.workers[allWorkers[_id]].on("message", listenConnListener);
cluster.workers[allWorkers[_id]].removeAllListeners(
"message",
);
addListenersToWorker(cluster.workers[allWorkers[_id]]);
}
checkWorker(callback, _id + 1);
}
}
checkWorker(function () {
};
checkWorker(() => {
if (goodWorkers.length > minWorkers) {
var wN = Math.floor(Math.random() * goodWorkers.length);
const wN = Math.floor(Math.random() * goodWorkers.length);
if (wN == goodWorkers.length) return;
try {
if (cluster.workers[goodWorkers[wN]]) {
isWorkerHungUpBuff = true;
cluster.workers[goodWorkers[wN]].on("message", msgListener);
cluster.workers[goodWorkers[wN]].on(
"message",
msgListener,
);
cluster.workers[goodWorkers[wN]].send("\x14KILLREQ");
}
} catch (err) {
if (cluster.workers[goodWorkers[wN]]) {
cluster.workers[goodWorkers[wN]].removeAllListeners("message");
cluster.workers[goodWorkers[wN]].on("message", bruteForceListenerWrapper(cluster.workers[goodWorkers[wN]]));
cluster.workers[goodWorkers[wN]].on("message", listenConnListener);
cluster.workers[goodWorkers[wN]].removeAllListeners(
"message",
);
addListenersToWorker(cluster.workers[goodWorkers[wN]]);
}
serverconsole.locwarnmessage("There was a problem while terminating unused worker process. Reason: " + err.message);
serverconsole.locwarnmessage(
"There was a problem while terminating unused worker process. Reason: " +
err.message,
);
}
}
});
}
}, 300000);
}, 2000);
}*/
}
}
}
}
@ -2040,10 +2075,9 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
process.on("exit", function (code) {
try {
// TODO: saveConfig function
/*if (!configJSONRErr && !configJSONPErr) {
if (!configJSONRErr && !configJSONPErr) {
saveConfig();
}*/
}
} catch (err) {
serverconsole.locwarnmessage(
"There was a problem while saving configuration file. Reason: " +