lib/server.js
function load(opts, cb) {
  if(typeof opts === 'function') {
    cb = opts;
    opts = null;
  }

  var args = opts && opts.args ? opts.args : null
    , defaults = path.normalize(path.join(__dirname, 'command'));
  opts = opts || argv(args);

  if(opts.commands === undefined) {
    opts.commands = [defaults];
  }else if(Array.isArray(opts.commands) && opts.defaults) {
    opts.commands.unshift(defaults);
  }

  function onConfigError(err) {
    //console.dir(err);
    console.error(err.message);
    if(process.env.NODE_ENV !== Constants.TEST) {
      process.exit(1);
    }
  }

  this.conf.once('error', onConfigError.bind(this));

  function onConfigLoad() {
    // if we loaded ok we are done with fatal config errors
    // config set/rewrite need to catch errors now
    this.conf.removeAllListeners('error');

    this.emit('config', this.conf);

    opts.conf = this.conf;

    // spawning the daemon process
    if(this.conf.get(ConfigKey.DAEMONIZE) && !this._daemonized) {
      var pid = daemon(
        path.join(__dirname, 'daemonize.js'), this.conf, opts.args);
      if(cb) cb(pid);
      return;
    }

    this.log.notice('cwd: %s', process.cwd());

    // pass configured timeout to script manager
    ScriptManager.timeout = this.conf.get(ConfigKey.LUA_TIME_LIMIT);

    // spawn now, makes first script executions faster
    // later this should be configurable - allow disabling all
    // scripting
    ScriptManager.spawn();

    // load commands
    return loader(opts, onCommandLoad);
  }

  function onCommandLoad(err, commands) {
    if(err) return cb(err);
    this.execs = commands;
    this.emit('ready', this.execs);

    // inject command list so the script manager
    // can detect write commands
    ScriptManager.commands = commands;

    // loading from rdb files breaks the tests
    if(process.env.NODE_ENV === Constants.TEST) {
      return cb();
    }
    Persistence.load(this.state.store, this.state.conf, onDatabaseLoad);
  }

  function onDatabaseLoad(err, time, diff, version) {
    // load errors are thrown, will cause
    // the process to exit
    if(err) throw err;
    if(err !== false) {
      this.log.notice('db loaded from disk: %s seconds (v%s)', time, version);
      this.emit('database', time, diff, version);
    }
    cb();
  }

  onConfigLoad = onConfigLoad.bind(this);
  onCommandLoad = onCommandLoad.bind(this);
  onDatabaseLoad = onDatabaseLoad.bind(this);

  // load config
  this.conf.load(opts, onConfigLoad);
}
function load(opts, cb) {
  if(typeof opts === 'function') {
    cb = opts;
    opts = null;
  }

  var args = opts && opts.args ? opts.args : null
    , defaults = path.normalize(path.join(__dirname, 'command'));
  opts = opts || argv(args);

  if(opts.commands === undefined) {
    opts.commands = [defaults];
  }else if(Array.isArray(opts.commands) && opts.defaults) {
    opts.commands.unshift(defaults);
  }

  function onConfigError(err) {
    //console.dir(err);
    console.error(err.message);
    if(process.env.NODE_ENV !== Constants.TEST) {
      process.exit(1);
    }
  }

  this.conf.once('error', onConfigError.bind(this));

  function onConfigLoad() {
    // if we loaded ok we are done with fatal config errors
    // config set/rewrite need to catch errors now
    this.conf.removeAllListeners('error');

    this.emit('config', this.conf);

    opts.conf = this.conf;

    // spawning the daemon process
    if(this.conf.get(ConfigKey.DAEMONIZE) && !this._daemonized) {
      var pid = daemon(
        path.join(__dirname, 'daemonize.js'), this.conf, opts.args);
      if(cb) cb(pid);
      return;
    }

    this.log.notice('cwd: %s', process.cwd());

    // pass configured timeout to script manager
    ScriptManager.timeout = this.conf.get(ConfigKey.LUA_TIME_LIMIT);

    // spawn now, makes first script executions faster
    // later this should be configurable - allow disabling all
    // scripting
    ScriptManager.spawn();

    // load commands
    return loader(opts, onCommandLoad);
  }

  function onCommandLoad(err, commands) {
    if(err) return cb(err);
    this.execs = commands;
    this.emit('ready', this.execs);

    // inject command list so the script manager
    // can detect write commands
    ScriptManager.commands = commands;

    // loading from rdb files breaks the tests
    if(process.env.NODE_ENV === Constants.TEST) {
      return cb();
    }
    Persistence.load(this.state.store, this.state.conf, onDatabaseLoad);
  }

  function onDatabaseLoad(err, time, diff, version) {
    // load errors are thrown, will cause
    // the process to exit
    if(err) throw err;
    if(err !== false) {
      this.log.notice('db loaded from disk: %s seconds (v%s)', time, version);
      this.emit('database', time, diff, version);
    }
    cb();
  }

  onConfigLoad = onConfigLoad.bind(this);
  onCommandLoad = onCommandLoad.bind(this);
  onDatabaseLoad = onDatabaseLoad.bind(this);

  // load config
  this.conf.load(opts, onConfigLoad);
}
function load ( opts , cb ) { if ( typeof opts === 'function' ) { cb = opts ; opts = null ; } var args = opts && opts . args ? opts . args : null , defaults = path . normalize ( path . join ( __dirname , 'command' ) ) ; opts = opts || argv ( args ) ; if ( opts . commands === undefined ) { opts . commands = [ defaults ] ; } else if ( Array . isArray ( opts . commands ) && opts . defaults ) { opts . commands . unshift ( defaults ) ; } function onConfigError ( err ) { //console.dir(err); console . error ( err . message ) ; if ( process . env . NODE_ENV !== Constants . TEST ) { process . exit ( 1 ) ; } } this . conf . once ( 'error' , onConfigError . bind ( this ) ) ; function onConfigLoad ( ) { // if we loaded ok we are done with fatal config errors // config set/rewrite need to catch errors now this . conf . removeAllListeners ( 'error' ) ; this . emit ( 'config' , this . conf ) ; opts . conf = this . conf ; // spawning the daemon process if ( this . conf . get ( ConfigKey . DAEMONIZE ) && ! this . _daemonized ) { var pid = daemon ( path . join ( __dirname , 'daemonize.js' ) , this . conf , opts . args ) ; if ( cb ) cb ( pid ) ; return ; } this . log . notice ( 'cwd: %s' , process . cwd ( ) ) ; // pass configured timeout to script manager ScriptManager . timeout = this . conf . get ( ConfigKey . LUA_TIME_LIMIT ) ; // spawn now, makes first script executions faster // later this should be configurable - allow disabling all // scripting ScriptManager . spawn ( ) ; // load commands return loader ( opts , onCommandLoad ) ; } function onCommandLoad ( err , commands ) { if ( err ) return cb ( err ) ; this . execs = commands ; this . emit ( 'ready' , this . execs ) ; // inject command list so the script manager // can detect write commands ScriptManager . commands = commands ; // loading from rdb files breaks the tests if ( process . env . NODE_ENV === Constants . TEST ) { return cb ( ) ; } Persistence . load ( this . state . store , this . state . conf , onDatabaseLoad ) ; } function onDatabaseLoad ( err , time , diff , version ) { // load errors are thrown, will cause // the process to exit if ( err ) throw err ; if ( err !== false ) { this . log . notice ( 'db loaded from disk: %s seconds (v%s)' , time , version ) ; this . emit ( 'database' , time , diff , version ) ; } cb ( ) ; } onConfigLoad = onConfigLoad . bind ( this ) ; onCommandLoad = onCommandLoad . bind ( this ) ; onDatabaseLoad = onDatabaseLoad . bind ( this ) ; // load config this . conf . load ( opts , onConfigLoad ) ; }
Loads command definitions.
Loads command definitions .