From 81cb92b9640aeb278b71e98b2ce0fe77cf2e522e Mon Sep 17 00:00:00 2001 From: Chris Boulton Date: Mon, 2 Feb 2015 14:00:16 -0800 Subject: [PATCH] throw Resque_RedisException whenever an errror connecting/talking to redis occurs --- lib/Resque/Redis.php | 48 ++++++++++--------- test/Resque/Tests/JobTest.php | 9 ++++ .../Tests/{DsnTest.php => RedisTest.php} | 17 +++++-- test/Resque/Tests/TestCase.php | 2 + 4 files changed, 49 insertions(+), 27 deletions(-) rename test/Resque/Tests/{DsnTest.php => RedisTest.php} (91%) diff --git a/lib/Resque/Redis.php b/lib/Resque/Redis.php index 1ba4ccc..2c0a11e 100644 --- a/lib/Resque/Redis.php +++ b/lib/Resque/Redis.php @@ -111,34 +111,38 @@ class Resque_Redis */ public function __construct($server, $database = null) { - if (is_array($server)) { - $this->driver = new Credis_Cluster($server); - } - else { + try { + if (is_array($server)) { + $this->driver = new Credis_Cluster($server); + } + else { + list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); + // $user is not used, only $password - list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server); - // $user is not used, only $password + // Look for known Credis_Client options + $timeout = isset($options['timeout']) ? intval($options['timeout']) : null; + $persistent = isset($options['persistent']) ? $options['persistent'] : ''; + $maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0; - // Look for known Credis_Client options - $timeout = isset($options['timeout']) ? intval($options['timeout']) : null; - $persistent = isset($options['persistent']) ? $options['persistent'] : ''; - $maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0; + $this->driver = new Credis_Client($host, $port, $timeout, $persistent); + $this->driver->setMaxConnectRetries($maxRetries); + if ($password){ + $this->driver->auth($password); + } - $this->driver = new Credis_Client($host, $port, $timeout, $persistent); - $this->driver->setMaxConnectRetries($maxRetries); - if ($password){ - $this->driver->auth($password); + // If we have found a database in our DSN, use it instead of the `$database` + // value passed into the constructor. + if ($dsnDatabase !== false) { + $database = $dsnDatabase; + } } - // If we have found a database in our DSN, use it instead of the `$database` - // value passed into the constructor. - if ($dsnDatabase !== false) { - $database = $dsnDatabase; + if ($database !== null) { + $this->driver->select($database); } } - - if ($database !== null) { - $this->driver->select($database); + catch(CredisException $e) { + throw new Resque_RedisException($e); } } @@ -241,7 +245,7 @@ class Resque_Redis return $this->driver->__call($name, $args); } catch (CredisException $e) { - return false; + throw new Resque_RedisException($e); } } diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index fe58ce4..ab55673 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -26,6 +26,15 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->assertTrue((bool)Resque::enqueue('jobs', 'Test_Job')); } + /** + * @expectedException Resque_RedisException + */ + public function testRedisErrorThrowsExceptionOnJobCreation() + { + Resque::setBackend('redis://255.255.255.255:1234'); + Resque::enqueue('jobs', 'This is a test'); + } + public function testQeueuedJobCanBeReserved() { Resque::enqueue('jobs', 'Test_Job'); diff --git a/test/Resque/Tests/DsnTest.php b/test/Resque/Tests/RedisTest.php similarity index 91% rename from test/Resque/Tests/DsnTest.php rename to test/Resque/Tests/RedisTest.php index 086db1e..a2895de 100644 --- a/test/Resque/Tests/DsnTest.php +++ b/test/Resque/Tests/RedisTest.php @@ -1,13 +1,21 @@ + * @author Chris Boulton * @license http://www.opensource.org/licenses/mit-license.php */ -class Resque_Tests_DsnTest extends Resque_Tests_TestCase +class Resque_Tests_RedisTest extends Resque_Tests_TestCase { + /** + * @expectedException Resque_RedisException + */ + public function testRedisExceptionsAreSurfaced() + { + $redis = new Resque_Redis('redis://255.255.255.255:1234'); + $redis->ping(); + } /** * These DNS strings are considered valid. @@ -178,5 +186,4 @@ class Resque_Tests_DsnTest extends Resque_Tests_TestCase // The next line should throw an InvalidArgumentException $result = Resque_Redis::parseDsn($dsn); } - -} +} \ No newline at end of file diff --git a/test/Resque/Tests/TestCase.php b/test/Resque/Tests/TestCase.php index 6a01219..a97f64b 100644 --- a/test/Resque/Tests/TestCase.php +++ b/test/Resque/Tests/TestCase.php @@ -22,6 +22,8 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches); $this->redis = new Credis_Client('localhost', $matches[1]); + Resque::setBackend('redis://localhost:' . $matches[1]); + // Flush redis $this->redis->flushAll(); }