1
0
Fork 0

[BC BREAK] bin/server.js and associated changes

This is based (very) loosely on an internal script to start the
daemon.  It accepts a configuration and starts the daemon.

To accommodate the configuration, a number of miscellaneous changes
have been made.

The vanilla configuration shows the concept, but it has not yet been
fully implemented; that'll likely happen at a later date.  Until then,
the existing environment-variable-based configuration will be used.

* bin/server.js: Add file.
* conf/vanilla-server.json: Example configuration added.
* src/server/daemon/Daemon.js (_httpPort): Remove field.
  (_conf): Add field.
  (__construct): [BC BREAK] Accept conf instead of port and log
    priority.  Move initialization code into `start'.
  (start): [BC BREAK] Initialization code moved here.  Now returns
    promise for entire daemon, which will error in the event of an
    error starting.  Move existing code into `_startDaemon'.
  (_startDaemon): Old `start' code.  Invoked after `start'
    initialization.
  (_createDebugLog, _createAccessLog): Use configuration.  Return
    promise.
  (_initHttpServer): Use configuration.
  (_httpError): Add function to output error and exit.  Extracted from
    `_initHttpServer'.
* src/server/daemon/scripts.js: [BC BREAK] Append "program/" to
    `LV_LEGACY_PATH' so that it can be re-used for script lookups
    rather than using the cwd.  This removes the need of the cwd being
    the legacy src path.
master
Mike Gerwitz 2017-08-28 12:37:46 -04:00
parent 464a46abf0
commit 985819c31b
4 changed files with 200 additions and 50 deletions

66
bin/server.js 100644
View File

