mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
work-in-progress commit to get cluster module working with loader.js
This commit is contained in:
224
loader.js
224
loader.js
@@ -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) {
|
|
||||||
if (timesStarted > 3) {
|
|
||||||
console.log('\n[loader] Experienced three start attempts in 10 seconds, most likely an error on startup. Halting.');
|
|
||||||
return nbb_stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
timesStarted++;
|
/* TODO
|
||||||
if (startTimer) {
|
* pidFile and reset timer
|
||||||
clearTimeout(startTimer);
|
* logging
|
||||||
}
|
* handling SIGHUP
|
||||||
startTimer = setTimeout(resetTimer, 1000*10);
|
* restart signal from child
|
||||||
|
*/
|
||||||
|
|
||||||
if (nbb) {
|
// pidFilePath = __dirname + '/pidfile',
|
||||||
nbbOld = nbb;
|
// output = fs.openSync(__dirname + '/logs/output.log', 'a'),
|
||||||
}
|
// start = function() {
|
||||||
|
// var nbb_start = function(callback) {
|
||||||
|
// if (timesStarted > 3) {
|
||||||
|
// console.log('\n[loader] Experienced three start attempts in 10 seconds, most likely an error on startup. Halting.');
|
||||||
|
// return nbb_stop();
|
||||||
|
// }
|
||||||
|
|
||||||
nbb = require('child_process').fork('./app', process.argv.slice(2), {
|
// timesStarted++;
|
||||||
env: process.env
|
// if (startTimer) {
|
||||||
});
|
// clearTimeout(startTimer);
|
||||||
|
// }
|
||||||
|
// startTimer = setTimeout(resetTimer, 1000*10);
|
||||||
|
|
||||||
nbb.on('message', function(message) {
|
// if (nbb) {
|
||||||
if (message && typeof message === 'object' && message.action) {
|
// nbbOld = nbb;
|
||||||
switch (message.action) {
|
// }
|
||||||
case 'ready':
|
|
||||||
if (!callback) return nbb.send('bind');
|
|
||||||
callback();
|
|
||||||
break;
|
|
||||||
case 'restart':
|
|
||||||
nbb_restart();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
nbb.on('exit', function(code, signal) {
|
// nbb = require('child_process').fork('./app', process.argv.slice(2), {
|
||||||
if (code) {
|
// env: process.env
|
||||||
nbb_start();
|
// });
|
||||||
} else {
|
|
||||||
nbb_stop();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
nbb_stop = function() {
|
|
||||||
if (startTimer) {
|
|
||||||
clearTimeout(startTimer);
|
|
||||||
}
|
|
||||||
|
|
||||||
nbb.kill();
|
// nbb.on('message', function(message) {
|
||||||
if (fs.existsSync(pidFilePath)) {
|
// if (message && typeof message === 'object' && message.action) {
|
||||||
var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10);
|
// switch (message.action) {
|
||||||
if (process.pid === pid) {
|
// case 'ready':
|
||||||
fs.unlinkSync(pidFilePath);
|
// if (!callback) return nbb.send('bind');
|
||||||
}
|
// callback();
|
||||||
}
|
// break;
|
||||||
},
|
// case 'restart':
|
||||||
nbb_restart = function() {
|
// nbb_restart();
|
||||||
nbb_start(function() {
|
// break;
|
||||||
nbbOld.removeAllListeners('exit').on('exit', function() {
|
// }
|
||||||
nbb.send('bind');
|
// }
|
||||||
});
|
// });
|
||||||
nbbOld.kill();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
resetTimer = function() {
|
|
||||||
clearTimeout(startTimer);
|
|
||||||
timesStarted = 0;
|
|
||||||
},
|
|
||||||
timesStarted = 0,
|
|
||||||
startTimer;
|
|
||||||
|
|
||||||
process.on('SIGINT', nbb_stop);
|
// nbb.on('exit', function(code, signal) {
|
||||||
process.on('SIGTERM', nbb_stop);
|
// if (code) {
|
||||||
process.on('SIGHUP', nbb_restart);
|
// nbb_start();
|
||||||
|
// } else {
|
||||||
|
// nbb_stop();
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// },
|
||||||
|
// nbb_stop = function() {
|
||||||
|
// if (startTimer) {
|
||||||
|
// clearTimeout(startTimer);
|
||||||
|
// }
|
||||||
|
|
||||||
nbb_start();
|
// nbb.kill();
|
||||||
},
|
// if (fs.existsSync(pidFilePath)) {
|
||||||
nbb, nbbOld;
|
// var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10);
|
||||||
|
// if (process.pid === pid) {
|
||||||
|
// fs.unlinkSync(pidFilePath);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// nbb_restart = function() {
|
||||||
|
// nbb_start(function() {
|
||||||
|
// nbbOld.removeAllListeners('exit').on('exit', function() {
|
||||||
|
// nbb.send('bind');
|
||||||
|
// });
|
||||||
|
// nbbOld.kill();
|
||||||
|
// });
|
||||||
|
// },
|
||||||
|
// resetTimer = function() {
|
||||||
|
// clearTimeout(startTimer);
|
||||||
|
// timesStarted = 0;
|
||||||
|
// },
|
||||||
|
// 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' });
|
|
||||||
process.kill(pid, 0);
|
|
||||||
process.exit();
|
|
||||||
} catch (e) {
|
|
||||||
fs.unlinkSync(pidFilePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Daemonize and record new pid
|
for(var x=0;x<numCPUs;x++) {
|
||||||
require('daemon')({
|
cluster.fork();
|
||||||
stdout: output
|
|
||||||
});
|
|
||||||
fs.writeFile(__dirname + '/pidfile', process.pid);
|
|
||||||
|
|
||||||
start();
|
|
||||||
} else {
|
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cluster.on('exit', function(worker, code, signal) {
|
||||||
|
console.log('worker ' + worker.process.pid + ' died');
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(cluster.workers).forEach(function(id) {
|
||||||
|
cluster.workers[id].on('message', function(message) {
|
||||||
|
if (message && typeof message === 'object' && message.action) {
|
||||||
|
switch (message.action) {
|
||||||
|
case 'ready':
|
||||||
|
cluster.workers[id].send('bind');
|
||||||
|
break;
|
||||||
|
// case 'restart':
|
||||||
|
// nbb_restart();
|
||||||
|
// break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the daemon!
|
||||||
|
// if (nconf.get('daemon') !== false) {
|
||||||
|
// // 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({
|
||||||
|
|||||||
Reference in New Issue
Block a user