2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base Resque class.
|
|
|
|
*
|
|
|
|
* @package Resque
|
2012-10-13 06:55:41 +00:00
|
|
|
* @author Chris Boulton <chris@bigcommerce.com>
|
2010-04-18 13:58:43 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
class Resque
|
|
|
|
{
|
2012-10-13 07:28:16 +00:00
|
|
|
const VERSION = '1.2';
|
2010-04-18 13:58:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Resque_Redis Instance of Resque_Redis that talks to redis.
|
|
|
|
*/
|
|
|
|
public static $redis = null;
|
|
|
|
|
2012-03-05 08:21:43 +00:00
|
|
|
/**
|
|
|
|
* @var mixed Host/port conbination separated by a colon, or a nested
|
|
|
|
* array of server swith host/port pairs
|
|
|
|
*/
|
|
|
|
protected static $redisServer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int ID of Redis database to select.
|
|
|
|
*/
|
|
|
|
protected static $redisDatabase = 0;
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
|
|
|
* Given a host/port combination separated by a colon, set it as
|
|
|
|
* the redis server that Resque will talk to.
|
|
|
|
*
|
2010-08-01 05:03:28 +00:00
|
|
|
* @param mixed $server Host/port combination separated by a colon, or
|
2012-03-01 04:32:52 +00:00
|
|
|
* a nested array of servers with host/port pairs.
|
|
|
|
* @param int $database
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
2011-01-16 17:15:31 +00:00
|
|
|
public static function setBackend($server, $database = 0)
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2012-03-05 08:21:43 +00:00
|
|
|
self::$redisServer = $server;
|
|
|
|
self::$redisDatabase = $database;
|
|
|
|
self::$redis = null;
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the Resque_Redis class instantiated for Resque.
|
|
|
|
*
|
|
|
|
* @return Resque_Redis Instance of Resque_Redis.
|
|
|
|
*/
|
|
|
|
public static function redis()
|
|
|
|
{
|
2013-01-12 15:59:06 +00:00
|
|
|
if (self::$redis !== null) {
|
2012-03-05 08:21:43 +00:00
|
|
|
return self::$redis;
|
|
|
|
}
|
|
|
|
|
|
|
|
$server = self::$redisServer;
|
|
|
|
if (empty($server)) {
|
|
|
|
$server = 'localhost:6379';
|
|
|
|
}
|
|
|
|
|
2013-01-12 12:37:38 +00:00
|
|
|
self::$redis = new Resque_Redis($server, self::$redisDatabase);
|
2010-04-18 13:58:43 +00:00
|
|
|
return self::$redis;
|
|
|
|
}
|
|
|
|
|
2013-01-12 15:59:06 +00:00
|
|
|
/**
|
|
|
|
* fork() helper method for php-resque that handles issues PHP socket
|
|
|
|
* and phpredis have with passing around sockets between child/parent
|
|
|
|
* processes.
|
|
|
|
*
|
|
|
|
* Will close connection to Redis before forking.
|
|
|
|
*
|
|
|
|
* @return int Return vars as per pcntl_fork()
|
|
|
|
*/
|
|
|
|
public static function fork()
|
|
|
|
{
|
|
|
|
if(!function_exists('pcntl_fork')) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the connection to Redis before forking.
|
|
|
|
// This is a workaround for issues phpredis has.
|
|
|
|
self::$redis = null;
|
|
|
|
|
|
|
|
$pid = pcntl_fork();
|
|
|
|
if($pid === -1) {
|
|
|
|
throw new RuntimeException('Unable to fork child worker.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
|
|
|
* Push a job to the end of a specific queue. If the queue does not
|
|
|
|
* exist, then create it as well.
|
|
|
|
*
|
|
|
|
* @param string $queue The name of the queue to add the job to.
|
2011-12-16 02:30:42 +00:00
|
|
|
* @param array $item Job description as an array to be JSON encoded.
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public static function push($queue, $item)
|
|
|
|
{
|
|
|
|
self::redis()->sadd('queues', $queue);
|
|
|
|
self::redis()->rpush('queue:' . $queue, json_encode($item));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pop an item off the end of the specified queue, decode it and
|
|
|
|
* return it.
|
|
|
|
*
|
|
|
|
* @param string $queue The name of the queue to fetch an item from.
|
2011-12-08 03:15:46 +00:00
|
|
|
* @return array Decoded item from the queue.
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public static function pop($queue)
|
|
|
|
{
|
|
|
|
$item = self::redis()->lpop('queue:' . $queue);
|
|
|
|
if(!$item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-19 00:35:50 +00:00
|
|
|
return json_decode($item, true);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size (number of pending jobs) of the specified queue.
|
|
|
|
*
|
2012-03-16 01:03:34 +00:00
|
|
|
* @param $queue name of the queue to be checked for pending jobs
|
2012-03-01 04:32:52 +00:00
|
|
|
*
|
2010-04-18 13:58:43 +00:00
|
|
|
* @return int The size of the queue.
|
|
|
|
*/
|
|
|
|
public static function size($queue)
|
|
|
|
{
|
|
|
|
return self::redis()->llen('queue:' . $queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job and save it to the specified queue.
|
|
|
|
*
|
|
|
|
* @param string $queue The name of the queue to place the job in.
|
|
|
|
* @param string $class The name of the class that contains the code to execute the job.
|
|
|
|
* @param array $args Any optional arguments that should be passed when the job is executed.
|
2012-03-01 04:32:52 +00:00
|
|
|
* @param boolean $trackStatus Set to true to be able to monitor the status of a job.
|
|
|
|
*
|
|
|
|
* @return string
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public static function enqueue($queue, $class, $args = null, $trackStatus = false)
|
|
|
|
{
|
2011-03-27 07:42:46 +00:00
|
|
|
$result = Resque_Job::create($queue, $class, $args, $trackStatus);
|
|
|
|
if ($result) {
|
|
|
|
Resque_Event::trigger('afterEnqueue', array(
|
|
|
|
'class' => $class,
|
2012-05-10 12:13:28 +00:00
|
|
|
'args' => $args,
|
|
|
|
'queue' => $queue,
|
2011-03-27 07:42:46 +00:00
|
|
|
));
|
|
|
|
}
|
2011-12-08 03:15:46 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
return $result;
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserve and return the next available job in the specified queue.
|
|
|
|
*
|
|
|
|
* @param string $queue Queue to fetch next available job from.
|
|
|
|
* @return Resque_Job Instance of Resque_Job to be processed, false if none or error.
|
|
|
|
*/
|
|
|
|
public static function reserve($queue)
|
|
|
|
{
|
|
|
|
return Resque_Job::reserve($queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of all known queues.
|
|
|
|
*
|
|
|
|
* @return array Array of queues.
|
|
|
|
*/
|
|
|
|
public static function queues()
|
|
|
|
{
|
|
|
|
$queues = self::redis()->smembers('queues');
|
|
|
|
if(!is_array($queues)) {
|
|
|
|
$queues = array();
|
|
|
|
}
|
|
|
|
return $queues;
|
|
|
|
}
|
2010-05-27 20:26:37 +00:00
|
|
|
}
|