Ejemplo n.º 1
0
		listen: function(queueName, handler) {
			if(!queueName) {
				throw new Error(consolePrefix, "Queue name must be provided. eg. 'emailQueue'.");
			}

			if(!handler) {
				throw new Error(consolePrefix, "You must provide a hander to .listen.");
			}

			if(alreadyListening) {
				throw new Error(consolePrefix, "You can only .listen once in an app. To listen to another queue, create another app.");
			}

			if(!blockingClient) {
				blockingClient = redis.createClient.apply(redis, redisArgs);
			}

			process.on("SIGINT", onKillSignal);
			process.on("SIGTERM", onKillSignal);
			alreadyListening = true;

			if(handler.length < 2) {
				console.log(consolePrefix, 'Assuming that the handler returns a promise.');
			}

			continueListening("cumin." + queueName, handler);
		}
Ejemplo n.º 2
0
  that.connect = function() {
    addClient = redis.createClient.apply(redis, arguments);
    addClient.on('error', function(err) {
      console.error(err);
    });
    addClient.on('ready', function() {
      checkReady();
    });

    shuffleClient = redis.createClient.apply(redis, arguments);
    shuffleClient.on('error', function(err) {
      console.error(err);
    });
    shuffleClient.on('ready', function() {
      checkReady();
    });
    return that;
  };
Ejemplo n.º 3
0
 util.enableRedis = function (port, host, options) {
   if (!_useRedis && redis.createClient) {
     if (port && host) {
       options = options || {};
       redis = redis.createClient.apply(this, arguments);
     } else {
       redis = redis.createClient();
     }
     _useRedis = true;
   }
 };
