Readd Credis

This commit is contained in:
Daniel Mason 2018-05-30 19:05:13 +12:00
parent 493d6dc6d8
commit bd0a0c2dc8
5 changed files with 1599 additions and 26 deletions

View File

@ -1,3 +1,6 @@
## 1.4.2 (2018-05-30)
- Reimplemented credis due to issues with Redis: Connection Closed.
## 1.4.1 (2018-05-29) ## 1.4.1 (2018-05-29)
- Updated travis builds to run on PHP 7.0, 7.1 and 7.2. - Updated travis builds to run on PHP 7.0, 7.1 and 7.2.
- Added ability to specify multiple log levels. [DEBUG/INFO/NOTICE/WARNING/ERROR/CRITICAL/ALERT/EMERGENCY] - Added ability to specify multiple log levels. [DEBUG/INFO/NOTICE/WARNING/ERROR/CRITICAL/ALERT/EMERGENCY]
@ -7,7 +10,7 @@
## 1.4 (2018-05-25) ## 1.4 (2018-05-25)
- Forked from chrisboulton/php-resque. - Forked from chrisboulton/php-resque.
- Replaced credis (rather unmaintained) in favour of phpredis. - Replaced credis in favour of phpredis.
- Reformatted codebase to be PSR2 compliant. - Reformatted codebase to be PSR2 compliant.

View File

@ -1,6 +1,6 @@
{ {
"name": "idanoo/php-resque", "name": "idanoo/php-resque",
"version": "1.4.1", "version": "1.4.2",
"type": "library", "type": "library",
"description": "Redis backed library for creating background jobs and processing them later. Based on resque for Ruby. Originally forked from chrisboulton/php-resque.", "description": "Redis backed library for creating background jobs and processing them later. Based on resque for Ruby. Originally forked from chrisboulton/php-resque.",
"keywords": ["job", "background", "redis", "resque"], "keywords": ["job", "background", "redis", "resque"],
@ -13,10 +13,11 @@
} }
], ],
"require": { "require": {
"php": ">=7.0.0", "php": "^7.0",
"ext-pcntl": "*", "ext-pcntl": "*",
"ext-redis": "*", "ext-redis": "*",
"psr/log": "~1.0" "psr/log": "~1.0",
"colinmollenhour/credis": "^1.10"
}, },
"suggest": { "suggest": {
"ext-proctitle": "Allows php-resque to rename the title of UNIX processes to show the status of a worker." "ext-proctitle": "Allows php-resque to rename the title of UNIX processes to show the status of a worker."

1570
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,12 @@
class Resque_Redis class Resque_Redis
{ {
/**
* Redis Client
* @var Credis_Client
*/
private $driver;
/** /**
* Redis namespace * Redis namespace
* @var string * @var string
@ -110,41 +116,36 @@ class Resque_Redis
* @param string|array $server A DSN or array * @param string|array $server A DSN or array
* @param int $database A database number to select. However, if we find a valid database number in the DSN the * @param int $database A database number to select. However, if we find a valid database number in the DSN the
* DSN-supplied value will be used instead and this parameter is ignored. * DSN-supplied value will be used instead and this parameter is ignored.
* @param object $client Optional Credis_Cluster or Credis_Client instance instantiated by you * @param object $client Optional Credis_Client instance instantiated by you
* @throws Resque_RedisException * @throws Resque_RedisException
*/ */
public function __construct($server, $database = null, $client = null) public function __construct($server, $database = null, $client = null)
{ {
try { try {
if (is_object($client)) { if (is_object($client)) {
$this->redisConnection = $client; $this->driver = $client;
} else { } else {
/** @noinspection PhpUnusedLocalVariableInspection */ /** @noinspection PhpUnusedLocalVariableInspection */
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
// $user is not used, only $password // $user is not used, only $password
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null; $timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
$this->redisConnection = new Redis(); $maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0;
$this->driver = new Credis_Client($host, $port, $timeout, $persistent);
if (!$this->redisConnection->connect($host, $port, $timeout)) { $this->driver->setMaxConnectRetries($maxRetries);
throw new RedisException("Connection Failed to Redis!");
};
if ($password) { if ($password) {
$this->redisConnection->auth($password); $this->driver->auth($password);
} }
// If we have found a database in our DSN, use it instead of the `$database` // If we have found a database in our DSN, use it instead of the `$database`
// value passed into the constructor. // value passed into the constructor.
if ($dsnDatabase !== false) { if ($dsnDatabase !== false) {
$database = $dsnDatabase; $database = $dsnDatabase;
} }
if ($database) {
$this->redisConnection->select($database);
}
} }
} catch (RedisException $e) { if ($database !== null) {
$this->driver->select($database);
}
} catch (Exception $e) {
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
} }
} }
@ -245,8 +246,8 @@ class Resque_Redis
} }
} }
try { try {
return call_user_func_array([$this->redisConnection, $name], $args); return $this->driver->__call($name, $args);
} catch (Exception $e) { } catch (CredisException $e) {
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e); throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
} }
} }

View File

@ -20,10 +20,8 @@ class Resque_Tests_TestCase extends PHPUnit\Framework\TestCase
public function setUp() public function setUp()
{ {
// Setup redis connection on DB 9 for testing. // Setup redis connection for testing.
$this->redis = new Redis(); $this->redis = new Credis_Client('localhost', '6379');
$this->redis->connect('localhost');
Resque::setBackend('localhost'); Resque::setBackend('localhost');
$this->redis->flushAll(); $this->redis->flushAll();
} }