From 8bcb9957289e2751c1135baa4b5faa8f1219e036 Mon Sep 17 00:00:00 2001 From: Joe Uhren Date: Sun, 20 Aug 2023 20:41:41 -0600 Subject: [PATCH] Cluster + instance improvements -Moved the database initialize call out of the instance code into the prestart script so that it is now only called once when you start the explorer even if you are running multiple cpus/threads -The database connection function now uses the default connection string in the event a connection string is not supplied -Removed unused reference to lib/explorer.js code from the cluster file --- bin/cluster | 1 - bin/instance | 35 +++++++------------ lib/database.js | 7 ++++ scripts/prestart.js | 84 +++++++++++++++++++++++++-------------------- 4 files changed, 66 insertions(+), 61 deletions(-) diff --git a/bin/cluster b/bin/cluster index fbca498..428f6f7 100644 --- a/bin/cluster +++ b/bin/cluster @@ -2,7 +2,6 @@ var cluster = require('cluster'); if (cluster.isMaster) { const isWinOS = process.platform == 'win32'; - const lib = require('../lib/explorer'); const instances = (process.argv[2] != null && process.argv[2] != '' && !isNaN(process.argv[2]) && Number.isInteger(parseFloat(process.argv[2])) ? parseInt(process.argv[2]) : require('os').cpus().length); console.log('Starting cluster with pid: ' + process.pid); diff --git a/bin/instance b/bin/instance index 0824892..4550b17 100644 --- a/bin/instance +++ b/bin/instance @@ -6,30 +6,21 @@ var app = require('../app'); app.set('port', process.env.PORT || settings.webserver.port); -var dbString = 'mongodb://' + encodeURIComponent(settings.dbsettings.user); -dbString = dbString + ':' + encodeURIComponent(settings.dbsettings.password); -dbString = dbString + '@' + settings.dbsettings.address; -dbString = dbString + ':' + settings.dbsettings.port; -dbString = dbString + '/' + settings.dbsettings.database; +db.connect(null, function() { + var server = app.listen(app.get('port'), '::', function() { + debug('Express server listening on port ' + server.address().port); + }); -db.connect(dbString, function() { - // initialize the database - db.initialize_data_startup(function() { - var server = app.listen(app.get('port'), '::', function() { - debug('Express server listening on port ' + server.address().port); - }); + process.on('SIGINT', () => { + server.close(() => { + var mongoose = require('mongoose'); - process.on('SIGINT', () => { - server.close(() => { - var mongoose = require('mongoose'); - - mongoose.connection.close(false).then(() => { - // close the main process now that all http and database connections have closed - process.exit(0); - }).catch((err) => { - console.log(err); - process.exit(1); - }); + mongoose.connection.close(false).then(() => { + // close the main process now that all http and database connections have closed + process.exit(0); + }).catch((err) => { + console.log(err); + process.exit(1); }); }); }); diff --git a/lib/database.js b/lib/database.js index 4fbd4db..ec7c17e 100644 --- a/lib/database.js +++ b/lib/database.js @@ -286,6 +286,13 @@ module.exports = { connect: function(database, cb) { mongoose.set('strictQuery', true); + if (database == null) + database = 'mongodb://' + encodeURIComponent(settings.dbsettings.user) + + ':' + encodeURIComponent(settings.dbsettings.password) + + '@' + settings.dbsettings.address + + ':' + settings.dbsettings.port + + '/' + settings.dbsettings.database; + mongoose.connect(database).then(() => { return cb(); }).catch((err) => { diff --git a/scripts/prestart.js b/scripts/prestart.js index 1953cba..d1d1cc8 100644 --- a/scripts/prestart.js +++ b/scripts/prestart.js @@ -10,7 +10,7 @@ var nodeVersionRevision = '0'; // check if the nodejs version # is blank or a very long string as that would usually indicate a problem if (nodeVersion != null && nodeVersion != '' && nodeVersion.length < 16) { - // Remove the 'v' from the beginning of the version string + // remove the 'v' from the beginning of the version string if (nodeVersion.indexOf('v') == 0) nodeVersion = nodeVersion.slice(1); @@ -99,48 +99,56 @@ check_arguments_passed(function(pidName, node_env) { // compile scss to css execSync('node ./scripts/compile_css.js', {stdio : 'inherit'}); - // check if the webserver should be started from here based on the pidName - switch (pidName) { - case 'pm2': - let startOrReload = 'start'; + const db = require('../lib/database'); - // get a json list of pm2 processes - let result = execSync(`pm2 jlist`); + // connect to the mongo database + db.connect(null, function() { + // initialize the database + db.initialize_data_startup(function() { + // check if the webserver should be started from here based on the pidName + switch (pidName) { + case 'pm2': + let startOrReload = 'start'; - // check if the result is null - if (result != null) { - try { - // convert return result to JSON - result = JSON.parse(result); + // get a json list of pm2 processes + let result = execSync(`pm2 jlist`); - // loop through the results - for (let i = 0; i < result.length; i++) { - // check if this is an explorer process - if (result[i].name == 'explorer') { - // explorer process exists, so reload the process - startOrReload = 'reload'; - break; + // check if the result is null + if (result != null) { + try { + // convert return result to JSON + result = JSON.parse(result); + + // loop through the results + for (let i = 0; i < result.length; i++) { + // check if this is an explorer process + if (result[i].name == 'explorer') { + // explorer process exists, so reload the process + startOrReload = 'reload'; + break; + } + } + } catch(e) { + // do nothing } } - } catch(e) { - // do nothing - } + + // Setting the NODE_ENV variable is more easily done from here seeing at the syntax changes slightly depending on operating system + execSync(`${(process.platform == 'win32' ? 'set' : 'export')} NODE_ENV=${node_env} && pm2 ${startOrReload} ./bin/instance -i 0 -n explorer -p "./tmp/pm2.pid" --node-args="--stack-size=10000" --update-env`, {stdio : 'inherit'}); + break; + case 'forever': + const path = require('path'); + + // there is a long-time bug or shortcoming in forever that still exists in the latest version which requires the absolute path to the pid file option + // more info: https://github.com/foreversd/forever/issues/421 + // forever is therefore started from here to be able to more easily resolve the absolute path + // also, setting the NODE_ENV variable is more easily done from here as well seeing at the syntax changes slightly depending on operating system + execSync(`${(process.platform == 'win32' ? 'set' : 'export')} NODE_ENV=${node_env} && forever start --append --uid "explorer" --pidFile "${path.resolve('./tmp/forever.pid')}" ./bin/cluster`, {stdio : 'inherit'}); + break; } - // Setting the NODE_ENV variable is more easily done from here seeing at the syntax changes slightly depending on operating system - execSync(`${(process.platform == 'win32' ? 'set' : 'export')} NODE_ENV=${node_env} && pm2 ${startOrReload} ./bin/instance -i 0 -n explorer -p "./tmp/pm2.pid" --node-args="--stack-size=10000" --update-env`, {stdio : 'inherit'}); - break; - case 'forever': - const path = require('path'); - - // there is a long-time bug or shortcoming in forever that still exists in the latest version which requires the absolute path to the pid file option - // more info: https://github.com/foreversd/forever/issues/421 - // forever is therefore started from here to be able to more easily resolve the absolute path - // also, setting the NODE_ENV variable is more easily done from here as well seeing at the syntax changes slightly depending on operating system - execSync(`${(process.platform == 'win32' ? 'set' : 'export')} NODE_ENV=${node_env} && forever start --append --uid "explorer" --pidFile "${path.resolve('./tmp/forever.pid')}" ./bin/cluster`, {stdio : 'inherit'}); - break; - } - - // finished pre-loading - process.exit(0); + // finished pre-loading + process.exit(0); + }); + }); }); \ No newline at end of file