work-in-progress commit to get cluster module working with loader.js

v1.18.x
Julian Lam 11 years ago
parent e6d0eede4f
commit 58a73b52b5

@ -2,109 +2,147 @@
var nconf = require('nconf'), var nconf = require('nconf'),
fs = require('fs'), fs = require('fs'),
pidFilePath = __dirname + '/pidfile', cluster = require('cluster'),
output = fs.openSync(__dirname + '/logs/output.log', 'a'), numCPUs = require('os').cpus().length;
start = function() {
var nbb_start = function(callback) { /* TODO
if (timesStarted > 3) { * pidFile and reset timer
console.log('\n[loader] Experienced three start attempts in 10 seconds, most likely an error on startup. Halting.'); * logging
return nbb_stop(); * handling SIGHUP
} * restart signal from child
*/
timesStarted++;
if (startTimer) { // pidFilePath = __dirname + '/pidfile',
clearTimeout(startTimer); // output = fs.openSync(__dirname + '/logs/output.log', 'a'),
} // start = function() {
startTimer = setTimeout(resetTimer, 1000*10); // var nbb_start = function(callback) {
// if (timesStarted > 3) {
if (nbb) { // console.log('\n[loader] Experienced three start attempts in 10 seconds, most likely an error on startup. Halting.');
nbbOld = nbb; // return nbb_stop();
} // }
nbb = require('child_process').fork('./app', process.argv.slice(2), { // timesStarted++;
env: process.env // if (startTimer) {
}); // clearTimeout(startTimer);
// }
nbb.on('message', function(message) { // startTimer = setTimeout(resetTimer, 1000*10);
if (message && typeof message === 'object' && message.action) {
switch (message.action) { // if (nbb) {
case 'ready': // nbbOld = nbb;
if (!callback) return nbb.send('bind'); // }
callback();
break; // nbb = require('child_process').fork('./app', process.argv.slice(2), {
case 'restart': // env: process.env
nbb_restart(); // });
break;
} // nbb.on('message', function(message) {
} // if (message && typeof message === 'object' && message.action) {
}); // switch (message.action) {
// case 'ready':
nbb.on('exit', function(code, signal) { // if (!callback) return nbb.send('bind');
if (code) { // callback();
nbb_start(); // break;
} else { // case 'restart':
nbb_stop(); // nbb_restart();
} // break;
}); // }
}, // }
nbb_stop = function() { // });
if (startTimer) {
clearTimeout(startTimer); // nbb.on('exit', function(code, signal) {
} // if (code) {
// nbb_start();
nbb.kill(); // } else {
if (fs.existsSync(pidFilePath)) { // nbb_stop();
var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10); // }
if (process.pid === pid) { // });
fs.unlinkSync(pidFilePath); // },
} // nbb_stop = function() {
} // if (startTimer) {
}, // clearTimeout(startTimer);
nbb_restart = function() { // }
nbb_start(function() {
nbbOld.removeAllListeners('exit').on('exit', function() { // nbb.kill();
nbb.send('bind'); // if (fs.existsSync(pidFilePath)) {
}); // var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10);
nbbOld.kill(); // if (process.pid === pid) {
}); // fs.unlinkSync(pidFilePath);
}, // }
resetTimer = function() { // }
clearTimeout(startTimer); // },
timesStarted = 0; // nbb_restart = function() {
}, // nbb_start(function() {
timesStarted = 0, // nbbOld.removeAllListeners('exit').on('exit', function() {
startTimer; // nbb.send('bind');
// });
process.on('SIGINT', nbb_stop); // nbbOld.kill();
process.on('SIGTERM', nbb_stop); // });
process.on('SIGHUP', nbb_restart); // },
// resetTimer = function() {
nbb_start(); // clearTimeout(startTimer);
}, // timesStarted = 0;
nbb, nbbOld; // },
// timesStarted = 0,
// startTimer;
// process.on('SIGINT', nbb_stop);
// process.on('SIGTERM', nbb_stop);
// process.on('SIGHUP', nbb_restart);
// nbb_start();
// },
// nbb, nbbOld;
nconf.argv(); nconf.argv();
// Start the daemon! cluster.setupMaster({
if (nconf.get('daemon') !== false) { exec: "app.js",
// Check for a still-active NodeBB process silent: true
if (fs.existsSync(pidFilePath)) { });
try {
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); for(var x=0;x<numCPUs;x++) {
process.kill(pid, 0); cluster.fork();
process.exit(); }
} catch (e) {
fs.unlinkSync(pidFilePath); cluster.on('exit', function(worker, code, signal) {
} console.log('worker ' + worker.process.pid + ' died');
} });
// Daemonize and record new pid Object.keys(cluster.workers).forEach(function(id) {
require('daemon')({ cluster.workers[id].on('message', function(message) {
stdout: output if (message && typeof message === 'object' && message.action) {
switch (message.action) {
case 'ready':
cluster.workers[id].send('bind');
break;
// case 'restart':
// nbb_restart();
// break;
}
}
}); });
fs.writeFile(__dirname + '/pidfile', process.pid); });
start(); // Start the daemon!
} else { // if (nconf.get('daemon') !== false) {
start(); // // Check for a still-active NodeBB process
} // if (fs.existsSync(pidFilePath)) {
// try {
// var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
// process.kill(pid, 0);
// process.exit();
// } catch (e) {
// fs.unlinkSync(pidFilePath);
// }
// }
// // Daemonize and record new pid
// require('daemon')({
// stdout: output
// });
// fs.writeFile(__dirname + '/pidfile', process.pid);
// start();
// } else {
// start();
// }