@ -0,0 +1,66 @@
/**
* Start the Liza Server
*
* Copyright (C) 2017 R-T Specialty, LLC.
*
* This file is part of the Liza Data Collection Framework.
*
* liza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const fs = require( 'fs' );
const {
conf: {
ConfLoader,
ConfStore,
},
server,
version,
} = require( '../' );
// kluge for now
const conf_path = (
( process.argv[ 2 ] === '-c' )
? process.argv[ 3 ]
: ''
) || __dirname + '/../conf/vanilla-server.json';
ConfLoader( fs, ConfStore )
.fromFile( conf_path )
.then( conf => Promise.all( [
conf.get( 'name' ),
conf.get( 'daemon' ),
Promise.resolve( conf ),
] ) )
.then( ([ name, daemon, conf ]) =>
{
greet( name, daemon );
return server.daemon[ daemon ]( conf ).start();
} )
.catch( e => {
console.error( e.stack );
process.exit( 1 );
} );
function greet( name, daemon )
{
console.log( `${name} (liza-${version})`);
console.log( `Server configuration: ${conf_path}` );
console.log( `Starting with ${daemon}, pid ${process.pid}` );
}

View File

@ -0,0 +1,55 @@
{
"name": "Liza Server",
"daemon": "DevDaemon",
"http": {
"port": 8822
},
"log": {
"priority": 10,
"access": {
"path": "/var/log/node/access.log"
},
"debug": {
"path": "/var/log/node/debug.log"
}
},
"user": {
"session": {
"handler": {
"type": "php",
"cookie": "PHPSESSID"
},
"store": {
"type": "memcache",
"host": "localhost",
"port": 11211
}
}
},
"documentStore": {
"store": "mongodb",
"host": "localhost",
"port": 27017
},
"services": {
"rating": {
"process": {
"port": 5859,
"argv": "inherit"
},
"remote": {
"host": "localhost",
"domain": ""
}
},
"c1export": {
"host": "localhost",
"domain": ""
}
}
}

View File

@ -34,10 +34,10 @@ var AbstractClass = require( 'easejs' ).AbstractClass,
module.exports = AbstractClass( 'Daemon', module.exports = AbstractClass( 'Daemon',
{ {
/** /**
* Quote server port * System configuration
* @type {number} * @type {Store}
*/ */
'private _httpPort': 0, 'private _conf': null,
/** /**
* Server to accept HTTP requests * Server to accept HTTP requests
@ -96,17 +96,9 @@ module.exports = AbstractClass( 'Daemon',
'private _rater': null, 'private _rater': null,
'public __construct': function( http_port, log_priority ) 'public __construct': function( conf )
{ {
this._httpPort = http_port; this._conf = conf;
this._rater = liza.server.rater.ProcessManager();
this._httpServer = this.getHttpServer();
this._accessLog = this._createAccessLog();
this._debugLog = this._createDebugLog( log_priority );
this._encService = this.getEncryptionService();
this._memcache = this.getMemcacheClient();
this._routers = this.getRouters();
}, },
@ -115,10 +107,28 @@ module.exports = AbstractClass( 'Daemon',
* *
* @return {undefined} * @return {undefined}
*/ */
'public start': function() 'public start'()
{ {
var _self = this; return Promise.all( [
this._createDebugLog(),
this._createAccessLog(),
] ).then( ([ debug_log, access_log ]) =>
{
this._debugLog = debug_log;
this._accessLog = access_log;
this._httpServer = this.getHttpServer();
this._rater = liza.server.rater.ProcessManager();
this._encService = this.getEncryptionService();
this._memcache = this.getMemcacheClient();
this._routers = this.getRouters();
} )
.then( () => this._startDaemon() );
},
'private _startDaemon'()
{
this._debugLog.log( this._debugLog.PRIORITY_IMPORTANT, this._debugLog.log( this._debugLog.PRIORITY_IMPORTANT,
"Access log path: %s", this._accessLogPath "Access log path: %s", this._accessLogPath
); );
@ -128,18 +138,18 @@ module.exports = AbstractClass( 'Daemon',
); );
this._initSignalHandlers(); this._initSignalHandlers();
this._testEncryptionService( function() this._testEncryptionService( () =>
{ {
_self._memcacheConnect(); this._memcacheConnect();
_self._initMemoryLogger(); this._initMemoryLogger();
_self._initRouters(); this._initRouters();
_self._initHttpServer( function() this._initHttpServer( () =>
{ {
_self._initUncaughtExceptionHandler(); this._initUncaughtExceptionHandler();
// ready to roll // ready to roll
_self._debugLog.log( _self._debugLog.PRIORITY_INFO, this._debugLog.log( this._debugLog.PRIORITY_INFO,
"Daemon initialization complete." "Daemon initialization complete."
); );
} ); } );
@ -299,22 +309,30 @@ module.exports = AbstractClass( 'Daemon',
'private _createAccessLog': function() 'private _createAccessLog': function()
{ {
this._accessLogPath = return this._conf.get( 'log.access.path' )
( process.env.LOG_PATH_ACCESS || '/var/log/node/access.log' ); .then( log_path =>
{
return this.getAccessLog()( this._accessLogPath ); this._accessLogPath = log_path;
return this.getAccessLog()( this._accessLogPath );
} );
}, },
'private _createDebugLog': function( log_priority ) 'private _createDebugLog': function()
{ {
this._debugLogPath = return Promise.all( [
( process.env.LOG_PATH_DEBUG || '/var/log/node/debug.log' ); this._conf.get( 'log.priority' ),
this._conf.get( 'log.debug.path' ),
] )
.then( ([ priority, debug_log_path ]) =>
{
this._debugLogPath = debug_log_path;
return this.getPriorityLog()( return this.getPriorityLog()(
this._debugLogPath, debug_log_path,
( process.env.LOG_PRIORITY || log_priority ) priority
); )
} );
}, },
@ -514,25 +532,33 @@ module.exports = AbstractClass( 'Daemon',
this._debugLog this._debugLog
); );
this._httpServer.listen( this._httpPort, function() this._conf.get( 'http.port' )
{ .then( port => this._httpServer.listen( port, () =>
_self._debugLog.log( {
1, "Server running on port %d", _self._httpPort this._debugLog.log(
); 1, "Server running on port %d", _self._httpPort
);
callback(); callback();
} ); } ) )
.catch( e => this._httpError( e ) );
} }
catch( err ) catch( e )
{ {
this._debugLog.log( this._debugLog.PRIORITY_ERROR, this._httpError( e );
"Unable to start HTTP server: %s",
err
);
// exit with an error
process.exit( 1 );
} }
}, },
'private _httpError'( e )
{
this._debugLog.log( this._debugLog.PRIORITY_ERROR,
"Unable to start HTTP server: %s",
err
);
// TODO: use daemon-level promise and reject it
process.exit( 1 );
},
} ); } );

View File

@ -51,10 +51,12 @@ var script_paths = [
( process.env.LV_ROOT_PATH || '.' ) + '/src/www/scripts/program/', ( process.env.LV_ROOT_PATH || '.' ) + '/src/www/scripts/program/',
]; ];
const legacy_path = process.env.LV_LEGACY_PATH + '/';
var script_prefix = { var script_prefix = {
liza: __dirname + '/../../', liza: __dirname + '/../../',
assert: __dirname + '/../../assert/', assert: __dirname + '/../../assert/',
program: ( process.env.LV_LEGACY_PATH + '/' ) || '', program: ( legacy_path + 'program/' ) || '',
}; };
/** /**
@ -103,7 +105,7 @@ exports.route = function( request, log )
suffix = parts[ 2 ]; suffix = parts[ 2 ];
var chk_paths = script_paths.slice(); var chk_paths = script_paths.slice();
chk_paths.unshift( script_prefix[ prefix ] || './' ); chk_paths.unshift( script_prefix[ prefix ] || legacy_path );
// check each of the paths for the script that was requested // check each of the paths for the script that was requested
( function check_path( paths ) ( function check_path( paths )
@ -119,6 +121,7 @@ exports.route = function( request, log )
// check to see if the file exists within the path // check to see if the file exists within the path
var filename = ( cur_path + suffix ); var filename = ( cur_path + suffix );
fs.exists( filename, function( exists ) fs.exists( filename, function( exists )
{ {
if ( !exists ) if ( !exists )