2013-11-19 13:53:36 +00:00
// Generated by CoffeeScript 1.6.3
/ *
2013-11-26 22:24:15 +00:00
2014-02-18 21:34:36 +00:00
WebAPI - ECA Engine
=== === === === === ==
2013-11-26 22:24:15 +00:00
2014-02-18 21:34:36 +00:00
> This is the main module that is used to run the whole application :
2013-11-20 14:41:41 +00:00
>
2014-02-18 21:34:36 +00:00
> node webapi - eca [ opt ]
2013-11-20 14:41:41 +00:00
>
2014-02-18 21:34:36 +00:00
> See below in the optimist CLI preparation for allowed optional parameters ` [opt] ` .
2013-11-19 13:53:36 +00:00
* /
( function ( ) {
2014-02-19 13:14:08 +00:00
var argv , conf , cp , db , engine , fs , http , init , logger , opt , optimist , path , procCmds , shutDown , usage ,
_this = this ;
2013-11-19 13:53:36 +00:00
2014-02-18 21:34:36 +00:00
logger = require ( './logging' ) ;
2013-11-19 17:13:38 +00:00
conf = require ( './config' ) ;
2014-02-13 17:16:03 +00:00
db = require ( './persistence' ) ;
2013-11-19 17:13:38 +00:00
engine = require ( './engine' ) ;
2014-02-18 08:48:18 +00:00
http = require ( './http-listener' ) ;
2013-11-19 17:13:38 +00:00
2014-02-17 22:27:26 +00:00
fs = require ( 'fs' ) ;
path = require ( 'path' ) ;
cp = require ( 'child_process' ) ;
optimist = require ( 'optimist' ) ;
2013-11-19 17:13:38 +00:00
procCmds = { } ;
2014-02-17 22:27:26 +00:00
/ *
2014-02-18 21:34:36 +00:00
Let ' s prepare the optimist CLI optional arguments ` [opt] ` :
2014-02-17 22:27:26 +00:00
* /
usage = 'This runs your webapi-based ECA engine' ;
opt = {
'h' : {
alias : 'help' ,
describe : 'Display this'
} ,
'c' : {
alias : 'config-path' ,
describe : 'Specify a path to a custom configuration file, other than "config/config.json"'
} ,
'w' : {
alias : 'http-port' ,
describe : 'Specify a HTTP port for the web server'
} ,
'd' : {
alias : 'db-port' ,
describe : 'Specify a port for the redis DB'
} ,
'm' : {
alias : 'log-mode' ,
describe : 'Specify a log mode: [development|productive]'
} ,
'i' : {
alias : 'log-io-level' ,
describe : 'Specify the log level for the I/O'
} ,
'f' : {
alias : 'log-file-level' ,
describe : 'Specify the log level for the log file'
} ,
'p' : {
alias : 'log-file-path' ,
describe : 'Specify the path to the log file within the "logs" folder'
} ,
'n' : {
alias : 'nolog' ,
describe : 'Set this if no output shall be generated'
2014-02-20 09:17:06 +00:00
} ,
'u' : {
alias : 'unit-test-flag' ,
describe : "Set this if you are running the unit tests. This will cause the\nsystem to not call process.exit() at the end of the shutDown routine\nin order to get rid of the express server that would keep running"
2014-02-17 22:27:26 +00:00
}
} ;
argv = optimist . usage ( usage ) . options ( opt ) . argv ;
if ( argv . help ) {
console . log ( optimist . help ( ) ) ;
process . exit ( ) ;
}
2013-11-19 17:13:38 +00:00
/ *
This function is invoked right after the module is loaded and starts the server .
2013-11-20 23:20:06 +00:00
@ private init ( )
2013-11-19 17:13:38 +00:00
* /
init = function ( ) {
2014-02-19 13:14:08 +00:00
var args , logconf ;
2014-02-17 22:27:26 +00:00
conf ( argv . c ) ;
2013-11-19 17:13:38 +00:00
if ( ! conf . isReady ( ) ) {
2014-02-17 22:27:26 +00:00
console . error ( 'FAIL: Config file not ready! Shutting down...' ) ;
2013-11-20 14:41:41 +00:00
process . exit ( ) ;
2013-11-19 17:13:38 +00:00
}
2014-02-20 09:17:06 +00:00
_this . isUnitTest = argv . u || false ;
2014-02-17 22:27:26 +00:00
logconf = conf . getLogConf ( ) ;
if ( argv . m ) {
logconf [ 'mode' ] = argv . m ;
}
if ( argv . i ) {
logconf [ 'io-level' ] = argv . i ;
}
if ( argv . f ) {
logconf [ 'file-level' ] = argv . f ;
}
if ( argv . p ) {
logconf [ 'file-path' ] = argv . p ;
2013-11-19 17:13:38 +00:00
}
2014-02-17 22:27:26 +00:00
if ( argv . n ) {
logconf [ 'nolog' ] = argv . n ;
2013-11-19 17:13:38 +00:00
}
2014-02-17 22:27:26 +00:00
try {
fs . unlinkSync ( path . resolve ( _ _dirname , '..' , 'logs' , logconf [ 'file-path' ] ) ) ;
} catch ( _error ) { }
2014-02-19 13:14:08 +00:00
_this . log = logger . getLogger ( logconf ) ;
_this . log . info ( 'RS | STARTING SERVER' ) ;
2014-02-17 22:27:26 +00:00
args = {
2014-02-19 13:14:08 +00:00
logger : _this . log ,
2014-02-17 22:27:26 +00:00
logconf : logconf
} ;
args [ 'http-port' ] = parseInt ( argv . w || conf . getHttpPort ( ) ) ;
2014-02-19 16:04:49 +00:00
args [ 'db-port' ] = parseInt ( argv . d || conf . getDbPort ( ) ) ;
2014-02-19 13:14:08 +00:00
_this . log . info ( 'RS | Initialzing DB' ) ;
2013-11-19 17:13:38 +00:00
db ( args ) ;
2014-02-19 16:04:49 +00:00
return db . isConnected ( function ( err ) {
2014-02-17 22:27:26 +00:00
var cliArgs , poller ;
2014-02-19 13:14:08 +00:00
if ( err ) {
2014-02-19 16:04:49 +00:00
_this . log . error ( 'RS | No DB connection, shutting down system!' ) ;
2014-02-19 13:14:08 +00:00
return shutDown ( ) ;
} else {
_this . log . info ( 'RS | Initialzing engine' ) ;
2013-11-20 14:41:41 +00:00
engine ( args ) ;
2014-02-19 13:14:08 +00:00
_this . log . info ( 'RS | Initialzing http listener' ) ;
2014-02-19 14:09:12 +00:00
http . addShutdownHandler ( shutDown ) ;
2014-02-18 08:48:18 +00:00
http ( args ) ;
2014-02-19 13:14:08 +00:00
_this . log . info ( 'RS | Passing handlers to engine' ) ;
2014-02-13 17:16:03 +00:00
engine . addPersistence ( db ) ;
2014-02-19 13:14:08 +00:00
_this . log . info ( 'RS | Passing handlers to http listener' ) ;
_this . log . info ( 'RS | Forking child process for the event poller' ) ;
2014-02-17 22:27:26 +00:00
cliArgs = [ args . logconf [ 'mode' ] , args . logconf [ 'io-level' ] , args . logconf [ 'file-level' ] , args . logconf [ 'file-path' ] , args . logconf [ 'nolog' ] ] ;
2014-02-18 08:48:18 +00:00
return poller = cp . fork ( path . resolve ( _ _dirname , 'event-poller' ) , cliArgs ) ;
2013-11-19 17:13:38 +00:00
}
} ) ;
} ;
/ *
Shuts down the server .
2013-11-20 23:20:06 +00:00
2013-11-26 22:24:15 +00:00
@ private shutDown ( )
2013-11-19 17:13:38 +00:00
* /
2013-11-26 22:24:15 +00:00
shutDown = function ( ) {
2014-02-19 13:14:08 +00:00
_this . log . warn ( 'RS | Received shut down command!' ) ;
2013-11-19 17:13:38 +00:00
if ( engine != null ) {
engine . shutDown ( ) ;
}
2014-02-19 13:14:08 +00:00
return process . exit ( ) ;
2013-11-19 17:13:38 +00:00
} ;
/ *
# # Process Commands
When the server is run as a child process , this function handles messages
from the parent process ( e . g . the testing suite )
* /
process . on ( 'message' , function ( cmd ) {
return typeof procCmds [ cmd ] === "function" ? procCmds [ cmd ] ( ) : void 0 ;
} ) ;
2014-02-19 13:14:08 +00:00
process . on ( 'SIGINT' , shutDown ) ;
process . on ( 'SIGTERM' , shutDown ) ;
2013-11-19 17:13:38 +00:00
procCmds . die = shutDown ;
init ( ) ;
2013-11-19 13:53:36 +00:00
} ) . call ( this ) ;