index.js
function(options){
  var _this = this;

  this.handlers = {};

  if(!options){
    options = {};
  }

  if(!options.redis){
    options.redis = {};
  }

  this.options = {
    id: options.id || shortId.generate(),
    autoConfig: options.autoConfig ||  false,
    dataExpiry: options.dataExpiry || 30,
    redis: {
      port: options.redis.port || 6379,
      host: options.redis.host || '127.0.0.1',
      redis: options.redis.options || {}
    }
  };

  //Create redis clients
  this.redisSub = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);
  this.redis = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);
  this.redisInstant = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);

  //Attempt auto config
  if(this.options.autoConfig){
    var config = '';
    this.redis.config("GET", "notify-keyspace-events", function(err, data){
      if(data){
        config = data[1];
      }
      if(config.indexOf('E') == -1){
        config += 'E'
      }
      if(config.indexOf('x') == -1){
        config += 'x'
      }
      _this.redis.config("SET", "notify-keyspace-events", config)
    });
  }

  //Listen to key expiry notifications and handle events
  var expiryListener = new RedisEvent(this.redisSub, 'expired', /redular:(.+):(.+):(.+)/);
  expiryListener.defineHandler(function(key){
    var clientId = key[1];
    var eventName = key[2];
    var eventId = key[3];

    _this.redis.get('redular-data:' + clientId + ':' + eventName + ':' + eventId, function(err, data){
      if(data){
        data = JSON.parse(data);
      }
      if(clientId == _this.options.id || clientId == 'global'){
        _this.handleEvent(eventName, data);
      }
    });
  });

  //Listen to instant events and handle them
  this.redisInstant.subscribe('redular:instant');
  this.redisInstant.on('message', function(channel, message){
    try{
      var parsedMessage = JSON.parse(message);
    } catch (e){
      throw e;
    }
    if(parsedMessage.client == _this.options.id || parsedMessage.client == 'global') {
      _this.handleEvent(parsedMessage.event, parsedMessage.data);
    }
  });

}
function(options){
  var _this = this;

  this.handlers = {};

  if(!options){
    options = {};
  }

  if(!options.redis){
    options.redis = {};
  }

  this.options = {
    id: options.id || shortId.generate(),
    autoConfig: options.autoConfig ||  false,
    dataExpiry: options.dataExpiry || 30,
    redis: {
      port: options.redis.port || 6379,
      host: options.redis.host || '127.0.0.1',
      redis: options.redis.options || {}
    }
  };

  //Create redis clients
  this.redisSub = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);
  this.redis = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);
  this.redisInstant = redis.createClient(this.options.redis.port, this.options.redis.host, this.options.redis.options);

  //Attempt auto config
  if(this.options.autoConfig){
    var config = '';
    this.redis.config("GET", "notify-keyspace-events", function(err, data){
      if(data){
        config = data[1];
      }
      if(config.indexOf('E') == -1){
        config += 'E'
      }
      if(config.indexOf('x') == -1){
        config += 'x'
      }
      _this.redis.config("SET", "notify-keyspace-events", config)
    });
  }

  //Listen to key expiry notifications and handle events
  var expiryListener = new RedisEvent(this.redisSub, 'expired', /redular:(.+):(.+):(.+)/);
  expiryListener.defineHandler(function(key){
    var clientId = key[1];
    var eventName = key[2];
    var eventId = key[3];

    _this.redis.get('redular-data:' + clientId + ':' + eventName + ':' + eventId, function(err, data){
      if(data){
        data = JSON.parse(data);
      }
      if(clientId == _this.options.id || clientId == 'global'){
        _this.handleEvent(eventName, data);
      }
    });
  });

  //Listen to instant events and handle them
  this.redisInstant.subscribe('redular:instant');
  this.redisInstant.on('message', function(channel, message){
    try{
      var parsedMessage = JSON.parse(message);
    } catch (e){
      throw e;
    }
    if(parsedMessage.client == _this.options.id || parsedMessage.client == 'global') {
      _this.handleEvent(parsedMessage.event, parsedMessage.data);
    }
  });

}
function ( options ) { var _this = this ; this . handlers = { } ; if ( ! options ) { options = { } ; } if ( ! options . redis ) { options . redis = { } ; } this . options = { id : options . id || shortId . generate ( ) , autoConfig : options . autoConfig || false , dataExpiry : options . dataExpiry || 30 , redis : { port : options . redis . port || 6379 , host : options . redis . host || '127.0.0.1' , redis : options . redis . options || { } } } ; //Create redis clients this . redisSub = redis . createClient ( this . options . redis . port , this . options . redis . host , this . options . redis . options ) ; this . redis = redis . createClient ( this . options . redis . port , this . options . redis . host , this . options . redis . options ) ; this . redisInstant = redis . createClient ( this . options . redis . port , this . options . redis . host , this . options . redis . options ) ; //Attempt auto config if ( this . options . autoConfig ) { var config = '' ; this . redis . config ( "GET" , "notify-keyspace-events" , function ( err , data ) { if ( data ) { config = data [ 1 ] ; } if ( config . indexOf ( 'E' ) == - 1 ) { config += 'E' } if ( config . indexOf ( 'x' ) == - 1 ) { config += 'x' } _this . redis . config ( "SET" , "notify-keyspace-events" , config ) } ) ; } //Listen to key expiry notifications and handle events var expiryListener = new RedisEvent ( this . redisSub , 'expired' , / redular:(.+):(.+):(.+) / ) ; expiryListener . defineHandler ( function ( key ) { var clientId = key [ 1 ] ; var eventName = key [ 2 ] ; var eventId = key [ 3 ] ; _this . redis . get ( 'redular-data:' + clientId + ':' + eventName + ':' + eventId , function ( err , data ) { if ( data ) { data = JSON . parse ( data ) ; } if ( clientId == _this . options . id || clientId == 'global' ) { _this . handleEvent ( eventName , data ) ; } } ) ; } ) ; //Listen to instant events and handle them this . redisInstant . subscribe ( 'redular:instant' ) ; this . redisInstant . on ( 'message' , function ( channel , message ) { try { var parsedMessage = JSON . parse ( message ) ; } catch ( e ) { throw e ; } if ( parsedMessage . client == _this . options . id || parsedMessage . client == 'global' ) { _this . handleEvent ( parsedMessage . event , parsedMessage . data ) ; } } ) ; }
Node.js scheduling system powered by Redis Keyspace Notifications
@param options {Object}
@constructor
Node . js scheduling system powered by Redis Keyspace Notifications