Ejemplo n.º 4
0
var RedisCache = function () {
    var redis = require('redis');
    this.client = redis.createClient.apply(redis, arguments);
    this.client.on('ready', function () {
        debug('Redis Cache ready');
    });
    this.client.on('error', function (err) {
        debug('Redis Cache error - ' + err);
    });
    this.defaultOptions = { autodisconnect: true, expiry: 60 };
};
Ejemplo n.º 5
0
exports.initialize = function initializeSchema(dataSource, callback) {
    if (!redis) return;

    if (dataSource.settings.url) {
        var url = require('url');
        var redisUrl = url.parse(dataSource.settings.url);
        var redisAuth = (redisUrl.auth || '').split(':');
        dataSource.settings.host = redisUrl.hostname;
        dataSource.settings.port = redisUrl.port;

        if (redisAuth.length > 1) {
            dataSource.settings.db = redisAuth[0];
            dataSource.settings.password = redisAuth.slice(1).join(':');
        }
    }

    var params = [];
    if (dataSource.settings.port) {
      params.push(dataSource.settings.port);
    }
    if (dataSource.settings.host) {
      params.push(dataSource.settings.host);
    }
    if (dataSource.settings.options) {
      params.push(dataSource.settings.options);
    }

    dataSource.client = redis.createClient.apply(redis.createClient, params);
    dataSource.client.auth(dataSource.settings.password);
    var callbackCalled = false;
    var database = dataSource.settings.hasOwnProperty('database') && dataSource.settings.database;
    dataSource.client.on('connect', function () {
        if (!callbackCalled && database === false) {
            callbackCalled = true;
            callback();
        } else if (database !== false) {
            if (callbackCalled) {
                return dataSource.client.select(dataSource.settings.database);
            } else {
                callbackCalled = true;
                return dataSource.client.select(dataSource.settings.database, callback);
            }
        }
    });
    dataSource.client.on('error', function (error) {
        console.log(error);
    })

    var clientWrapper = new Client(dataSource.client);

    dataSource.connector = new BridgeToRedis(clientWrapper);
    clientWrapper._adapter = dataSource.connector;
};
Ejemplo n.º 6
0
var Redback = exports.Redback = function (client, options) {
    if (typeof client === 'object' && client && client.command_queue && client.offline_queue) {
        this.client = client;
    } else {
        this.client = redis.createClient.apply(this, arguments);
        options = arguments[1] instanceof Object ?
            arguments[1] : arguments[2];
    }
    this.namespace = '';
    if (typeof options === 'object' && options.namespace) {
        this.namespace = options.namespace;
    }
}
Ejemplo n.º 7
0
Pooldis.prototype._createClient = function(callback){
  var client = Redis.createClient.apply(Redis, this.connectArgs);
  if (this.connectAuth) {
    debug("authenticating client");
    return client.auth(this.connectAuth, function(error){
      debug("authenticated");
      debug("connected");
      callback(error, client);
    });
  }
  process.nextTick(function(){
    debug("connected");
    callback(null, client);
  });
};
Ejemplo n.º 8
0
Client.prototype.connect = function(hallow) {
  var self = this;
  self.redisClient = redis.createClient.apply(null, self.redisSettings);
  self.redisClient.on('error', function(err) {
    self.emit('error', err);
  });
  self.redisClient.once('connect', function() {
    var m = self.redisClient.multi();
    if (self.redisOptions && self.redisOptions.overwriteConfig) {
      _.forEach(self.redisOptions, function(val, key) {
        if (/^config_\w+/.test(key)) {
          m.config('set', key.replace('config_', ''), val);
        }
      });
    }
    m.exec(function(err) {
      if (err) {
        return self.emit('error', err);
      }

      if (hallow) {
        self.db = {
          s: {
            databaseName: hallow
          },
          hallow: true
        };
        self.emit('connect',
          new CacheLayer(self, self.db, self.redisClient));
      } else {
        mongo.MongoClient.connect.apply(null, [self.mongoSettings[0],
          self.mongoSettings[1],
          function(err, db) {
            if (err) {
              self.emit('error', err);
            } else {
              self.db = db;
              self.emit('connect',
                new CacheLayer(self, self.db, self.redisClient)
              );
            }
          }
        ]);
      }
    });
  });
  return self;
};
Ejemplo n.º 9
0
 middleWare.sio.on('connection', function (socket) {
   socket.pubsubClient = redis.createClient.apply(redis, middleWare.redisClientArgs);
   socket.on('redis-web', function (data) {
     data.args.push(function () {
       var d = {
         id: data.id,
         args: Array.prototype.slice.call(arguments)
       };
       socket.emit('redis-web', d);
     });
     socket.pubsubClient[data.cmd].apply(socket.pubsubClient, data.args);
   });
   socket.on('disconnect', function () {
     socket.pubsubClient.quit();
   })
 });
Ejemplo n.º 10
0
function RedisTemp(options) {
    Temp.call(this, options);

    if ('undefined' == typeof options.prefix) {
        this.options.prefix = 'rtcs.io:'
    }
    if ('undefined' == typeof options.prefixUserSockets) {
        this.options.prefixUserSockets = 'userSockets:'
    }
    if ('undefined' == typeof options.prefixUserRooms) {
        this.options.prefixUserRooms = 'userRooms:'
    }
    if ('undefined' == typeof options.prefixRoomUsers) {
        this.options.prefixRoomUsers = 'roomUsers:'
    }
    if ('undefined' == typeof options.prefixToken) {
        this.options.prefixToken = 'token:'
    }

    this.options.prefixUserSockets = this.options.prefix + this.options.prefixUserSockets;
    this.options.prefixUserRooms = this.options.prefix + this.options.prefixUserRooms;
    this.options.prefixRoomUsers = this.options.prefix + this.options.prefixRoomUsers;
    this.options.prefixToken = this.options.prefix + this.options.prefixToken;

    if (options.client) {
        this.redis = options.client;
    } else {
        var args = [];
        if (options.socket) {
            args.push(options.socket);
        } else if (options.port && options.host) {
            args.push(options.port);
            args.push(options.host);
        }
        args.push(options);

        this.redis = redis.createClient.apply(null, args);
    }

    var self = this;
    self.redis.on('error', function (err) {
        self.emit('error', err);
    });
}
Ejemplo n.º 11
0
Client.prototype.createCacheClient = function(cb) {
  var self = this;
  var newRedisClient = redis.createClient.apply(null, self.redisSettings);
  var called;
  newRedisClient.once('error', function(err) {
    if (!called) {
      called = true;
      if (typeof cb === 'function') {
        cb(err);
      }
    } else if (called && err) {
      throw err;
    }
  });
  newRedisClient.once('connect', function() {
    if (!called && typeof cb === 'function') {
      called = true;
      cb(null, newRedisClient);
    }
  });
  return self;
};
Ejemplo n.º 12
0
module.exports.createClient = function() {
  var client = redis.createClient.apply(redis, arguments);

  return require('co-redis')(client);
};
function RedisScanner() {
  this._client = redis.createClient.apply(redis, arguments);
  this._patternArray = [];
  this._handlerByPattern = {};
  this._isScanning = false;
}
Ejemplo n.º 14
0
 constructor(){
   this.pubLoginRedis = redis.createClient.apply(null, config.redis);
   this.subLoginRedis = redis.createClient.apply(null, config.redis);
   [this.pubLoginRedis, this.subLoginRedis].forEach(r => {r.on('error', utils.log.error);});
 }
