mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-25 01:35:14 +00:00
Merge pull request #229 from chrisboulton/exceptions
Surface Redis exceptions instead of silently returning false
This commit is contained in:
commit
6b560798c4
@ -111,11 +111,11 @@ class Resque_Redis
|
|||||||
*/
|
*/
|
||||||
public function __construct($server, $database = null)
|
public function __construct($server, $database = null)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
if (is_array($server)) {
|
if (is_array($server)) {
|
||||||
$this->driver = new Credis_Cluster($server);
|
$this->driver = new Credis_Cluster($server);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -141,6 +141,10 @@ class Resque_Redis
|
|||||||
$this->driver->select($database);
|
$this->driver->select($database);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch(CredisException $e) {
|
||||||
|
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a DSN string, which can have one of the following formats:
|
* Parse a DSN string, which can have one of the following formats:
|
||||||
@ -241,7 +245,7 @@ class Resque_Redis
|
|||||||
return $this->driver->__call($name, $args);
|
return $this->driver->__call($name, $args);
|
||||||
}
|
}
|
||||||
catch (CredisException $e) {
|
catch (CredisException $e) {
|
||||||
return false;
|
throw new Resque_RedisException('Error communicating with Redis: ' . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
12
lib/Resque/RedisException.php
Normal file
12
lib/Resque/RedisException.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Redis related exceptions
|
||||||
|
*
|
||||||
|
* @package Resque
|
||||||
|
* @author Chris Boulton <chris@bigcommerce.com>
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.php
|
||||||
|
*/
|
||||||
|
class Resque_RedisException extends Resque_Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
?>
|
@ -26,6 +26,15 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
|
|||||||
$this->assertTrue((bool)Resque::enqueue('jobs', 'Test_Job'));
|
$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()
|
public function testQeueuedJobCanBeReserved()
|
||||||
{
|
{
|
||||||
Resque::enqueue('jobs', 'Test_Job');
|
Resque::enqueue('jobs', 'Test_Job');
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Resque_Redis DSN tests.
|
* Resque_Event tests.
|
||||||
*
|
*
|
||||||
* @package Resque/Tests
|
* @package Resque/Tests
|
||||||
* @author Iskandar Najmuddin <github@iskandar.co.uk>
|
* @author Chris Boulton <chris@bigcommerce.com>
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php
|
* @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.
|
* 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
|
// The next line should throw an InvalidArgumentException
|
||||||
$result = Resque_Redis::parseDsn($dsn);
|
$result = Resque_Redis::parseDsn($dsn);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -22,6 +22,8 @@ class Resque_Tests_TestCase extends PHPUnit_Framework_TestCase
|
|||||||
preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches);
|
preg_match('#^\s*port\s+([0-9]+)#m', $config, $matches);
|
||||||
$this->redis = new Credis_Client('localhost', $matches[1]);
|
$this->redis = new Credis_Client('localhost', $matches[1]);
|
||||||
|
|
||||||
|
Resque::setBackend('redis://localhost:' . $matches[1]);
|
||||||
|
|
||||||
// Flush redis
|
// Flush redis
|
||||||
$this->redis->flushAll();
|
$this->redis->flushAll();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user