Skip to content

Node.js based forum software built for the modern web

fixed #2259

Julian Lam97dba9ae39b8
diff --git a/app.js b/app.js
index 052ddb595d..ac339fedc3 100644
--- a/app.js
+++ b/app.js
@@ -58,11 +58,13 @@ if(os.platform() === 'linux') {
});
}
-// Log GNU copyright info along with server info
-winston.info('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.');
-winston.info('This program comes with ABSOLUTELY NO WARRANTY.');
-winston.info('This is free software, and you are welcome to redistribute it under certain conditions.');
-winston.info('');
+if (!cluster.isWorker) {
+ // If run using `node app`, log GNU copyright info along with server info
+ winston.info('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.');
+ winston.info('This program comes with ABSOLUTELY NO WARRANTY.');
+ winston.info('This is free software, and you are welcome to redistribute it under certain conditions.');
+ winston.info('');
+}
// Alternate configuration file support
var configFile = path.join(__dirname, '/config.json'),
@@ -102,20 +104,20 @@ function loadConfig() {
}
function start() {
-
loadConfig();
- winston.info('Time: ' + new Date());
- winston.info('Initializing NodeBB v' + pkg.version);
- winston.info('* using configuration stored in: ' + configFile);
- var host = nconf.get(nconf.get('database') + ':host'),
- storeLocation = host ? 'at ' + host + (host.indexOf('/') === -1 ? ':' + nconf.get(nconf.get('database') + ':port') : '') : '';
+ if (!cluster.isWorker) {
+ winston.info('Time: ' + new Date());
+ winston.info('Initializing NodeBB v' + pkg.version);
+ winston.info('* using configuration stored in: ' + configFile);
+ }
- winston.info('* using ' + nconf.get('database') +' store ' + storeLocation);
- winston.info('* using themes stored in: ' + nconf.get('themes_path'));
+ if (cluster.isWorker && process.env.cluster_setup === 'true') {
+ var host = nconf.get(nconf.get('database') + ':host'),
+ storeLocation = host ? 'at ' + host + (host.indexOf('/') === -1 ? ':' + nconf.get(nconf.get('database') + ':port') : '') : '';
- if (process.env.NODE_ENV === 'development') {
- winston.info('Base Configuration OK.');
+ winston.info('* using ' + nconf.get('database') +' store ' + storeLocation);
+ winston.info('* using themes stored in: ' + nconf.get('themes_path'));
}
require('./src/database').init(function(err) {
diff --git a/loader.js b/loader.js
index 0b384ba8a1..aee0b18c44 100644
--- a/loader.js
+++ b/loader.js
@@ -6,10 +6,14 @@ var nconf = require('nconf'),
cluster = require('cluster'),
async = require('async'),
logrotate = require('logrotate-stream'),
+
+ pkg = require('./package.json'),
+
pidFilePath = __dirname + '/pidfile',
output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
silent = process.env.NODE_ENV !== 'development' ? true : false,
- numCPUs,
+ numProcs,
+
Loader = {
timesStarted: 0,
shutdown_queue: [],
@@ -23,7 +27,7 @@ var nconf = require('nconf'),
}
};
-Loader.init = function() {
+Loader.init = function(callback) {
cluster.setupMaster({
exec: "app.js",
silent: silent
@@ -36,6 +40,20 @@ Loader.init = function() {
};
}
+ process.on('SIGHUP', Loader.restart);
+ callback();
+};
+
+Loader.displayStartupMessages = function(callback) {
+ console.log('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.');
+ console.log('This program comes with ABSOLUTELY NO WARRANTY.');
+ console.log('This is free software, and you are welcome to redistribute it under certain conditions.');
+ console.log('For the full license, please visit: http://www.gnu.org/copyleft/gpl.html');
+ console.log('');
+ callback();
+};
+
+Loader.addClusterEvents = function(callback) {
cluster.on('fork', function(worker) {
worker.on('message', function(message) {
if (message && typeof message === 'object' && message.action) {
@@ -115,7 +133,7 @@ Loader.init = function() {
case 'user:connect':
case 'user:disconnect':
case 'config:update':
- notifyWorkers(message);
+ Loader.notifyWorkers(message);
break;
}
}
@@ -126,15 +144,9 @@ Loader.init = function() {
console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.');
});
- function notifyWorkers(msg) {
- Object.keys(cluster.workers).forEach(function(id) {
- cluster.workers[id].send(msg);
- });
- }
-
cluster.on('exit', function(worker, code, signal) {
if (code !== 0) {
- if (Loader.timesStarted < numCPUs*3) {
+ if (Loader.timesStarted < numProcs*3) {
Loader.timesStarted++;
if (Loader.crashTimer) {
clearTimeout(Loader.crashTimer);
@@ -143,7 +155,7 @@ Loader.init = function() {
Loader.timesStarted = 0;
});
} else {
- console.log(numCPUs*3 + ' restarts in 10 seconds, most likely an error on startup. Halting.');
+ console.log(numProcs*3 + ' restarts in 10 seconds, most likely an error on startup. Halting.');
process.exit();
}
}
@@ -159,16 +171,16 @@ Loader.init = function() {
}
});
- process.on('SIGHUP', Loader.restart);
-
- Loader.start();
-};
+ callback();
+}
-Loader.start = function() {
+Loader.start = function(callback) {
var output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
worker;
- for(var x=0;x<numCPUs;x++) {
+ console.log('Clustering enabled: Spinning up ' + numProcs + ' process(es).\n');
+
+ for(var x=0;x<numProcs;x++) {
// Only the first worker sets up templates/sounds/jobs/etc
worker = cluster.fork({
cluster_setup: x === 0,
@@ -180,6 +192,8 @@ Loader.start = function() {
worker.process.stdout.pipe(output);
}
}
+
+ callback();
};
Loader.restart = function(callback) {
@@ -196,15 +210,19 @@ Loader.reload = function() {
});
};
-
+Loader.notifyWorkers = function (msg) {
+ Object.keys(cluster.workers).forEach(function(id) {
+ cluster.workers[id].send(msg);
+ });
+}
nconf.argv().file({
file: path.join(__dirname, '/config.json')
});
-numCPUs = nconf.get('cluster') || 1;
-numCPUs = (numCPUs === true) ? require('os').cpus().length : numCPUs;
+numProcs = nconf.get('cluster') || 1;
+numProcs = (numProcs === true) ? require('os').cpus().length : numProcs;
if (nconf.get('daemon') !== false) {
if (fs.existsSync(pidFilePath)) {
@@ -222,4 +240,13 @@ if (nconf.get('daemon') !== false) {
fs.writeFile(__dirname + '/pidfile', process.pid);
}
-Loader.init();
\ No newline at end of file
+async.series([
+ Loader.init,
+ Loader.displayStartupMessages,
+ Loader.addClusterEvents,
+ Loader.start
+], function(err) {
+ if (err) {
+ console.log('[loader] Error during startup: ' + err.message);
+ }
+});
\ No newline at end of file