@ -22,6 +22,7 @@ var utils = require('./../../public/src/utils'),
multipart = require('connect-multiparty'), multipart = require('connect-multiparty'),
csrf = require('csurf'), csrf = require('csurf'),
session = require('express-session'), session = require('express-session'),
cluster = require('cluster'),
relativePath, relativePath,
themesPath; themesPath;
@ -128,7 +129,13 @@ module.exports = function(app, data) {
routeCurrentTheme(app, data.currentThemeId, data.themesData); routeCurrentTheme(app, data.currentThemeId, data.themesData);
routeThemeScreenshots(app, data.themesData); routeThemeScreenshots(app, data.themesData);
meta.templates.compile(); if (cluster.worker.id === 1) {
meta.templates.compile();
} else {
setTimeout(function() {
emitter.emit('templates:compiled');
}, 1000);
}
return middleware; return middleware;
}; };

@ -198,13 +198,6 @@ module.exports = function(app, middleware) {
// Add plugin routes // Add plugin routes
plugins.reloadRoutes(); plugins.reloadRoutes();
// plugins.ready(function() {
// plugins.fireHook('static:app.load', pluginRouter, middleware, controllers, function() {
// });
// });
}; };
function handleErrors(err, req, res, next) { function handleErrors(err, req, res, next) {

@ -6,6 +6,7 @@ var path = require('path'),
server, server,
winston = require('winston'), winston = require('winston'),
async = require('async'), async = require('async'),
cluster = require('cluster'),
emailer = require('./emailer'), emailer = require('./emailer'),
db = require('./database'), db = require('./database'),
@ -39,13 +40,19 @@ if(nconf.get('ssl')) {
auth.registerApp(app); auth.registerApp(app);
emailer.registerApp(app); emailer.registerApp(app);
notifications.init(); notifications.init();
user.startJobs();
if (cluster.worker.id === 1) {
user.startJobs();
}
// Preparation dependent on plugins // Preparation dependent on plugins
plugins.ready(function() { plugins.ready(function() {
meta.js.minify(app.enabled('minification')); meta.js.minify(app.enabled('minification'));
meta.css.minify(); meta.css.minify();
meta.sounds.init();
if (cluster.worker.id === 1) {
meta.sounds.init();
}
}); });
async.parallel({ async.parallel({

Loading…
Cancel
Save