Merge pull request #63 from ruudk/blocking-list-pop

Basic support for blocking list pop
This commit is contained in:
Chris Boulton 2013-06-12 01:09:11 -07:00
commit 59f617e639
16 changed files with 704 additions and 185 deletions

View file

@ -10,6 +10,8 @@ class Resque
{
const VERSION = '1.2';
const DEFAULT_INTERVAL = 5;
/**
* @var Resque_Redis Instance of Resque_Redis that talks to redis.
*/
@ -60,7 +62,7 @@ class Resque
self::$redis = new Resque_Redis($server, self::$redisDatabase);
return self::$redis;
}
/**
* fork() helper method for php-resque that handles issues PHP socket
* and phpredis have with passing around sockets between child/parent
@ -114,7 +116,8 @@ class Resque
*/
public static function pop($queue)
{
$item = self::redis()->lpop('queue:' . $queue);
$item = self::redis()->lpop('queue:' . $queue);
if(!$item) {
return;
}
@ -122,6 +125,40 @@ class Resque
return json_decode($item, true);
}
/**
* Pop an item off the end of the specified queues, using blocking list pop,
* decode it and return it.
*
* @param array $queues
* @param int $timeout
* @return null|array Decoded item from the queue.
*/
public static function blpop(array $queues, $timeout)
{
$list = array();
foreach($queues AS $queue) {
$list[] = 'queue:' . $queue;
}
$item = self::redis()->blpop($list, (int)$timeout);
if(!$item) {
return;
}
/**
* Normally the Resque_Redis class returns queue names without the prefix
* But the blpop is a bit different. It returns the name as prefix:queue:name
* So we need to strip off the prefix:queue: part
*/
$queue = substr($item[0], strlen(self::redis()->getPrefix() . 'queue:'));
return array(
'queue' => $queue,
'payload' => json_decode($item[1], true)
);
}
/**
* Return the size (number of pending jobs) of the specified queue.
*