Ejemplo n.º 15
0
module.exports = function(port, host, options) {
	var redisArgs = arguments;

	var nonBlockingClient = redis.createClient.apply(redis, redisArgs),
		blockingClient;

	var redisBlpopTimeout = 1;

	var alreadyListening = false,
		killSignalReceived = false,
		pendingTasks = 0,
		killWaitTimeout = 20;

	function onKillSignal() {
		if(!killSignalReceived) {
			killSignalReceived = true;
			console.info("\n" + consolePrefix, "Attempting clean shutdown...");
			console.info(consolePrefix, "To force shutdown, hit Ctrl+C again.");
			console.info(consolePrefix, "Waiting upto", redisBlpopTimeout, "seconds for next chance to kill the redis connection...");
			setTimeout(function() {
				console.info(consolePrefix, "Forcing kill due to", killWaitTimeout, "seconds timeout.");
				process.exit();
			}, killWaitTimeout * 1000)
		} else {
			console.info("\n" + consolePrefix, "Forcing shutdown now.");
			setTimeout(process.exit, 500);
		}
	}

	function attemptCleanShutdown() {
		console.info(consolePrefix, "Not reconnecting to redis because of kill signal received.");
		if(pendingTasks == 0) {
			console.info(consolePrefix, "No pending tasks. Exiting now.");
			process.exit();
		} else {
			console.info(consolePrefix, "Waiting for pending tasks to be completed. Pending count:", pendingTasks);
		}
	}

	function continueListening(queueName, handler) {
		var promiseMode = (handler.length < 2);

		if(killSignalReceived) return attemptCleanShutdown();

		blockingClient.blpop(queueName, redisBlpopTimeout, function(err, data) {
			if(err) return console.log(err);

			if(data) {
				var bareQueueName = queueName.slice(("cumin.").length);
				nonBlockingClient.hset("cuminmeta." + bareQueueName, "lastDequeued", Date.now());
				nonBlockingClient.publish("cumin.dequeued", data[1]);

				var queueItem = JSON.parse(data[1]);

				var handlerOnComplete = function() {
					pendingTasks--;
					
					nonBlockingClient.hset("cuminmeta." + bareQueueName, "completed", Date.now());
					nonBlockingClient.publish("cumin.processed", data[1]);

					if(killSignalReceived && pendingTasks) {
						console.info(consolePrefix, "Waiting for pending tasks to be completed. Pending count:", pendingTasks);
					}

					if(killSignalReceived && !pendingTasks) {
						console.info(consolePrefix, "Pending tasks completed. Shutting down now.");
						process.exit();
					}
				}

				pendingTasks++;

				if(promiseMode) {
					handler(queueItem.data).then(handlerOnComplete);
				} else {
					handler(queueItem.data, handlerOnComplete);
				}
			}

			process.nextTick(function() {
				continueListening(queueName, handler);
			});
		});
	}

	return {
		enqueue: promisify(function(queueName, queueData, done) {
			if(!queueName) {
				throw new Error("Queue name must be provided. eg. 'emailQueue'.");
			}

			var now = Date.now();
			var message = JSON.stringify({
				byPid: process.pid,
				byTitle: process.title,
				queueName: queueName,
				date: now,
				data: queueData,
				retryCount: 0
			});

			nonBlockingClient.sadd("cuminqueues", queueName);
			nonBlockingClient.hset("cuminmeta." + queueName, "lastEnqueued", now);
			nonBlockingClient.rpush("cumin." + queueName, message);
			nonBlockingClient.publish("cumin.enqueued", message, done);
		}),

		listen: function(queueName, handler) {
			if(!queueName) {
				throw new Error(consolePrefix, "Queue name must be provided. eg. 'emailQueue'.");
			}

			if(!handler) {
				throw new Error(consolePrefix, "You must provide a hander to .listen.");
			}

			if(alreadyListening) {
				throw new Error(consolePrefix, "You can only .listen once in an app. To listen to another queue, create another app.");
			}

			if(!blockingClient) {
				blockingClient = redis.createClient.apply(redis, redisArgs);
			}

			process.on("SIGINT", onKillSignal);
			process.on("SIGTERM", onKillSignal);
			alreadyListening = true;

			if(handler.length < 2) {
				console.log(consolePrefix, 'Assuming that the handler returns a promise.');
			}

			continueListening("cumin." + queueName, handler);
		}
	}
}
Ejemplo n.º 16
0
module.exports = function () {
  var middleWare = function (req, res, next) {
    if (req.url.indexOf('/redis-web.js') >= 0) {
      res.setHeader('content-type', 'text/javascript');
      return res.end(clientScript);
    }

    if (req.url.indexOf('/redis-web-post') >= 0) {
      req.body = '';
      req.on('data', function (d) {
        req.body += d.toString('utf8');
      });
      req.on('end', function (d) {
        if (d) {
          req.body += d.toString('utf8');
        }
        try {
          var data = JSON.parse(req.body);

          data.args.push(function (err, results) {
            return res.end(JSON.stringify({
              err: err,
              results: results
            }));
          });
          middleWare.cmdClient[data.cmd].apply(middleWare.cmdClient, data.args);
        } catch (ex) {
          return next(new Error("Invalid POST body"));
        }
      });
      return;
    }

    return next();
  };

  middleWare.redisClientArgs = arguments;

  middleWare.setSocketIo = function (sio) {
    middleWare.sio = sio;
    middleWare.sio.on('connection', function (socket) {
      socket.pubsubClient = redis.createClient.apply(redis, middleWare.redisClientArgs);
      socket.on('redis-web', function (data) {
        data.args.push(function () {
          var d = {
            id: data.id,
            args: Array.prototype.slice.call(arguments)
          };
          socket.emit('redis-web', d);
        });
        socket.pubsubClient[data.cmd].apply(socket.pubsubClient, data.args);
      });
      socket.on('disconnect', function () {
        socket.pubsubClient.quit();
      })
    });
  };

  middleWare.cmdClient = redis.createClient.apply(redis, middleWare.redisClientArgs);

  return middleWare;
};
Ejemplo n.º 17
0
var redisClient = function (args) {
    return redis.createClient.apply(redis, args);
};
Ejemplo n.º 18
0
 var makeClient = function (clientName) {
   this[clientName] = redis.createClient.apply(redis, this.settings.redis.params);
   this[clientName].on('error', this.emit.bind(this, 'error'));
   this[clientName].select(this.settings.redis.db, reportReady);
 }.bind(this);