Skip to content

Node.js based forum software built for the modern web

cluster socket.io fixes

barisusakli1abe35092b9f
diff --git a/app.js b/app.js
index 768ac48c64..7a04ee64c9 100644
--- a/app.js
+++ b/app.js
@@ -121,6 +121,9 @@ function start() {
winston.verbose('* using themes stored in: %s', nconf.get('themes_path'));
}
+
+ var webserver = require('./src/webserver');
+
require('./src/database').init(function(err) {
if (err) {
winston.error(err.stack);
@@ -129,17 +132,24 @@ function start() {
var meta = require('./src/meta');
meta.configs.init(function () {
var templates = require('templates.js'),
- webserver = require('./src/webserver'),
sockets = require('./src/socket.io'),
plugins = require('./src/plugins'),
upgrade = require('./src/upgrade');
+ meta.themes.setupPaths();
+
templates.setGlobal('relative_path', nconf.get('relative_path'));
upgrade.check(function(schema_ok) {
if (schema_ok || nconf.get('check-schema') === false) {
+ webserver.init();
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'));
async.waterfall([
diff --git a/loader.js b/loader.js
index aaf9a03c19..98a569b531 100644
--- a/loader.js
+++ b/loader.js
@@ -14,8 +14,9 @@ var nconf = require('nconf'),
output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
silent = process.env.NODE_ENV !== 'development' ? true : false,
numProcs,
- handles = [],
+ handles = {},
handleIndex = 0,
+ server,
Loader = {
timesStarted: 0,
@@ -191,10 +192,9 @@ Loader.start = function(callback) {
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
connection._handle.readStop();
-
handles[handleIndex] = connection._handle;
var workers = clusterWorkers();
@@ -277,6 +277,8 @@ Loader.stop = function() {
// Clean up the pidfile
fs.unlinkSync(__dirname + '/pidfile');
+
+ server.close();
};
Loader.notifyWorkers = function (msg) {
diff --git a/src/meta/themes.js b/src/meta/themes.js
index dde10c3786..e3dcb67d01 100644
--- a/src/meta/themes.js
+++ b/src/meta/themes.js
@@ -2,6 +2,7 @@
'use strict';
var nconf = require('nconf'),
+ winston = require('winston'),
fs = require('fs'),
path = require('path'),
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) {
// Theme's 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_config', path.join(nconf.get('themes_path'), themeObj.id, 'theme.json'));
};
+
+
};
\ No newline at end of file
diff --git a/src/middleware/index.js b/src/middleware/index.js
index 77af0a13ac..487bda3592 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -19,22 +19,6 @@ var meta = require('../meta'),
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) {
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
if (fs.existsSync(faviconPath)) {
@@ -42,14 +26,11 @@ function setupFavicon(app) {
}
}
-module.exports = function(app, data) {
- var relativePath, themesPath;
+module.exports = function(app) {
+ var relativePath = nconf.get('relative_path');
middleware = require('./middleware')(app);
- relativePath = nconf.get('relative_path');
- themesPath = nconf.get('themes_path');
-
app.engine('tpl', templates.__express);
app.set('view engine', 'tpl');
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)
};
- if(meta.config.cookieDomain) {
+ if (meta.config.cookieDomain) {
cookie.domain = meta.config.cookieDomain;
}
@@ -98,7 +79,6 @@ module.exports = function(app, data) {
app.use(middleware.processRender);
auth.initialize(app, middleware);
- routeCurrentTheme(app, data.currentThemeId, data.themesData);
return middleware;
};
diff --git a/src/webserver.js b/src/webserver.js
index 5cbe8cd225..5eeae83225 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -1,3 +1,6 @@
+
+'use strict';
+
var path = require('path'),
fs = require('fs'),
nconf = require('nconf'),
@@ -9,11 +12,7 @@ var path = require('path'),
cluster = require('cluster'),
emailer = require('./emailer'),
- db = require('./database'),
- auth = require('./routes/authentication'),
meta = require('./meta'),
- user = require('./user'),
- notifications = require('./notifications'),
logger = require('./logger'),
plugins = require('./plugins'),
middleware = require('./middleware'),
@@ -32,73 +31,51 @@ if(nconf.get('ssl')) {
}
(function (app) {
- "use strict";
-
var port = nconf.get('PORT') || nconf.get('port');
- logger.init(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)
- ]);
- });
+ module.exports.init = function() {
+ emailer.registerApp(app);
- async.parallel({
- themesData: meta.themes.get,
- currentThemeId: function(next) {
- db.getObjectField('config', 'theme:id', next);
- }
- }, function(err, data) {
- middleware = middleware(app, data);
+ // 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)
+ ]);
+ });
+
+ middleware = middleware(app);
routes(app, middleware);
- if (err) {
- winston.error('Errors were encountered while attempting to initialise NodeBB.');
- process.exit();
- } else {
- if (process.env.NODE_ENV === 'development') {
- winston.info('Middlewares loaded.');
- }
+ // Cache static files on production
+ if (global.env !== 'development') {
+ app.enable('cache');
+ 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();
+ });
+ }
+ });
}
- });
- // Cache static files on production
- if (global.env !== 'development') {
- app.enable('cache');
- 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 && nconf.get('use_port') === false) {
+ winston.info('Enabling \'trust proxy\'');
+ app.enable('trust proxy');
+ }
- 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');
- }
+ 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');
+ }
+ };
server.on('error', function(err) {
winston.error(err.stack);
@@ -123,6 +100,8 @@ if(nconf.get('ssl')) {
});
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;
if (cluster.isWorker) {
port = 0;