1
0
Fork 0
forked from svrjs/svrjs

feat: add options related to disabling configuration file saving, and use them in the "dev" script

This commit is contained in:
Dorian Niemiec 2024-11-09 13:31:31 +01:00
parent 8e1ea9e5bc
commit 5a0ea4007e
4 changed files with 81 additions and 64 deletions

View file

@ -59,5 +59,6 @@
"disableTrailingSlashRedirects": false, "disableTrailingSlashRedirects": false,
"environmentVariables": {}, "environmentVariables": {},
"allowDoubleSlashes": false, "allowDoubleSlashes": false,
"optOutOfStatisticsServer": false "optOutOfStatisticsServer": false,
"disableConfigurationSaving": false
} }

View file

@ -6,7 +6,7 @@
"build": "npm run clean && NODE_ENV=production node esbuild.config.js", "build": "npm run clean && NODE_ENV=production node esbuild.config.js",
"cz": "cz", "cz": "cz",
"clean": "rimraf dist && rimraf out && rimraf generatedAssets", "clean": "rimraf dist && rimraf out && rimraf generatedAssets",
"dev": "npm run clean && concurrently \"NODE_ENV=development node esbuild.config.js\" \"wait-on dist/svr.js && nodemon dist/svr.js --stdout-notty\"", "dev": "npm run clean && concurrently \"NODE_ENV=development node esbuild.config.js\" \"wait-on dist/svr.js && nodemon dist/svr.js --stdout-notty --no-save-config\"",
"lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js tests/*.test.js tests/*.js", "lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js tests/*.test.js tests/*.js",
"lint:fix": "npm run lint -- --fix", "lint:fix": "npm run lint -- --fix",
"prepare": "husky", "prepare": "husky",

View file

