Example #1
0
 /**
  * Return an instance of the Redis class instantiated for Resque.
  * @return Redis Instance of Redis.
  */
 public static function redis()
 {
     // Detect when the PID of the current process has changed (from a fork, etc)
     // and force a reconnect to redis.
     $pid = getmypid();
     if (self::$pid !== $pid) {
         self::$redis = null;
         self::$pid = $pid;
     }
     if (!is_null(self::$redis)) {
         return self::$redis;
     }
     $server = self::$redisServer;
     if (empty($server)) {
         $server = 'localhost:6379';
     }
     if (is_array($server)) {
         self::$redis = new RedisCluster($server);
     } else {
         if (strpos($server, 'unix:') === false) {
             list($host, $port) = explode(':', $server);
         } else {
             // $server is 'unix:/tmp/redis.sock'
             $host = $server;
             $port = null;
         }
         self::$redis = new Redis($host, $port);
         if (!empty(self::$redisPassword)) {
             self::$redis->auth(self::$redisPassword);
         }
     }
     self::$redis->select(self::$redisDatabase);
     return self::$redis;
 }
Example #2
0
 /**
  * fork() helper method for php-resque that handles issues PHP socket
  * and phpredis have with passing around sockets between child/parent
  * processes.
  *
  * Will close connection to Redis before forking.
  *
  * @return int Return vars as per pcntl_fork()
  */
 public static function fork()
 {
     if (!function_exists('pcntl_fork')) {
         return -1;
     }
     // Close the connection to Redis before forking.
     // This is a workaround for issues phpredis has.
     self::$redis = null;
     $pid = pcntl_fork();
     if ($pid === -1) {
         throw new RuntimeException('Unable to fork child worker.');
     }
     return $pid;
 }