diff --git a/js/engine.js b/js/engine.js index 7af816b..982f8e8 100644 --- a/js/engine.js +++ b/js/engine.js @@ -240,8 +240,8 @@ function loadEventModules(args, answHandler) { function shutDown() { log.print('EN', 'Shutting down Poller and DB Link'); isRunning = false; - poller.send('cmd|shutdown'); - db.shutDown(); + if(poller) poller.send('cmd|shutdown'); + if(db) db.shutDown(); } exports.init = init; diff --git a/js/logging.js b/js/logging.js index 0f14656..b7b959b 100644 --- a/js/logging.js +++ b/js/logging.js @@ -28,10 +28,13 @@ exports.error = function(module, err) { var e = new Error(); if(module) console.error(ts + module + ' | ERROR AND BAD HANDLING: ' + err + '\n' + e.stack); else console.error(ts + '!N/A! | ERROR, BAD HANDLING AND NO MODULE NAME: ' + err + '\n' + e.stack); - } else { + } else if(err) { if(err.addInfo) ai = ' (' + err.addInfo + ')'; if(!err.message) err.message = 'UNKNOWN REASON!\n' + err.stack; if(module) console.error(ts + module + ' | ERROR'+ai+': ' + err.message); else console.error(ts + '!N/A! | ERROR AND NO MODULE NAME'+ai+': ' + err.message + '\n' + err.stack); + } else { + var e = new Error('Unexpected error'); + console.error(e.message + ': \n' + e.stack); } }; diff --git a/js/server.js b/js/server.js index b28b780..0dc677e 100644 --- a/js/server.js +++ b/js/server.js @@ -22,6 +22,7 @@ dog's back. > > ## This is an H2 in a blockquote */ + var http_listener = require('./http_listener'), db = require('./db_interface'), engine = require('./engine'), @@ -29,6 +30,9 @@ var http_listener = require('./http_listener'), log = require('./logging'), fs = require('fs'), path = require('path'), + procCmds = { + 'die': function() { shutDown(); } + }, objCmds = { 'loadrules': mm.loadRulesFile, 'loadaction': mm.loadActionModule, @@ -53,10 +57,14 @@ function handleAdminCommands(args, answHandler) { } function shutDown(args, answHandler) { - answHandler.answerSuccess('Goodbye!'); + if(answHandler) answHandler.answerSuccess('Goodbye!'); log.print('RS', 'Received shut down command!'); - engine.shutDown(); - http_listener.shutDown(); + if(engine && typeof engine.shutDown === 'function') engine.shutDown(); + else { + console.error(typeof engine.shutDown); + console.error('no function!'); + } + if(http_listener) http_listener.shutDown(); } fs.readFile(path.resolve(__dirname, '..', 'config', 'config.json'), 'utf8', function (err, data) { @@ -78,4 +86,9 @@ fs.readFile(path.resolve(__dirname, '..', 'config', 'config.json'), 'utf8', func log.print('RS', 'Initialzing module manager'); mm.init(db, engine.loadActionModule, engine.loadRule); } -}); \ No newline at end of file +}); + +process.on('message', function(cmd) { + if(typeof procCmds[cmd] === 'function') procCmds[cmd](); + else console.error('err with command'); +}); diff --git a/run_server.sh b/run_server.sh index 57b37df..fe86b0c 100755 --- a/run_server.sh +++ b/run_server.sh @@ -1,3 +1,3 @@ #!/bin/bash DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -node $DIR/js/webapi_eca_server > $DIR/server.log 2>&1 & +node $DIR/js/server > $DIR/server.log 2>&1 & diff --git a/testing/mod_server.js b/testing/mod_server.js index 3a5ce50..43bbb07 100644 --- a/testing/mod_server.js +++ b/testing/mod_server.js @@ -1,12 +1,27 @@ -exports.setUp = function() { - // this.srv = require('../js/webapi_eca_server.js'); +var path = require('path'); +//FIXME handle EADDR in use! +exports.setUp = function(cb) { + this.srv = require('child_process').fork(path.resolve(__dirname, '..', 'js', 'server')); + cb(); }; exports.testUnit_SRV = function(test){ - console.log(this.srv); - test.ok(true, "SRV"); + setTimeout( + function() { + + test.ok(true, "SRV"); test.done(); + } + , 2000); }; +exports.tearDown = function(cb) { + console.log('this.srv: '); + // console.log(global.srv); + this.srv.send('die'); + this.srv = null; + cb(); +}; + // var http_listener = require('./http_listener'), // db = require('./db_interface'), // engine = require('./engine'),