Skip to content

Commit

Permalink
chore(cleanup): Cleaned up index, app and added chalk
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsy committed Jun 24, 2014
1 parent 1868b4b commit 919b124
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
58 changes: 28 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
var cluster = require( 'cluster' )
, config = require( './config' )
, packageJson = require( './package.json' )
, path = require( 'path' )
, os = require( 'os' )
, isWin = /^win32/.test( os.platform() )
, fs = require( 'fs' );

function loadModulesAppFiles( moduleName ) {
packageJson.bundledDependencies.forEach(function( moduleName ) {
var file = [ path.resolve( './' ), 'modules', moduleName, 'bin', 'app.js' ].join( path.sep );
if ( fs.existsSync( file ) ) {
require( file )( cluster, config, packageJson );
}
});
}
var cluster = require( 'cluster' )
, debug = require( 'debug' )( cluster.isMaster ? 'Cluster' : 'Worker' )
, config = require( './config' )
, os = require( 'os' )
, numWorkers = config.numChildren ? config.numChildren : os.cpus()
, helpers = require( './lib/utils/helpers.js' )
, chalk = require( 'chalk' );

// Cleanup the NODE_PATH for module loading
process.env.NODE_PATH = helpers.nodePath();

// Set the node path - this works only because the other processes are forked. (Be sure to use the right delimiter)
process.env.NODE_PATH = process.env.NODE_PATH
? [ './lib/', './modules', process.env.NODE_PATH ].join( isWin ? ';' : ':' )
: [ './lib/', './modules' ].join( isWin ? ';' : ':' );
// Allow modules to hook into this file
helpers.loadModulesFileByName( 'app.js', config, cluster, debug );

// Master process
if ( cluster.isMaster ) {
cluster.on('exit', function( worker, code, signal ) {
console.dir( arguments );
cluster.fork();

debug( 'started with pid %s', chalk.yellow( process.pid ) );

// Output the current NODE_PATH for debugging
debug( 'NODE_PATH is "%s"', process.env.NODE_PATH );

// Make sure we manage child processes exiting
cluster.on( 'exit', function( worker, code, signal ) {
debug( 'Worker %s has died with code %s and signal %s - Forking new process in 1 second...', worker.pid, code, signal );
setTimeout( cluster.fork.bind( cluster ), 1000 );
});

for ( var i=0; i<config.numChildren; ++i ) {
// Create worker processes
for ( var i = 0; i < numWorkers; ++i ) {
cluster.fork();
}

// moduleName/bin/app.js hook
loadModulesAppFiles();
} else {

// moduleName/bin/app.js hook
loadModulesAppFiles();
// Load a single application worker instance
require( './index.js' );

require('./index.js');
}
}
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ var utils = require( 'utils' )
, env = utils.bootstrapEnv()
, moduleLdr = env.moduleLoader
, cors = require( 'cors' )
, chalk = require( 'chalk' )
, debug = require( 'debug' )( 'Worker' )
, express = env.express
, app = module.exports = env.app;

debug( 'started with pid %s', chalk.yellow( process.pid ) );

moduleLdr.on( 'preLoadModules', function() {
debug( 'Configuring express application...' );

app.configure(function() {
app.use( express.urlencoded() );
app.use( express.json() );
Expand All @@ -18,16 +24,21 @@ moduleLdr.on( 'preLoadModules', function() {
});

moduleLdr.on( 'modulesLoaded', function() {
debug( 'Initializing routes...' );

moduleLdr.initializeRoutes();
});

moduleLdr.on( 'routesInitialized', function() {
debug( 'Setting up router and starting http server...' );

app.configure(function() {
app.use( app.router );
app.listen( env.webPort, function() {
console.log( "Starting server on port " + env.webPort + " in " + env.config.environmentName + " mode" );
debug( 'Started web server on port %s in enviromment %s', chalk.yellow( env.webPort ), chalk.yellow( process.env.NODE_ENV ? process.env.NODE_ENV : "LOCAL" ) );
});
});
});

env.moduleLoader.loadModules();
debug( 'Loading modules...' );
moduleLdr.loadModules();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"async": "~0.2.9",
"bluebird": "^2.0.2",
"chalk": "^0.4.0",
"clever-controller": "~1.1.4",
"clever-injector": "~1.0.0",
"cors": "~2.1.1",
Expand Down Expand Up @@ -57,4 +58,4 @@
"supertest": ""
},
"bundledDependencies": []
}
}

0 comments on commit 919b124

Please sign in to comment.