restart handling and crash detection

v1.18.x
Julian Lam 11 years ago
parent c40355b816
commit de41896770

@ -17,7 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
"use strict"; "use strict";
/*global require, global, process*/ /*global require, global, process*/

@ -3,6 +3,7 @@
var nconf = require('nconf'), var nconf = require('nconf'),
fs = require('fs'), fs = require('fs'),
cluster = require('cluster'), cluster = require('cluster'),
async = require('async'),
numCPUs = require('os').cpus().length; numCPUs = require('os').cpus().length;
/* TODO /* TODO
@ -93,36 +94,66 @@ var nconf = require('nconf'),
// }, // },
// nbb, nbbOld; // nbb, nbbOld;
var Loader = {
timesStarted: 0
};
Loader.init = function() {
nconf.argv(); nconf.argv();
cluster.setupMaster({ cluster.setupMaster({
exec: "app.js", exec: "app.js",
silent: false silent: true
}); });
for(var x=0;x<numCPUs;x++) { for(var x=0;x<numCPUs;x++) {
cluster.fork(); // Only the first worker sets up templates/sounds/jobs/etc
cluster.fork({ cluster_setup: x === 0 });
} }
cluster.on('exit', function(worker, code, signal) { cluster.on('fork', function(worker) {
console.log('worker ' + worker.process.pid + ' died'); worker.on('message', function(message) {
});
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].on('message', function(message) {
if (message && typeof message === 'object' && message.action) { if (message && typeof message === 'object' && message.action) {
switch (message.action) { switch (message.action) {
case 'ready': case 'ready':
cluster.workers[id].send('bind'); console.log('[cluster] Child Process (' + worker.process.pid + ') listening for connections.');
worker.send('bind');
break;
case 'restart':
console.log('[cluster] Restarting...');
Loader.restart(function(err) {
console.log('[cluster] Restarting...');
});
break; break;
// case 'restart':
// nbb_restart();
// break;
} }
} }
}); });
}); });
cluster.on('exit', function(worker, code, signal) {
if (code !== 0) {
if (Loader.timesStarted < numCPUs*3) {
Loader.timesStarted++;
} else {
console.log(numCPUs*3, 'restarts in 10 seconds, most likely an error on startup. Halting.');
process.exit();
}
}
console.log('[cluster] Child Process (' + worker.process.pid + ') has exited (code: ' + code + ')');
cluster.fork();
});
};
Loader.restart = function(callback) {
async.eachSeries(Object.keys(cluster.workers), function(id, next) {
cluster.workers[id].kill();
next();
}, callback);
}
Loader.init();
// Start the daemon! // Start the daemon!
// if (nconf.get('daemon') !== false) { // if (nconf.get('daemon') !== false) {
// // Check for a still-active NodeBB process // // Check for a still-active NodeBB process

@ -129,7 +129,7 @@ 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);
if (cluster.worker.id === 1) { if (process.env.cluster_setup === 'true') {
meta.templates.compile(); meta.templates.compile();
} else { } else {
setTimeout(function() { setTimeout(function() {

@ -56,6 +56,10 @@ module.exports = function(app, middleware, controllers) {
}); });
router.get('/test', function(req, res) { router.get('/test', function(req, res) {
res.redirect(404); var cluster = require('cluster');
console.log('answered by worker', cluster.worker.id);
res.send(200);
process.exit();
// res.redirect(404);
}); });
}; };

@ -41,7 +41,7 @@ if(nconf.get('ssl')) {
emailer.registerApp(app); emailer.registerApp(app);
notifications.init(); notifications.init();
if (cluster.worker.id === 1) { if (process.env.cluster_setup === 'true') {
user.startJobs(); user.startJobs();
} }
@ -50,7 +50,7 @@ if(nconf.get('ssl')) {
meta.js.minify(app.enabled('minification')); meta.js.minify(app.enabled('minification'));
meta.css.minify(); meta.css.minify();
if (cluster.worker.id === 1) { if (process.env.cluster_setup === 'true') {
meta.sounds.init(); meta.sounds.init();
} }
}); });

Loading…
Cancel
Save