forked from svrjs/svrjs
Add server console, and fixed listening error message handler
This commit is contained in:
parent
a55b123926
commit
3de85443d5
2 changed files with 66 additions and 46 deletions
|
@ -37,7 +37,7 @@ function serverErrorHandler(err, isRedirect, server, start) {
|
||||||
// Probably main process exited
|
// Probably main process exited
|
||||||
}
|
}
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
var errno = os.constants.errno[err.code];
|
const errno = os.constants.errno[err.code];
|
||||||
process.exit(errno !== undefined ? errno : 1);
|
process.exit(errno !== undefined ? errno : 1);
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,10 @@ serverErrorHandler.resetAttempts = (isRedirect) => {
|
||||||
|
|
||||||
process.messageEventListeners.push((worker, serverconsole) => {
|
process.messageEventListeners.push((worker, serverconsole) => {
|
||||||
return (message) => {
|
return (message) => {
|
||||||
|
if (worker.id == Object.keys(cluster.workers)[0]) {
|
||||||
if (message.indexOf("\x12ERRLIST") == 0) {
|
if (message.indexOf("\x12ERRLIST") == 0) {
|
||||||
const tries = parseInt(msg.substring(8, 9));
|
const tries = parseInt(message.substring(8, 9));
|
||||||
const errCode = msg.substring(9);
|
const errCode = message.substring(9);
|
||||||
serverconsole.locerrmessage(
|
serverconsole.locerrmessage(
|
||||||
serverErrorDescs[errCode]
|
serverErrorDescs[errCode]
|
||||||
? serverErrorDescs[errCode]
|
? serverErrorDescs[errCode]
|
||||||
|
@ -60,10 +61,11 @@ process.messageEventListeners.push((worker, serverconsole) => {
|
||||||
);
|
);
|
||||||
serverconsole.locmessage(tries + " attempts left.");
|
serverconsole.locmessage(tries + " attempts left.");
|
||||||
}
|
}
|
||||||
if (message.length >= 9 && msg.indexOf("\x12ERRCRASH") == 0) {
|
if (message.length >= 9 && message.indexOf("\x12ERRCRASH") == 0) {
|
||||||
const errno = errors[msg.substring(9)];
|
const errno = os.constants.errno[message.substring(9)];
|
||||||
process.exit(errno !== undefined ? errno : 1);
|
process.exit(errno !== undefined ? errno : 1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
80
src/index.js
80
src/index.js
|
@ -1,6 +1,7 @@
|
||||||
const http = require("http");
|
const http = require("http");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
|
const readline = require("readline");
|
||||||
const logo = require("./res/logo.js");
|
const logo = require("./res/logo.js");
|
||||||
const generateServerString = require("./utils/generateServerString.js");
|
const generateServerString = require("./utils/generateServerString.js");
|
||||||
const deleteFolderRecursive = require("./utils/deleteFolderRecursive.js");
|
const deleteFolderRecursive = require("./utils/deleteFolderRecursive.js");
|
||||||
|
@ -747,8 +748,17 @@ const serverErrorHandler = require("./handlers/serverErrorHandler.js")(
|
||||||
serverconsole,
|
serverconsole,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function listeningMessage() {
|
||||||
|
closedMaster = false;
|
||||||
|
if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
|
||||||
|
process.send("\x12LISTEN");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO: listeningMessage()
|
||||||
|
}
|
||||||
|
|
||||||
let reqcounterKillReq = 0;
|
let reqcounterKillReq = 0;
|
||||||
let closedMaster = false; // TODO: closedMaster
|
let closedMaster = true;
|
||||||
|
|
||||||
let server = {};
|
let server = {};
|
||||||
let server2 = {};
|
let server2 = {};
|
||||||
|
@ -775,7 +785,7 @@ server2.on("error", (err) => {
|
||||||
});
|
});
|
||||||
server2.on("listening", () => {
|
server2.on("listening", () => {
|
||||||
serverErrorHandler.resetAttempts(true);
|
serverErrorHandler.resetAttempts(true);
|
||||||
// TODO: listeningMessage();
|
listeningMessage();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create HTTP server
|
// Create HTTP server
|
||||||
|
@ -879,7 +889,7 @@ server.on("error", function (err) {
|
||||||
});
|
});
|
||||||
server.on("listening", () => {
|
server.on("listening", () => {
|
||||||
serverErrorHandler.resetAttempts(false);
|
serverErrorHandler.resetAttempts(false);
|
||||||
// TODO: listeningMessage();
|
listeningMessage();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.serverConfig.secure) {
|
if (process.serverConfig.secure) {
|
||||||
|
@ -1089,14 +1099,11 @@ let commands = {
|
||||||
middleware.forEach((middlewareO) => {
|
middleware.forEach((middlewareO) => {
|
||||||
if (middlewareO.commands) {
|
if (middlewareO.commands) {
|
||||||
Object.keys(middlewareO.commands).forEach((command) => {
|
Object.keys(middlewareO.commands).forEach((command) => {
|
||||||
if (commands[command]) {
|
const prevCommand = commands[command];
|
||||||
commands[command] = (args, log) => {
|
if (prevCommand) {
|
||||||
middlewareO.commands(args, log, commands[command]);
|
commands[command] = (args, log) => middlewareO.commands[command](args, log, prevCommand);
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
commands[command] = (args, log) => {
|
commands[command] = (args, log) => middlewareO.commands[command](args, log, () => {});
|
||||||
middlewareO.commands(args, log, () => {});
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1243,10 +1250,28 @@ function forkWorkers(workersToFork, callback) {
|
||||||
// TODO: main message event listener
|
// TODO: main message event listener
|
||||||
process.messageEventListeners.push((worker, serverconsole) => {
|
process.messageEventListeners.push((worker, serverconsole) => {
|
||||||
return (message) => {
|
return (message) => {
|
||||||
console.log("Message received from worker: " + message);
|
if (message == "\x12LISTEN") {
|
||||||
|
listeningMessage();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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");
|
||||||
|
addListenersToWorker(cluster.workers[Object.keys(cluster.workers)[i]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (message == "\x12END") {
|
||||||
|
// Do nothing
|
||||||
|
} else if (message[0] == "\x12") {
|
||||||
|
console.log("RECEIVED CONTROL MESSAGE: " + message.substr(1));
|
||||||
|
} else {
|
||||||
|
serverconsole.climessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Starting function
|
// Starting function
|
||||||
function start(init) {
|
function start(init) {
|
||||||
init = Boolean(init);
|
init = Boolean(init);
|
||||||
|
@ -1631,14 +1656,13 @@ function start(init) {
|
||||||
}, 300000);
|
}, 300000);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
/*
|
|
||||||
if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
|
if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
|
||||||
process.on("message", function (line) {
|
process.on("message", function (line) {
|
||||||
try {
|
try {
|
||||||
if (line == "") {
|
if (line == "") {
|
||||||
// Does Nothing
|
// Does Nothing
|
||||||
process.send("\x12END");
|
process.send("\x12END");
|
||||||
} else if (line == "\x14SAVECONF") {
|
} /*else if (line == "\x14SAVECONF") {
|
||||||
// Save configuration file
|
// Save configuration file
|
||||||
try {
|
try {
|
||||||
saveConfig();
|
saveConfig();
|
||||||
|
@ -1666,10 +1690,10 @@ function start(init) {
|
||||||
} else {
|
} else {
|
||||||
reqcounterKillReq = reqcounter;
|
reqcounterKillReq = reqcounter;
|
||||||
}
|
}
|
||||||
} else if (commands[line.split(" ")[0]] !== undefined && commands[line.split(" ")[0]] !== null) {
|
}*/ else if (commands[line.split(" ")[0]] !== undefined && commands[line.split(" ")[0]] !== null) {
|
||||||
var argss = line.split(" ");
|
var argss = line.split(" ");
|
||||||
var command = argss.shift();
|
var command = argss.shift();
|
||||||
commands[command](argss);
|
commands[command](argss, (msg) => process.send(msg));
|
||||||
process.send("\x12END");
|
process.send("\x12END");
|
||||||
} else {
|
} else {
|
||||||
process.send("Unrecognized command \"" + line.split(" ")[0] + "\".");
|
process.send("Unrecognized command \"" + line.split(" ")[0] + "\".");
|
||||||
|
@ -1683,7 +1707,7 @@ function start(init) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var rla = readline.createInterface({
|
const rla = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout,
|
output: process.stdout,
|
||||||
prompt: ""
|
prompt: ""
|
||||||
|
@ -1691,13 +1715,13 @@ function start(init) {
|
||||||
rla.prompt();
|
rla.prompt();
|
||||||
rla.on("line", function (line) {
|
rla.on("line", function (line) {
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
var argss = line.split(" ");
|
const argss = line.split(" ");
|
||||||
var command = argss.shift();
|
const command = argss.shift();
|
||||||
if (line != "") {
|
if (line != "") {
|
||||||
if (cluster.isPrimary !== undefined) {
|
if (cluster.isPrimary !== undefined) {
|
||||||
var allWorkers = Object.keys(cluster.workers);
|
var allWorkers = Object.keys(cluster.workers);
|
||||||
if (command == "block") commands.block(argss);
|
if (command == "block") commands.block(argss, serverconsole.climessage);
|
||||||
if (command == "unblock") commands.unblock(argss);
|
if (command == "unblock") commands.unblock(argss, serverconsole.climessage);
|
||||||
if (command == "restart") {
|
if (command == "restart") {
|
||||||
var stopError = false;
|
var stopError = false;
|
||||||
exiting = true;
|
exiting = true;
|
||||||
|
@ -1727,7 +1751,7 @@ function start(init) {
|
||||||
exiting = true;
|
exiting = true;
|
||||||
allWorkers = Object.keys(cluster.workers);
|
allWorkers = Object.keys(cluster.workers);
|
||||||
}
|
}
|
||||||
allWorkers.forEach(function (clusterID) {
|
allWorkers.forEach((clusterID)=> {
|
||||||
try {
|
try {
|
||||||
if (cluster.workers[clusterID]) {
|
if (cluster.workers[clusterID]) {
|
||||||
cluster.workers[clusterID].on("message", msgListener);
|
cluster.workers[clusterID].on("message", msgListener);
|
||||||
|
@ -1736,25 +1760,19 @@ function start(init) {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (cluster.workers[clusterID]) {
|
if (cluster.workers[clusterID]) {
|
||||||
cluster.workers[clusterID].removeAllListeners("message");
|
cluster.workers[clusterID].removeAllListeners("message");
|
||||||
cluster.workers[clusterID].on("message", bruteForceListenerWrapper(cluster.workers[clusterID]));
|
addListenersToWorker(cluster.workers[clusterID]);
|
||||||
cluster.workers[clusterID].on("message", listenConnListener);
|
|
||||||
}
|
}
|
||||||
serverconsole.climessage("Can't run command \"" + command + "\".");
|
serverconsole.climessage("Can't run command \"" + command + "\".");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (command == "stop") {
|
if (command == "stop") {
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
reallyExiting = true;
|
commands[command](argss, serverconsole.climessage);
|
||||||
process.exit(0);
|
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (command == "stop") {
|
|
||||||
reallyExiting = true;
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
commands[command](argss);
|
commands[command](argss, serverconsole.climessage);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
serverconsole.climessage("Unrecognized command \"" + command + "\".");
|
serverconsole.climessage("Unrecognized command \"" + command + "\".");
|
||||||
}
|
}
|
||||||
|
@ -1762,7 +1780,7 @@ function start(init) {
|
||||||
}
|
}
|
||||||
rla.prompt();
|
rla.prompt();
|
||||||
});
|
});
|
||||||
}*/
|
}
|
||||||
|
|
||||||
if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
if (cluster.isPrimary || cluster.isPrimary === undefined) {
|
||||||
// Cluster forking code
|
// Cluster forking code
|
||||||
|
|
Reference in a new issue