@ -117,7 +117,7 @@ if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into pr
function printUsage() { function printUsage() {
console.log(`${name} usage:`); console.log(`${name} usage:`);
console.log( console.log(
"node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [--stdout-notty] [-v] [--version]" "node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [--stdout-notty] [--no-save-config] [-v] [--version]"
); );
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("--clean -- Cleans up files created by " + name); console.log("--clean -- Cleans up files created by " + name);
@ -130,6 +130,7 @@ function printUsage() {
console.log( console.log(
"--stdout-notty -- Enable stdout even when stdout is not a TTY. May decrease the performace" "--stdout-notty -- Enable stdout even when stdout is not a TTY. May decrease the performace"
); );
console.log("--no-save-config -- Don't save configuration file");
console.log("-v --version -- Display server version"); console.log("-v --version -- Display server version");
} }
@ -137,6 +138,7 @@ let exiting = false;
let forceSecure = false; let forceSecure = false;
let disableMods = false; let disableMods = false;
let stdoutNoTTY = false; let stdoutNoTTY = false;
let noSaveConfig = false;
// Handle command line arguments // Handle command line arguments
const args = process.argv; const args = process.argv;
@ -190,6 +192,8 @@ for (
process.singleThreaded = true; process.singleThreaded = true;
} else if (args[i] == "--stdout-notty") { } else if (args[i] == "--stdout-notty") {
stdoutNoTTY = true; stdoutNoTTY = true;
} else if (args[i] == "--no-save-config") {
noSaveConfig = true;
} else { } else {
console.log(`Unrecognized argument: ${args[i]}`); console.log(`Unrecognized argument: ${args[i]}`);
printUsage(); printUsage();
@ -335,6 +339,11 @@ if (process.serverConfig.allowPostfixDoubleSlashes === undefined)
process.serverConfig.allowPostfixDoubleSlashes = false; process.serverConfig.allowPostfixDoubleSlashes = false;
if (process.serverConfig.optOutOfStatisticsServer === undefined) if (process.serverConfig.optOutOfStatisticsServer === undefined)
process.serverConfig.optOutOfStatisticsServer = false; process.serverConfig.optOutOfStatisticsServer = false;
if (process.serverConfig.disableConfigurationSaving === undefined)
process.serverConfig.disableConfigurationSaving = false;
// Don't save configuration if disableConfigurationSaving option is set to true
if (process.serverConfig.disableConfigurationSaving) noSaveConfig = true;
// Compatiblity for very old SVR.JS mods // Compatiblity for very old SVR.JS mods
process.serverConfig.version = version; process.serverConfig.version = version;
@ -1692,6 +1701,8 @@ function saveConfig() {
configJSONobj.allowDoubleSlashes = false; configJSONobj.allowDoubleSlashes = false;
if (configJSONobj.optOutOfStatisticsServer === undefined) if (configJSONobj.optOutOfStatisticsServer === undefined)
configJSONobj.optOutOfStatisticsServer = false; configJSONobj.optOutOfStatisticsServer = false;
if (configJSONobj.disableConfigurationSaving === undefined)
configJSONobj.disableConfigurationSaving = false;
fs.writeFileSync( fs.writeFileSync(
process.dirname + "/config.json", process.dirname + "/config.json",
@ -1984,69 +1995,73 @@ function start(init) {
let workersToFork = 1; let workersToFork = 1;
if (cluster.isPrimary === undefined) { if (cluster.isPrimary === undefined) {
setInterval(() => { if (!noSaveConfig) {
try { setInterval(() => {
saveConfig();
serverconsole.locmessage("Configuration saved.");
} catch (err) {
throw new Error(err);
}
}, 300000);
} else if (cluster.isPrimary) {
setInterval(() => {
let allWorkers = Object.keys(cluster.workers);
let goodWorkers = [];
const checkWorker = (callback, _id) => {
if (typeof _id === "undefined") _id = 0;
if (_id >= allWorkers.length) {
callback();
return;
}
try { try {
if (cluster.workers[allWorkers[_id]]) { saveConfig();
isWorkerHungUpBuff2 = true; serverconsole.locmessage("Configuration saved.");
cluster.workers[allWorkers[_id]].on("message", msgListener); } catch (err) {
cluster.workers[allWorkers[_id]].send("\x14PINGPING"); throw new Error(err);
setTimeout(() => { }
if (isWorkerHungUpBuff2) { }, 300000);
checkWorker(callback, _id + 1); }
} else { } else if (cluster.isPrimary) {
goodWorkers.push(allWorkers[_id]); if (!noSaveConfig) {
checkWorker(callback, _id + 1); setInterval(() => {
} let allWorkers = Object.keys(cluster.workers);
}, 250); let goodWorkers = [];
} else {
const checkWorker = (callback, _id) => {
if (typeof _id === "undefined") _id = 0;
if (_id >= allWorkers.length) {
callback();
return;
}
try {
if (cluster.workers[allWorkers[_id]]) {
isWorkerHungUpBuff2 = true;
cluster.workers[allWorkers[_id]].on("message", msgListener);
cluster.workers[allWorkers[_id]].send("\x14PINGPING");
setTimeout(() => {
if (isWorkerHungUpBuff2) {
checkWorker(callback, _id + 1);
} else {
goodWorkers.push(allWorkers[_id]);
checkWorker(callback, _id + 1);
}
}, 250);
} else {
checkWorker(callback, _id + 1);
}
// eslint-disable-next-line no-unused-vars
} catch (err) {
if (cluster.workers[allWorkers[_id]]) {
cluster.workers[allWorkers[_id]].removeAllListeners("message");
addListenersToWorker(cluster.workers[allWorkers[_id]]);
}
checkWorker(callback, _id + 1); checkWorker(callback, _id + 1);
} }
// eslint-disable-next-line no-unused-vars };
} catch (err) { checkWorker(() => {
if (cluster.workers[allWorkers[_id]]) { const wN = Math.floor(Math.random() * goodWorkers.length); //Send a configuration saving message to a random worker.
cluster.workers[allWorkers[_id]].removeAllListeners("message"); try {
addListenersToWorker(cluster.workers[allWorkers[_id]]); if (cluster.workers[goodWorkers[wN]]) {
isWorkerHungUpBuff2 = true;
cluster.workers[goodWorkers[wN]].on("message", msgListener);
cluster.workers[goodWorkers[wN]].send("\x14SAVECONF");
}
} catch (err) {
if (cluster.workers[goodWorkers[wN]]) {
cluster.workers[goodWorkers[wN]].removeAllListeners("message");
addListenersToWorker(cluster.workers[goodWorkers[wN]]);
}
serverconsole.locwarnmessage(
`There was a problem while saving configuration file. Reason: ${err.message}`
);
} }
checkWorker(callback, _id + 1); });
} }, 300000);
}; }
checkWorker(() => {
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;
cluster.workers[goodWorkers[wN]].on("message", msgListener);
cluster.workers[goodWorkers[wN]].send("\x14SAVECONF");
}
} catch (err) {
if (cluster.workers[goodWorkers[wN]]) {
cluster.workers[goodWorkers[wN]].removeAllListeners("message");
addListenersToWorker(cluster.workers[goodWorkers[wN]]);
}
serverconsole.locwarnmessage(
`There was a problem while saving configuration file. Reason: ${err.message}`
);
}
});
}, 300000);
} }
if (!cluster.isPrimary && cluster.isPrimary !== undefined) { if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
@ -2435,7 +2450,7 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
process.on("exit", (code) => { process.on("exit", (code) => {
try { try {
if (!configJSONRErr && !configJSONPErr) { if (!configJSONRErr && !configJSONPErr && !noSaveConfig) {
saveConfig(); saveConfig();
} }
} catch (err) { } catch (err) {

View file

@ -73,7 +73,8 @@
"disableTrailingSlashRedirects": false, "disableTrailingSlashRedirects": false,
"environmentVariables": {}, "environmentVariables": {},
"allowDoubleSlashes": false, "allowDoubleSlashes": false,
"optOutOfStatisticsServer": false "optOutOfStatisticsServer": false,
"disableConfigurationSaving": false
}</pre> }</pre>
</code> </code>
<p>Changes:</p> <p>Changes:</p>