Skip to content
Browse files

Merge pull request #27 from hlegius/master

Namespace support (prefix)
  • Loading branch information...
2 parents 4c02511 + 016a7a1 commit 45c49cf0023df033324082312f14063cd5ab81db @chrisboulton chrisboulton committed Nov 23, 2011
Showing with 54 additions and 5 deletions.
  1. +20 −3 lib/Resque/Redis.php
  2. +19 −2 lib/Resque/RedisCluster.php
  3. +15 −0 test/Resque/Tests/JobTest.php
View
23 lib/Resque/Redis.php
@@ -16,6 +16,11 @@
*/
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]);
View
21 lib/Resque/RedisCluster.php
@@ -16,6 +16,11 @@
*/
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]);
View
15 test/Resque/Tests/JobTest.php
@@ -100,6 +100,7 @@ public function testRecreatedJobMatchesExistingJob()
$this->assertEquals($job->payload['args'], $newJob->getArguments());
}
+
public function testFailedJobExceptionsAreCaught()
{
$payload = array(
@@ -166,4 +167,18 @@ public function testJobWithTearDownCallbackFiresTearDown()
$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);
+ }
}

0 comments on commit 45c49cf

Please sign in to comment.
Something went wrong with that request. Please try again.