Revert "reverting #2259 temporarily until we figure out why logs aren't showing up"

This reverts commit 59b3e6f78f.
v1.18.x
psychobunny 10 years ago
parent 59b3e6f78f
commit 8ef4ad6f14

@ -58,11 +58,13 @@ if(os.platform() === 'linux') {
}); });
} }
// Log GNU copyright info along with server info if (!cluster.isWorker) {
winston.info('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.'); // If run using `node app`, log GNU copyright info along with server info
winston.info('This program comes with ABSOLUTELY NO WARRANTY.'); winston.info('NodeBB v' + pkg.version + ' Copyright (C) 2013-2014 NodeBB Inc.');
winston.info('This is free software, and you are welcome to redistribute it under certain conditions.'); winston.info('This program comes with ABSOLUTELY NO WARRANTY.');
winston.info(''); winston.info('This is free software, and you are welcome to redistribute it under certain conditions.');
winston.info('');
}
// Alternate configuration file support // Alternate configuration file support
var configFile = path.join(__dirname, '/config.json'), var configFile = path.join(__dirname, '/config.json'),
@ -102,20 +104,20 @@ function loadConfig() {
} }
function start() { function start() {
loadConfig(); loadConfig();
winston.info('Time: ' + new Date()); if (!cluster.isWorker) {
winston.info('Initializing NodeBB v' + pkg.version); winston.info('Time: ' + new Date());
winston.info('* using configuration stored in: ' + configFile); winston.info('Initializing NodeBB v' + pkg.version);
var host = nconf.get(nconf.get('database') + ':host'), winston.info('* using configuration stored in: ' + configFile);
storeLocation = host ? 'at ' + host + (host.indexOf('/') === -1 ? ':' + nconf.get(nconf.get('database') + ':port') : '') : ''; }
winston.info('* using ' + nconf.get('database') +' store ' + storeLocation); if (cluster.isWorker && process.env.cluster_setup === 'true') {
winston.info('* using themes stored in: ' + nconf.get('themes_path')); 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('* using ' + nconf.get('database') +' store ' + storeLocation);
winston.info('Base Configuration OK.'); winston.info('* using themes stored in: ' + nconf.get('themes_path'));
} }
require('./src/database').init(function(err) { require('./src/database').init(function(err) {

@ -6,10 +6,14 @@ var nconf = require('nconf'),
cluster = require('cluster'), cluster = require('cluster'),
async = require('async'), async = require('async'),
logrotate = require('logrotate-stream'), logrotate = require('logrotate-stream'),
pkg = require('./package.json'),
pidFilePath = __dirname + '/pidfile', pidFilePath = __dirname + '/pidfile',
output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }), output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
silent = process.env.NODE_ENV !== 'development' ? true : false, silent = process.env.NODE_ENV !== 'development' ? true : false,
numCPUs, numProcs,
Loader = { Loader = {
timesStarted: 0, timesStarted: 0,
shutdown_queue: [], shutdown_queue: [],
@ -23,7 +27,7 @@ var nconf = require('nconf'),
} }
}; };
Loader.init = function() { Loader.init = function(callback) {
cluster.setupMaster({ cluster.setupMaster({
exec: "app.js", exec: "app.js",
silent: silent 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) { cluster.on('fork', function(worker) {
worker.on('message', function(message) { worker.on('message', function(message) {
if (message && typeof message === 'object' && message.action) { if (message && typeof message === 'object' && message.action) {
@ -115,7 +133,7 @@ Loader.init = function() {
case 'user:connect': case 'user:connect':
case 'user:disconnect': case 'user:disconnect':
case 'config:update': case 'config:update':
notifyWorkers(message); Loader.notifyWorkers(message);
break; break;
} }
} }
@ -126,15 +144,9 @@ Loader.init = function() {
console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.'); 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) { cluster.on('exit', function(worker, code, signal) {
if (code !== 0) { if (code !== 0) {
if (Loader.timesStarted < numCPUs*3) { if (Loader.timesStarted < numProcs*3) {
Loader.timesStarted++; Loader.timesStarted++;
if (Loader.crashTimer) { if (Loader.crashTimer) {
clearTimeout(Loader.crashTimer); clearTimeout(Loader.crashTimer);
@ -143,7 +155,7 @@ Loader.init = function() {
Loader.timesStarted = 0; Loader.timesStarted = 0;
}); });
} else { } 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(); process.exit();
} }
} }
@ -159,16 +171,16 @@ Loader.init = function() {
} }
}); });
process.on('SIGHUP', Loader.restart); callback();
}
Loader.start();
};
Loader.start = function() { Loader.start = function(callback) {
var output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }), var output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
worker; 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 // Only the first worker sets up templates/sounds/jobs/etc
worker = cluster.fork({ worker = cluster.fork({
cluster_setup: x === 0, cluster_setup: x === 0,
@ -180,6 +192,8 @@ Loader.start = function() {
worker.process.stdout.pipe(output); worker.process.stdout.pipe(output);
} }
} }
if (callback) callback();
}; };
Loader.restart = function(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({ nconf.argv().file({
file: path.join(__dirname, '/config.json') file: path.join(__dirname, '/config.json')
}); });
numCPUs = nconf.get('cluster') || 1; numProcs = nconf.get('cluster') || 1;
numCPUs = (numCPUs === true) ? require('os').cpus().length : numCPUs; numProcs = (numProcs === true) ? require('os').cpus().length : numProcs;
if (nconf.get('daemon') !== false) { if (nconf.get('daemon') !== false) {
if (fs.existsSync(pidFilePath)) { if (fs.existsSync(pidFilePath)) {
@ -222,4 +240,13 @@ if (nconf.get('daemon') !== false) {
fs.writeFile(__dirname + '/pidfile', process.pid); fs.writeFile(__dirname + '/pidfile', process.pid);
} }
Loader.init(); async.series([
Loader.init,
Loader.displayStartupMessages,
Loader.addClusterEvents,
Loader.start
], function(err) {
if (err) {
console.log('[loader] Error during startup: ' + err.message);
}
});
Loading…
Cancel
Save