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) {
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);
+13 -22
View File
@@ -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);
});
});
});
+7
View File
@@ -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) => {
+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
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);
});
});
});