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,
"environmentVariables": {},
"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",
"cz": "cz",
"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:fix": "npm run lint -- --fix",
"prepare": "husky",

View file

@ -117,7 +117,7 @@ if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into pr
function printUsage() {
console.log(`${name} usage:`);
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("--clean -- Cleans up files created by " + name);
@ -130,6 +130,7 @@ function printUsage() {
console.log(
"--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");
}
@ -137,6 +138,7 @@ let exiting = false;
let forceSecure = false;
let disableMods = false;
let stdoutNoTTY = false;
let noSaveConfig = false;
// Handle command line arguments
const args = process.argv;
@ -190,6 +192,8 @@ for (
process.singleThreaded = true;
} else if (args[i] == "--stdout-notty") {
stdoutNoTTY = true;
} else if (args[i] == "--no-save-config") {
noSaveConfig = true;
} else {
console.log(`Unrecognized argument: ${args[i]}`);
printUsage();
@ -335,6 +339,11 @@ if (process.serverConfig.allowPostfixDoubleSlashes === undefined)
process.serverConfig.allowPostfixDoubleSlashes = false;
if (process.serverConfig.optOutOfStatisticsServer === undefined)
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
process.serverConfig.version = version;
@ -1692,6 +1701,8 @@ function saveConfig() {
configJSONobj.allowDoubleSlashes = false;
if (configJSONobj.optOutOfStatisticsServer === undefined)
configJSONobj.optOutOfStatisticsServer = false;
if (configJSONobj.disableConfigurationSaving === undefined)
configJSONobj.disableConfigurationSaving = false;
fs.writeFileSync(
process.dirname + "/config.json",
@ -1984,69 +1995,73 @@ function start(init) {
let workersToFork = 1;
if (cluster.isPrimary === undefined) {
setInterval(() => {
try {
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;
}
if (!noSaveConfig) {
setInterval(() => {
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 {
saveConfig();
serverconsole.locmessage("Configuration saved.");
} catch (err) {
throw new Error(err);
}
}, 300000);
}
} else if (cluster.isPrimary) {
if (!noSaveConfig) {
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 {
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);
}
// 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(() => {
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}`
);
}
checkWorker(callback, _id + 1);
}
};
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);
});
}, 300000);
}
}
if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
@ -2435,7 +2450,7 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
process.on("exit", (code) => {
try {
if (!configJSONRErr && !configJSONPErr) {
if (!configJSONRErr && !configJSONPErr && !noSaveConfig) {
saveConfig();
}
} catch (err) {

View file

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