Skip to content

Commit 81cb92b

Browse files
committed
throw Resque_RedisException whenever an errror connecting/talking to redis occurs
1 parent ee9f133 commit 81cb92b

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

lib/Resque/Redis.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,38 @@ public static function prefix($namespace)
111111
*/
112112
public function __construct($server, $database = null)
113113
{
114-
if (is_array($server)) {
115-
$this->driver = new Credis_Cluster($server);
116-
}
117-
else {
114+
try {
115+
if (is_array($server)) {
116+
$this->driver = new Credis_Cluster($server);
117+
}
118+
else {
119+
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
120+
// $user is not used, only $password
118121

119-
list($host, $port, $dsnDatabase, $user, $password, $options) = self::parseDsn($server);
120-
// $user is not used, only $password
122+
// Look for known Credis_Client options
123+
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
124+
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
125+
$maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0;
121126

122-
// Look for known Credis_Client options
123-
$timeout = isset($options['timeout']) ? intval($options['timeout']) : null;
124-
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
125-
$maxRetries = isset($options['max_connect_retries']) ? $options['max_connect_retries'] : 0;
127+
$this->driver = new Credis_Client($host, $port, $timeout, $persistent);
128+
$this->driver->setMaxConnectRetries($maxRetries);
129+
if ($password){
130+
$this->driver->auth($password);
131+
}
126132

127-
$this->driver = new Credis_Client($host, $port, $timeout, $persistent);
128-
$this->driver->setMaxConnectRetries($maxRetries);
129-
if ($password){
130-
$this->driver->auth($password);
133+
// If we have found a database in our DSN, use it instead of the `$database`
134+
// value passed into the constructor.
135+
if ($dsnDatabase !== false) {
136+
$database = $dsnDatabase;
137+
}
131138
}
132139

133-
// If we have found a database in our DSN, use it instead of the `$database`
134-
// value passed into the constructor.
135-
if ($dsnDatabase !== false) {
136-
$database = $dsnDatabase;
140+
if ($database !== null) {
141+
$this->driver->select($database);
137142
}
138143
}
139-
140-
if ($database !== null) {
141-
$this->driver->select($database);
144+
catch(CredisException $e) {
145+
throw new Resque_RedisException($e);
142146
}
143147
}
144148

@@ -241,7 +245,7 @@ public function __call($name, $args)
241245
return $this->driver->__call($name, $args);
242246
}
243247
catch (CredisException $e) {
244-
return false;
248+
throw new Resque_RedisException($e);
245249
}
246250
}
247251

test/Resque/Tests/JobTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public function testJobCanBeQueued()
2626
$this->assertTrue((bool)Resque::enqueue('jobs', 'Test_Job'));
2727
}
2828

29+
/**
30+
* @expectedException Resque_RedisException
31+
*/
32+
public function testRedisErrorThrowsExceptionOnJobCreation()
33+
{
34+
Resque::setBackend('redis://255.255.255.255:1234');
35+
Resque::enqueue('jobs', 'This is a test');
36+
}
37+
2938
public function testQeueuedJobCanBeReserved()
3039
{
3140
Resque::enqueue('jobs', 'Test_Job');

test/Resque/Tests/DsnTest.php renamed to test/Resque/Tests/RedisTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
<?php
22
/**
3-
* Resque_Redis DSN tests.
3+
* Resque_Event tests.
44
*
55
* @package Resque/Tests
6-
* @author Iskandar Najmuddin <github@iskandar.co.uk>
6+
* @author Chris Boulton <chris@bigcommerce.com>
77
* @license http://www.opensource.org/licenses/mit-license.php
88
*/
9-
class Resque_Tests_DsnTest extends Resque_Tests_TestCase
9+
class Resque_Tests_RedisTest extends Resque_Tests_TestCase
1010
{
11+
/**
12+
* @expectedException Resque_RedisException
13+
*/
14+
public function testRedisExceptionsAreSurfaced()
15+
{
16+
$redis = new Resque_Redis('redis://255.255.255.255:1234');
17+
$redis->ping();
18+
}
1119

1220
/**
1321
* These DNS strings are considered valid.
@@ -178,5 +186,4 @@ public function testParsingBogusDsnStringThrowsException($dsn)
178186
// The next line should throw an InvalidArgumentException
179187
$result = Resque_Redis::parseDsn($dsn);
180188
}
181-
182-
}
189+
}

test/Resque/Tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function setUp()
2222
preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches);
2323
$this->redis = new Credis_Client('localhost', $matches[1]);
2424

25+
Resque::setBackend('redis://localhost:' . $matches[1]);
26+
2527
// Flush redis
2628
$this->redis->flushAll();
2729
}

0 commit comments

Comments
 (0)