From 016a7a13fdd658f41206507338611f39954745fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9lio=20Costa=20e=20Silva?= Date: Tue, 22 Nov 2011 14:55:40 -0200 Subject: [PATCH] Added support of Redis prefix (namespaces) --- lib/Resque/Redis.php | 23 ++++++++++++++++++++--- lib/Resque/RedisCluster.php | 21 +++++++++++++++++++-- test/Resque/Tests/JobTest.php | 15 +++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/Resque/Redis.php b/lib/Resque/Redis.php index 874bf69..0d67a74 100644 --- a/lib/Resque/Redis.php +++ b/lib/Resque/Redis.php @@ -16,6 +16,11 @@ if(!class_exists('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 * first argument. Used to prefix keys with the Resque namespace. @@ -76,10 +81,22 @@ class Resque_Redis extends Redisent // msetnx // mset // 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 - * operations with the 'resque:' key prefix. + * operations with the {self::$defaultNamespace} key prefix. * * @param string $name The name of the method called. * @param array $args Array of supplied arguments to the method. @@ -88,7 +105,7 @@ class Resque_Redis extends Redisent public function __call($name, $args) { $args = func_get_args(); if(in_array($name, $this->keyCommands)) { - $args[1][0] = 'resque:' . $args[1][0]; + $args[1][0] = self::$defaultNamespace . $args[1][0]; } try { return parent::__call($name, $args[1]); diff --git a/lib/Resque/RedisCluster.php b/lib/Resque/RedisCluster.php index 6ac15e3..9297287 100644 --- a/lib/Resque/RedisCluster.php +++ b/lib/Resque/RedisCluster.php @@ -16,6 +16,11 @@ if(!class_exists('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 * first argument. Used to prefix keys with the Resque namespace. @@ -76,10 +81,22 @@ class Resque_RedisCluster extends RedisentCluster // msetnx // mset // 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 - * operations with the 'resque:' key prefix. + * operations with the '{self::$defaultNamespace}' key prefix. * * @param string $name The name of the method called. * @param array $args Array of supplied arguments to the method. @@ -88,7 +105,7 @@ class Resque_RedisCluster extends RedisentCluster public function __call($name, $args) { $args = func_get_args(); if(in_array($name, $this->keyCommands)) { - $args[1][0] = 'resque:' . $args[1][0]; + $args[1][0] = self::$defaultNamespace . $args[1][0]; } try { return parent::__call($name, $args[1]); diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index 1f33fee..3102291 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -100,6 +100,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->assertEquals($job->payload['args'], $newJob->getArguments()); } + public function testFailedJobExceptionsAreCaught() { $payload = array( @@ -166,4 +167,18 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $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); + } } \ No newline at end of file