cluster socket.io fixes

close proxy server on loader.stop
changed handles to object
v1.18.x
barisusakli 10 years ago
parent 785790c414
commit 1abe35092b

@ -121,6 +121,9 @@ function start() {
winston.verbose('* using themes stored in: %s', nconf.get('themes_path')); winston.verbose('* using themes stored in: %s', nconf.get('themes_path'));
} }
var webserver = require('./src/webserver');
require('./src/database').init(function(err) { require('./src/database').init(function(err) {
if (err) { if (err) {
winston.error(err.stack); winston.error(err.stack);
@ -129,17 +132,24 @@ function start() {
var meta = require('./src/meta'); var meta = require('./src/meta');
meta.configs.init(function () { meta.configs.init(function () {
var templates = require('templates.js'), var templates = require('templates.js'),
webserver = require('./src/webserver'),
sockets = require('./src/socket.io'), sockets = require('./src/socket.io'),
plugins = require('./src/plugins'), plugins = require('./src/plugins'),
upgrade = require('./src/upgrade'); upgrade = require('./src/upgrade');
meta.themes.setupPaths();
templates.setGlobal('relative_path', nconf.get('relative_path')); templates.setGlobal('relative_path', nconf.get('relative_path'));
upgrade.check(function(schema_ok) { upgrade.check(function(schema_ok) {
if (schema_ok || nconf.get('check-schema') === false) { if (schema_ok || nconf.get('check-schema') === false) {
webserver.init();
sockets.init(webserver.server); sockets.init(webserver.server);
if (cluster.isWorker && process.env.handle_jobs === 'true') {
require('./src/notifications').init();
require('./src/user').startJobs();
}
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path')); nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path'));
async.waterfall([ async.waterfall([

@ -14,8 +14,9 @@ var nconf = require('nconf'),
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,
numProcs, numProcs,
handles = [], handles = {},
handleIndex = 0, handleIndex = 0,
server,
Loader = { Loader = {
timesStarted: 0, timesStarted: 0,
@ -191,10 +192,9 @@ Loader.start = function(callback) {
var port = nconf.get('PORT') || nconf.get('port'); var port = nconf.get('PORT') || nconf.get('port');
var server = net.createServer(function(connection) { server = net.createServer(function(connection) {
// remove this once node 0.12.x ships, see https://github.com/elad/node-cluster-socket.io/issues/4 // remove this once node 0.12.x ships, see https://github.com/elad/node-cluster-socket.io/issues/4
connection._handle.readStop(); connection._handle.readStop();
handles[handleIndex] = connection._handle; handles[handleIndex] = connection._handle;
var workers = clusterWorkers(); var workers = clusterWorkers();
@ -277,6 +277,8 @@ Loader.stop = function() {
// Clean up the pidfile // Clean up the pidfile
fs.unlinkSync(__dirname + '/pidfile'); fs.unlinkSync(__dirname + '/pidfile');
server.close();
}; };
Loader.notifyWorkers = function (msg) { Loader.notifyWorkers = function (msg) {

@ -2,6 +2,7 @@
'use strict'; 'use strict';
var nconf = require('nconf'), var nconf = require('nconf'),
winston = require('winston'),
fs = require('fs'), fs = require('fs'),
path = require('path'), path = require('path'),
async = require('async'), async = require('async'),
@ -107,6 +108,32 @@ module.exports = function(Meta) {
} }
}; };
Meta.themes.setupPaths = function() {
async.parallel({
themesData: Meta.themes.get,
currentThemeId: function(next) {
db.getObjectField('config', 'theme:id', next);
}
}, function(err, data) {
if (err) {
return winston.error(err.stack);
}
var themeId = data.currentThemeId || 'nodebb-theme-vanilla';
var themeObj = data.themesData.filter(function(themeObj) {
return themeObj.id === themeId;
})[0];
if (process.env.NODE_ENV === 'development') {
winston.info('[themes] Using theme ' + themeId);
}
Meta.themes.setPath(themeObj);
});
};
Meta.themes.setPath = function(themeObj) { Meta.themes.setPath = function(themeObj) {
// Theme's templates path // Theme's templates path
var themePath = nconf.get('base_templates_path'), var themePath = nconf.get('base_templates_path'),
@ -121,4 +148,6 @@ module.exports = function(Meta) {
nconf.set('theme_templates_path', themePath); nconf.set('theme_templates_path', themePath);
nconf.set('theme_config', path.join(nconf.get('themes_path'), themeObj.id, 'theme.json')); nconf.set('theme_config', path.join(nconf.get('themes_path'), themeObj.id, 'theme.json'));
}; };
}; };

@ -19,22 +19,6 @@ var meta = require('../meta'),
var middleware = {}; var middleware = {};
function routeCurrentTheme(app, themeId, themesData) {
themeId = (themeId || 'nodebb-theme-vanilla');
var themeObj = (function(id) {
return themesData.filter(function(themeObj) {
return themeObj.id === id;
})[0];
})(themeId);
if (process.env.NODE_ENV === 'development') {
winston.info('[themes] Using theme ' + themeId);
}
meta.themes.setPath(themeObj);
}
function setupFavicon(app) { function setupFavicon(app) {
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico'); var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
if (fs.existsSync(faviconPath)) { if (fs.existsSync(faviconPath)) {
@ -42,14 +26,11 @@ function setupFavicon(app) {
} }
} }
module.exports = function(app, data) { module.exports = function(app) {
var relativePath, themesPath; var relativePath = nconf.get('relative_path');
middleware = require('./middleware')(app); middleware = require('./middleware')(app);
relativePath = nconf.get('relative_path');
themesPath = nconf.get('themes_path');
app.engine('tpl', templates.__express); app.engine('tpl', templates.__express);
app.set('view engine', 'tpl'); app.set('view engine', 'tpl');
app.set('views', nconf.get('views_dir')); app.set('views', nconf.get('views_dir'));
@ -72,7 +53,7 @@ module.exports = function(app, data) {
maxAge: 1000 * 60 * 60 * 24 * parseInt(meta.config.loginDays || 14, 10) maxAge: 1000 * 60 * 60 * 24 * parseInt(meta.config.loginDays || 14, 10)
}; };
if(meta.config.cookieDomain) { if (meta.config.cookieDomain) {
cookie.domain = meta.config.cookieDomain; cookie.domain = meta.config.cookieDomain;
} }
@ -98,7 +79,6 @@ module.exports = function(app, data) {
app.use(middleware.processRender); app.use(middleware.processRender);
auth.initialize(app, middleware); auth.initialize(app, middleware);
routeCurrentTheme(app, data.currentThemeId, data.themesData);
return middleware; return middleware;
}; };

@ -1,3 +1,6 @@
'use strict';
var path = require('path'), var path = require('path'),
fs = require('fs'), fs = require('fs'),
nconf = require('nconf'), nconf = require('nconf'),
@ -9,11 +12,7 @@ var path = require('path'),
cluster = require('cluster'), cluster = require('cluster'),
emailer = require('./emailer'), emailer = require('./emailer'),
db = require('./database'),
auth = require('./routes/authentication'),
meta = require('./meta'), meta = require('./meta'),
user = require('./user'),
notifications = require('./notifications'),
logger = require('./logger'), logger = require('./logger'),
plugins = require('./plugins'), plugins = require('./plugins'),
middleware = require('./middleware'), middleware = require('./middleware'),
@ -32,73 +31,51 @@ if(nconf.get('ssl')) {
} }
(function (app) { (function (app) {
"use strict";
var port = nconf.get('PORT') || nconf.get('port'); var port = nconf.get('PORT') || nconf.get('port');
logger.init(app); module.exports.init = function() {
emailer.registerApp(app); emailer.registerApp(app);
if (cluster.isWorker && process.env.handle_jobs === 'true') {
notifications.init();
user.startJobs();
}
// Preparation dependent on plugins
plugins.ready(function() {
async.parallel([
async.apply(!nconf.get('from-file') ? meta.js.minify : meta.js.getFromFile, app.enabled('minification')),
async.apply(!nconf.get('from-file') ? meta.css.minify : meta.css.getFromFile),
async.apply(meta.sounds.init)
]);
});
async.parallel({ // Preparation dependent on plugins
themesData: meta.themes.get, plugins.ready(function() {
currentThemeId: function(next) { async.parallel([
db.getObjectField('config', 'theme:id', next); async.apply(!nconf.get('from-file') ? meta.js.minify : meta.js.getFromFile, app.enabled('minification')),
} async.apply(!nconf.get('from-file') ? meta.css.minify : meta.css.getFromFile),
}, function(err, data) { async.apply(meta.sounds.init)
middleware = middleware(app, data); ]);
});
middleware = middleware(app);
routes(app, middleware); routes(app, middleware);
if (err) { // Cache static files on production
winston.error('Errors were encountered while attempting to initialise NodeBB.'); if (global.env !== 'development') {
process.exit(); app.enable('cache');
} else { app.enable('minification');
if (process.env.NODE_ENV === 'development') {
winston.info('Middlewares loaded.'); // Configure cache-buster timestamp
} require('child_process').exec('git describe --tags', {
cwd: path.join(__dirname, '../')
}, function(err, stdOut) {
if (!err) {
meta.config['cache-buster'] = stdOut.trim();
} else {
fs.stat(path.join(__dirname, '../package.json'), function(err, stats) {
meta.config['cache-buster'] = new Date(stats.mtime).getTime();
});
}
});
} }
});
// Cache static files on production if (port !== 80 && port !== 443 && nconf.get('use_port') === false) {
if (global.env !== 'development') { winston.info('Enabling \'trust proxy\'');
app.enable('cache'); app.enable('trust proxy');
app.enable('minification'); }
// Configure cache-buster timestamp
require('child_process').exec('git describe --tags', {
cwd: path.join(__dirname, '../')
}, function(err, stdOut) {
if (!err) {
meta.config['cache-buster'] = stdOut.trim();
} else {
fs.stat(path.join(__dirname, '../package.json'), function(err, stats) {
meta.config['cache-buster'] = new Date(stats.mtime).getTime();
});
}
});
}
if (port !== 80 && port !== 443 && nconf.get('use_port') === false) {
winston.info('Enabling \'trust proxy\'');
app.enable('trust proxy');
}
if ((port === 80 || port === 443) && process.env.NODE_ENV !== 'development') { if ((port === 80 || port === 443) && process.env.NODE_ENV !== 'development') {
winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md'); winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md');
} }
};
server.on('error', function(err) { server.on('error', function(err) {
winston.error(err.stack); winston.error(err.stack);
@ -123,6 +100,8 @@ if(nconf.get('ssl')) {
}); });
module.exports.listen = function(callback) { module.exports.listen = function(callback) {
logger.init(app);
var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port; var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port;
if (cluster.isWorker) { if (cluster.isWorker) {
port = 0; port = 0;

Loading…
Cancel
Save