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
This commit is contained in:
Joe Uhren
2023-08-20 20:41:41 -06:00
parent 550b961ab3
commit 8bcb995728
4 changed files with 66 additions and 61 deletions
-1
View File
@@ -2,7 +2,6 @@ var cluster = require('cluster');
if (cluster.isMaster) { if (cluster.isMaster) {
const isWinOS = process.platform == 'win32'; 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); 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); console.log('Starting cluster with pid: ' + process.pid);
+13 -22
View File
@@ -6,30 +6,21 @@ var app = require('../app');
app.set('port', process.env.PORT || settings.webserver.port); app.set('port', process.env.PORT || settings.webserver.port);
var dbString = 'mongodb://' + encodeURIComponent(settings.dbsettings.user); db.connect(null, function() {
dbString = dbString + ':' + encodeURIComponent(settings.dbsettings.password); var server = app.listen(app.get('port'), '::', function() {
dbString = dbString + '@' + settings.dbsettings.address; debug('Express server listening on port ' + server.address().port);
dbString = dbString + ':' + settings.dbsettings.port; });
dbString = dbString + '/' + settings.dbsettings.database;
db.connect(dbString, function() { process.on('SIGINT', () => {
// initialize the database server.close(() => {
db.initialize_data_startup(function() { var mongoose = require('mongoose');
var server = app.listen(app.get('port'), '::', function() {
debug('Express server listening on port ' + server.address().port);
});
process.on('SIGINT', () => { mongoose.connection.close(false).then(() => {
server.close(() => { // close the main process now that all http and database connections have closed
var mongoose = require('mongoose'); process.exit(0);
}).catch((err) => {
mongoose.connection.close(false).then(() => { console.log(err);
// close the main process now that all http and database connections have closed process.exit(1);
process.exit(0);
}).catch((err) => {
console.log(err);
process.exit(1);
});
}); });
}); });
}); });
+7
View File
@@ -286,6 +286,13 @@ module.exports = {
connect: function(database, cb) { connect: function(database, cb) {
mongoose.set('strictQuery', true); 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(() => { mongoose.connect(database).then(() => {
return cb(); return cb();
}).catch((err) => { }).catch((err) => {
+46 -38
View File
@@ -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 // 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) { 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) if (nodeVersion.indexOf('v') == 0)
nodeVersion = nodeVersion.slice(1); nodeVersion = nodeVersion.slice(1);
@@ -99,48 +99,56 @@ check_arguments_passed(function(pidName, node_env) {
// compile scss to css // compile scss to css
execSync('node ./scripts/compile_css.js', {stdio : 'inherit'}); execSync('node ./scripts/compile_css.js', {stdio : 'inherit'});
// check if the webserver should be started from here based on the pidName const db = require('../lib/database');
switch (pidName) {
case 'pm2':
let startOrReload = 'start';
// get a json list of pm2 processes // connect to the mongo database
let result = execSync(`pm2 jlist`); 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 // get a json list of pm2 processes
if (result != null) { let result = execSync(`pm2 jlist`);
try {
// convert return result to JSON
result = JSON.parse(result);
// loop through the results // check if the result is null
for (let i = 0; i < result.length; i++) { if (result != null) {
// check if this is an explorer process try {
if (result[i].name == 'explorer') { // convert return result to JSON
// explorer process exists, so reload the process result = JSON.parse(result);
startOrReload = 'reload';
break; // 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 // finished pre-loading
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'}); process.exit(0);
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);
}); });