Node.js based forum software built for the modern web
closed #2012
Julian Lam2bfa7d5b97d2
diff --git a/app.js b/app.jsindex 26fe077f87..bd9cb02e0a 100644--- a/app.js+++ b/app.js@@ -141,7 +141,13 @@ function start() {nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path'));plugins.ready(function() {- webserver.init();+ webserver.init(function() {+ // If this callback is called, this means that loader.js is used+ process.on('SIGCONT', webserver.listen);+ process.send({+ action: 'ready'+ });+ });});process.on('SIGTERM', shutdown);@@ -313,6 +319,8 @@ function shutdown(code) {winston.info('[app] Shutdown (SIGTERM/SIGINT) Initialised.');require('./src/database').close();winston.info('[app] Database connection closed.');+ require('./src/webserver').server.close();+ winston.info('[app] Web server closed to connections.');winston.info('[app] Shutdown complete.');process.exit(code || 0);diff --git a/loader.js b/loader.jsindex 6448615348..a3ce275704 100644--- a/loader.js+++ b/loader.js@@ -5,8 +5,7 @@ var nconf = require('nconf'),pidFilePath = __dirname + '/pidfile',output = fs.openSync(__dirname + '/logs/output.log', 'a'),start = function() {- var fork = require('child_process').fork,- nbb_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();@@ -18,14 +17,24 @@ var nconf = require('nconf'),}startTimer = setTimeout(resetTimer, 1000*10);- nbb = fork('./app', process.argv.slice(2), {+ if (nbb) {+ nbbOld = nbb;+ }++ nbb = require('child_process').fork('./app', process.argv.slice(2), {env: process.env});nbb.on('message', function(message) {if (message && typeof message === 'object' && message.action) {- if (message.action === 'restart') {- nbb_restart();+ switch (message.action) {+ case 'ready':+ if (!callback) return nbb.kill('SIGCONT');+ callback();+ break;+ case 'restart':+ nbb_restart();+ break;}}});@@ -52,10 +61,12 @@ var nconf = require('nconf'),}},nbb_restart = function() {- nbb.removeAllListeners('exit').on('exit', function() {- nbb_start();+ nbb_start(function() {+ nbbOld.removeAllListeners('exit').on('exit', function() {+ nbb.kill('SIGCONT');+ });+ nbbOld.kill();});- nbb.kill();},resetTimer = function() {clearTimeout(startTimer);@@ -70,7 +81,7 @@ var nconf = require('nconf'),nbb_start();},- nbb;+ nbb, nbbOld;nconf.argv();diff --git a/src/webserver.js b/src/webserver.jsindex 5c6f571372..3278713dd1 100644--- a/src/webserver.js+++ b/src/webserver.js@@ -97,7 +97,7 @@ if(nconf.get('ssl')) {}module.exports.server = server;- module.exports.init = function () {+ module.exports.init = function(callback) {server.on("error", function(err){if (err.code === 'EADDRINUSE') {winston.error('NodeBB address in use, exiting...');@@ -114,18 +114,26 @@ if(nconf.get('ssl')) {});emitter.on('templates:compiled', function() {- var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port;- winston.info('NodeBB attempting to listen on: ' + bind_address);-- server.listen(port, nconf.get('bind_address'), function(){- winston.info('NodeBB is now listening on: ' + bind_address);- if (process.send) {- process.send({- action: 'ready',- bind_address: bind_address- });- }- });+ if (process.send) {+ callback();+ } else {+ module.exports.listen();+ }+ });+ };++ module.exports.listen = function() {+ var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port;+ winston.info('NodeBB attempting to listen on: ' + bind_address);++ server.listen(port, nconf.get('bind_address'), function(){+ winston.info('NodeBB is now listening on: ' + bind_address);+ if (process.send) {+ process.send({+ action: 'listening',+ bind_address: bind_address+ });+ }});};