Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions lib/Resque/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,38 @@ public static function prefix($namespace)
*/
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('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
}
}

Expand Down Expand Up @@ -241,7 +245,7 @@ public function __call($name, $args)
return $this->driver->__call($name, $args);
}
catch (CredisException $e) {
return false;
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
}
}

Expand Down
12 changes: 12 additions & 0 deletions lib/Resque/RedisException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Redis related exceptions
*
* @package Resque
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_RedisException extends Resque_Exception
{
}
?>
9 changes: 9 additions & 0 deletions test/Resque/Tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public function testJobCanBeQueued()
$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');
Expand Down
17 changes: 12 additions & 5 deletions test/Resque/Tests/DsnTest.php → test/Resque/Tests/RedisTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php
/**
* Resque_Redis DSN tests.
* Resque_Event tests.
*
* @package Resque/Tests
* @author Iskandar Najmuddin <[email protected]>
* @author Chris Boulton <[email protected]>
* @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.
Expand Down Expand Up @@ -178,5 +186,4 @@ public function testParsingBogusDsnStringThrowsException($dsn)
// The next line should throw an InvalidArgumentException
$result = Resque_Redis::parseDsn($dsn);
}

}
}
2 changes: 2 additions & 0 deletions test/Resque/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function setUp()
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();
}
Expand Down