diff --git a/coffee/http-listener.coffee b/coffee/http-listener.coffee index 3b55351..ca08743 100644 --- a/coffee/http-listener.coffee +++ b/coffee/http-listener.coffee @@ -80,9 +80,20 @@ initRouting = ( port ) => # - **`POST` to _"/user"_:** User requests are possible for all users with an account app.post '/usercommand', requestHandler.handleUserCommand try - @server = app.listen port # inbound event channel - catch e - @log.error e, 'HL | Unable to listen...' + @server = app.listen parseInt( port ) || 8125 # inbound event channel + ### + Error handling of the express port listener requires special attention, + thus we have to catch the error, which is issued if the port is already in use. + ### + @server.on 'listening', () => + addr = @server.address() + if addr.port is not port + @shutDownSystem() + @server.on 'error', ( err ) => + if err.errno is 'EADDRINUSE' + @log.error err, 'HL | http-port already in use, shutting down!' + @shutDownSystem() + ### Adds the shutdown handler to the admin commands. @@ -90,7 +101,8 @@ Adds the shutdown handler to the admin commands. @param {function} fshutDown @public addShutdownHandler( *fShutDown* ) ### -exports.addShutdownHandler = ( fShutDown ) -> +exports.addShutdownHandler = ( fShutDown ) => + @shutDownSystem = fShutDown requestHandler.addShutdownHandler fShutDown ### @@ -100,9 +112,6 @@ Shuts down the http listener. ### exports.shutDown = () => @log.warn 'HL | Shutting down HTTP listener' - console.log 'apppp' - console.log app - @server.close() - #TODO This is a bit brute force... - #process.exit() + try + @server.close() diff --git a/coffee/webapi-eca.coffee b/coffee/webapi-eca.coffee index 37cdb76..dc3dbfb 100644 --- a/coffee/webapi-eca.coffee +++ b/coffee/webapi-eca.coffee @@ -88,18 +88,6 @@ if argv.help process.exit() ### -Error handling of the express port listener requires special attention, -thus we have to catch the process error, which is issued if -the port is already in use. -### -process.on 'uncaughtException', ( err ) => - switch err.errno - when 'EADDRINUSE' - @log.error err, 'RS | http-port already in use, shutting down!' - shutDown() - # else @log.error 'RS', err - else throw err -### This function is invoked right after the module is loaded and starts the server. @private init() @@ -146,6 +134,8 @@ init = => @log.info 'RS | Initialzing engine' engine args @log.info 'RS | Initialzing http listener' + # We give the HTTP listener the ability to shutdown the whole system + http.addShutdownHandler shutDown http args # > Distribute handlers between modules to link the application. @@ -153,7 +143,6 @@ init = => engine.addPersistence db @log.info 'RS | Passing handlers to http listener' #TODO engine pushEvent needs to go into redis queue - http.addShutdownHandler shutDown #TODO loadAction and addRule will be removed #mm.addHandlers db, engine.loadActionModule, engine.addRule @log.info 'RS | Forking child process for the event poller' diff --git a/js-coffee/http-listener.js b/js-coffee/http-listener.js index 590a0fe..570623c 100644 --- a/js-coffee/http-listener.js +++ b/js-coffee/http-listener.js @@ -48,7 +48,7 @@ HTTP Listener initRouting = function(port) { - var e, sess_sec; + var sess_sec; app.use(express.cookieParser()); sess_sec = "149u*y8C:@kmN/520Gt\\v'+KFBnQ!\\r<>5X/xRI`sT # Garbage collect eventually still running process fWaitForDeath = () -> if isRunning - test.ok false, 'Engine didn\'t shut down!' + test.ok false, '"testShutDown" Engine didn\'t shut down!' engine.kill() test.done() @@ -50,7 +50,7 @@ exports.testKill = ( test ) => # Garbage collect eventually still running process fWaitForDeath = () -> - test.ok engine.killed, 'Engine didn\'t shut down!' + test.ok engine.killed, '"testKill" Engine didn\'t shut down!' test.done() setTimeout fWaitForStartup, 1000 @@ -69,12 +69,12 @@ exports.testHttpPortAlreadyUsed = ( test ) => isRunning = false test.done() - setTimeout fWaitForDeath, 3000 + setTimeout fWaitForDeath, 1000 # Garbage collect eventually still running process fWaitForDeath = () => if isRunning - test.ok false, 'Engine didn\'t shut down!' + test.ok false, '"testHttpPortAlreadyUsed" Engine didn\'t shut down!' test.done() @engine_one.kill() @@ -87,7 +87,7 @@ exports.testHttpPortInvalid = ( test ) => isRunning = true pth = path.resolve 'js-coffee', 'webapi-eca' - engine = cp.fork pth, ['-n', '-w', '-1'] # [ '-i' , 'warn' ] + engine = cp.fork pth, ['-n', '-w', '0'] # [ '-i' , 'warn' ] engine.on 'exit', ( code, signal ) -> test.ok true, 'Engine stopped' isRunning = false @@ -95,10 +95,10 @@ exports.testHttpPortInvalid = ( test ) => # Garbage collect eventually still running process fWaitForDeath = () => + engine.kill() if isRunning - test.ok false, 'Engine didn\'t shut down!' + test.ok false, '"testHttpPortInvalid" Engine didn\'t shut down!' test.done() - engine.kill() setTimeout fWaitForDeath, 1000