Merge pull request #27 from hlegius/master

Namespace support (prefix)
This commit is contained in:
Chris Boulton 2011-11-23 13:57:55 -08:00
commit 45c49cf002
3 changed files with 54 additions and 5 deletions

View File

@ -16,6 +16,11 @@ if(!class_exists('Redisent')) {
*/ */
class Resque_Redis extends Redisent class Resque_Redis extends Redisent
{ {
/**
* Redis namespace
* @var string
*/
private static $defaultNamespace = 'resque:';
/** /**
* @var array List of all commands in Redis that supply a key as their * @var array List of all commands in Redis that supply a key as their
* first argument. Used to prefix keys with the Resque namespace. * first argument. Used to prefix keys with the Resque namespace.
@ -77,9 +82,21 @@ class Resque_Redis extends Redisent
// mset // mset
// renamenx // renamenx
/**
* Set Redis namespace (prefix) default: resque
* @param string $namespace
*/
public static function prefix($namespace)
{
if (strpos($namespace, ':') === false) {
$namespace .= ':';
}
self::$defaultNamespace = $namespace;
}
/** /**
* Magic method to handle all function requests and prefix key based * Magic method to handle all function requests and prefix key based
* operations with the 'resque:' key prefix. * operations with the {self::$defaultNamespace} key prefix.
* *
* @param string $name The name of the method called. * @param string $name The name of the method called.
* @param array $args Array of supplied arguments to the method. * @param array $args Array of supplied arguments to the method.
@ -88,7 +105,7 @@ class Resque_Redis extends Redisent
public function __call($name, $args) { public function __call($name, $args) {
$args = func_get_args(); $args = func_get_args();
if(in_array($name, $this->keyCommands)) { if(in_array($name, $this->keyCommands)) {
$args[1][0] = 'resque:' . $args[1][0]; $args[1][0] = self::$defaultNamespace . $args[1][0];
} }
try { try {
return parent::__call($name, $args[1]); return parent::__call($name, $args[1]);

View File

@ -16,6 +16,11 @@ if(!class_exists('RedisentCluster')) {
*/ */
class Resque_RedisCluster extends RedisentCluster class Resque_RedisCluster extends RedisentCluster
{ {
/**
* Redis namespace
* @var string
*/
private static $defaultNamespace = 'resque:';
/** /**
* @var array List of all commands in Redis that supply a key as their * @var array List of all commands in Redis that supply a key as their
* first argument. Used to prefix keys with the Resque namespace. * first argument. Used to prefix keys with the Resque namespace.
@ -77,9 +82,21 @@ class Resque_RedisCluster extends RedisentCluster
// mset // mset
// renamenx // renamenx
/**
* Set Redis namespace (prefix) default: resque
* @param string $namespace
*/
public static function prefix($namespace)
{
if (strpos($namespace, ':') === false) {
$namespace .= ':';
}
self::$defaultNamespace = $namespace;
}
/** /**
* Magic method to handle all function requests and prefix key based * Magic method to handle all function requests and prefix key based
* operations with the 'resque:' key prefix. * operations with the '{self::$defaultNamespace}' key prefix.
* *
* @param string $name The name of the method called. * @param string $name The name of the method called.
* @param array $args Array of supplied arguments to the method. * @param array $args Array of supplied arguments to the method.
@ -88,7 +105,7 @@ class Resque_RedisCluster extends RedisentCluster
public function __call($name, $args) { public function __call($name, $args) {
$args = func_get_args(); $args = func_get_args();
if(in_array($name, $this->keyCommands)) { if(in_array($name, $this->keyCommands)) {
$args[1][0] = 'resque:' . $args[1][0]; $args[1][0] = self::$defaultNamespace . $args[1][0];
} }
try { try {
return parent::__call($name, $args[1]); return parent::__call($name, $args[1]);

View File

@ -100,6 +100,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$this->assertEquals($job->payload['args'], $newJob->getArguments()); $this->assertEquals($job->payload['args'], $newJob->getArguments());
} }
public function testFailedJobExceptionsAreCaught() public function testFailedJobExceptionsAreCaught()
{ {
$payload = array( $payload = array(
@ -166,4 +167,18 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase
$this->assertTrue(Test_Job_With_TearDown::$called); $this->assertTrue(Test_Job_With_TearDown::$called);
} }
public function testJobWithNamespace()
{
Resque_Redis::prefix('php');
$queue = 'jobs';
$payload = array('another_value');
Resque::enqueue($queue, 'Test_Job_With_TearDown', $payload);
$this->assertEquals(Resque::queues(), array('jobs'));
$this->assertEquals(Resque::size($queue), 1);
Resque_Redis::prefix('resque');
$this->assertEquals(Resque::size($queue), 0);
}
} }