@ -5,9 +5,16 @@ var nconf = require('nconf'),
pidFilePath = _ _dirname + '/pidfile' ,
start = function ( ) {
var fork = require ( 'child_process' ) . fork ,
// output = fs.openSync(__dirname + '/logs/output.log', 'a'),
output = fs . createWriteStream ( _ _dirname + '/logs/output.log' , {
flags : 'a' ,
encoding : 'utf-8'
} ) ,
nbb _start = function ( ) {
var silent = nconf . get ( 'daemon' ) !== false ;
if ( timesStarted > 3 ) {
console . log ( '\n[loader] Experienced three start attempts in 10 seconds, most likely am error on startup. Halting.' ) ;
console . log ( '\n[loader] Experienced three start attempts in 10 seconds, most likely a n error on startup. Halting.') ;
return nbb _stop ( ) ;
}
@ -20,13 +27,29 @@ var nconf = require('nconf'),
nbb = fork ( './app' , process . argv . slice ( 2 ) , {
env : {
'NODE_ENV' : process . env . NODE _ENV
}
} ,
silent : silent
} ) ;
if ( silent ) {
nbb . stdout . pipe ( output ) ;
nbb . stderr . pipe ( output ) ;
}
nbb . on ( 'message' , function ( message ) {
if ( message && typeof message === 'object' && message . action ) {
if ( message . action === 'restart' ) {
nbb _restart ( ) ;
} else if ( message . action === 'ready' && silent ) {
// Output bind_address
process . stdout . write ( '\nNodeBB listening on ' + message . bind _address + '\n\n' ) ;
// Daemonize and record new pid
require ( 'daemon' ) ( {
stdout : output
} ) ;
fs . writeFile ( _ _dirname + '/pidfile' , process . pid ) ;
}
}
} ) ;
@ -89,19 +112,7 @@ if (nconf.get('daemon') !== false) {
}
}
// Initialise logging streams
var outputStream = fs . createWriteStream ( _ _dirname + '/logs/output.log' ) ;
outputStream . on ( 'open' , function ( fd ) {
// Daemonize
require ( 'daemon' ) ( {
stdout : fd
} ) ;
// Write its pid to a pidfile
fs . writeFile ( _ _dirname + '/pidfile' , process . pid ) ;
start ( ) ;
} ) ;
} else {
start ( ) ;
}