2
require_once dirname(__FILE__) . '/Resque/Event.php';
3
require_once dirname(__FILE__) . '/Resque/Exception.php';
9
* @author Chris Boulton <chris.boulton@interspire.com>
10
* @copyright (c) 2010 Chris Boulton
11
* @license http://www.opensource.org/licenses/mit-license.php
15
const VERSION = '1.0';
18
* @var Resque_Redis Instance of Resque_Redis that talks to redis.
20
public static $redis = null;
23
* Given a host/port combination separated by a colon, set it as
24
* the redis server that Resque will talk to.
26
* @param mixed $server Host/port combination separated by a colon, or
27
* a nested array of servers with host/port pairs.
29
public static function setBackend($server, $database = 0)
31
if(is_array($server)) {
32
require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
33
self::$redis = new Resque_RedisCluster($server);
36
list($host, $port) = explode(':', $server);
37
require_once dirname(__FILE__) . '/Resque/Redis.php';
38
self::$redis = new Resque_Redis($host, $port);
41
self::redis()->select($database);
45
* Return an instance of the Resque_Redis class instantiated for Resque.
47
* @return Resque_Redis Instance of Resque_Redis.
49
public static function redis()
51
if(is_null(self::$redis)) {
52
self::setBackend('localhost:6379');
59
* Push a job to the end of a specific queue. If the queue does not
60
* exist, then create it as well.
62
* @param string $queue The name of the queue to add the job to.
63
* @param array $item Job description as an array to be JSON encoded.
65
public static function push($queue, $item)
67
self::redis()->sadd('queues', $queue);
68
self::redis()->rpush('queue:' . $queue, json_encode($item));
72
* Pop an item off the end of the specified queue, decode it and
75
* @param string $queue The name of the queue to fetch an item from.
76
* @return array Decoded item from the queue.
78
public static function pop($queue)
80
$item = self::redis()->lpop('queue:' . $queue);
85
return json_decode($item, true);
89
* Return the size (number of pending jobs) of the specified queue.
91
* @return int The size of the queue.
93
public static function size($queue)
95
return self::redis()->llen('queue:' . $queue);
99
* Create a new job and save it to the specified queue.
101
* @param string $queue The name of the queue to place the job in.
102
* @param string $class The name of the class that contains the code to execute the job.
103
* @param array $args Any optional arguments that should be passed when the job is executed.
104
* @param boolean $monitor Set to true to be able to monitor the status of a job.
106
public static function enqueue($queue, $class, $args = null, $trackStatus = false)
108
require_once dirname(__FILE__) . '/Resque/Job.php';
109
$result = Resque_Job::create($queue, $class, $args, $trackStatus);
111
Resque_Event::trigger('afterEnqueue', array(
121
* Reserve and return the next available job in the specified queue.
123
* @param string $queue Queue to fetch next available job from.
124
* @return Resque_Job Instance of Resque_Job to be processed, false if none or error.
126
public static function reserve($queue)
128
require_once dirname(__FILE__) . '/Resque/Job.php';
129
return Resque_Job::reserve($queue);
133
* Get an array of all known queues.
135
* @return array Array of queues.
137
public static function queues()
139
$queues = self::redis()->smembers('queues');
140
if(!is_array($queues)) {