2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
2011-03-27 07:42:46 +00:00
|
|
|
require_once dirname(__FILE__) . '/Event.php';
|
2010-04-20 00:59:06 +00:00
|
|
|
require_once dirname(__FILE__) . '/Job/Status.php';
|
2011-03-27 07:42:46 +00:00
|
|
|
require_once dirname(__FILE__) . '/Job/DontPerform.php';
|
2010-04-18 13:58:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resque job.
|
|
|
|
*
|
|
|
|
* @package Resque/Job
|
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_Job
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The name of the queue that this job belongs to.
|
|
|
|
*/
|
|
|
|
public $queue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Resque_Worker Instance of the Resque worker running this job.
|
|
|
|
*/
|
|
|
|
public $worker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var object Object containing details of the job.
|
|
|
|
*/
|
|
|
|
public $payload;
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
/**
|
|
|
|
* @var object Instance of the class performing work for this job.
|
|
|
|
*/
|
|
|
|
private $instance;
|
2010-04-18 13:58:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new instance of a job.
|
|
|
|
*
|
|
|
|
* @param string $queue The queue that the job belongs to.
|
2011-12-08 03:15:46 +00:00
|
|
|
* @param array $payload array containing details of the job.
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public function __construct($queue, $payload)
|
|
|
|
{
|
|
|
|
$this->queue = $queue;
|
|
|
|
$this->payload = $payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2010-04-19 00:35:50 +00:00
|
|
|
* @param array $args Any optional arguments that should be passed when the job is executed.
|
2010-04-18 13:58:43 +00:00
|
|
|
* @param boolean $monitor Set to true to be able to monitor the status of a job.
|
2012-03-02 01:21:06 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public static function create($queue, $class, $args = null, $monitor = false)
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
if($args !== null && !is_array($args)) {
|
2010-04-18 13:58:43 +00:00
|
|
|
throw new InvalidArgumentException(
|
2010-04-19 00:35:50 +00:00
|
|
|
'Supplied $args must be an array.'
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$id = md5(uniqid('', true));
|
|
|
|
Resque::push($queue, array(
|
|
|
|
'class' => $class,
|
2011-07-21 03:49:18 +00:00
|
|
|
'args' => array($args),
|
2010-04-18 13:58:43 +00:00
|
|
|
'id' => $id,
|
|
|
|
));
|
|
|
|
|
|
|
|
if($monitor) {
|
|
|
|
Resque_Job_Status::create($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the next available job from the specified queue and return an
|
|
|
|
* instance of Resque_Job for it.
|
|
|
|
*
|
|
|
|
* @param string $queue The name of the queue to check for a job in.
|
|
|
|
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
|
|
|
|
*/
|
|
|
|
public static function reserve($queue)
|
|
|
|
{
|
|
|
|
$payload = Resque::pop($queue);
|
2011-12-08 03:15:46 +00:00
|
|
|
if(!is_array($payload)) {
|
2010-04-18 13:58:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Resque_Job($queue, $payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the status of the current job.
|
|
|
|
*
|
|
|
|
* @param int $status Status constant from Resque_Job_Status indicating the current status of a job.
|
|
|
|
*/
|
|
|
|
public function updateStatus($status)
|
|
|
|
{
|
2010-12-11 05:25:33 +00:00
|
|
|
if(empty($this->payload['id'])) {
|
2010-04-18 13:58:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-19 00:35:50 +00:00
|
|
|
$statusInstance = new Resque_Job_Status($this->payload['id']);
|
2010-04-18 13:58:43 +00:00
|
|
|
$statusInstance->update($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the status of the current job.
|
|
|
|
*
|
|
|
|
* @return int The status of the job as one of the Resque_Job_Status constants.
|
|
|
|
*/
|
|
|
|
public function getStatus()
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$status = new Resque_Job_Status($this->payload['id']);
|
2010-04-18 13:58:43 +00:00
|
|
|
return $status->get();
|
|
|
|
}
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
2011-03-27 07:42:46 +00:00
|
|
|
* Get the arguments supplied to this job.
|
2010-04-18 13:58:43 +00:00
|
|
|
*
|
2011-03-27 07:42:46 +00:00
|
|
|
* @return array Array of arguments.
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
2011-03-27 07:42:46 +00:00
|
|
|
public function getArguments()
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2011-03-27 07:42:46 +00:00
|
|
|
if (!isset($this->payload['args'])) {
|
|
|
|
return array();
|
|
|
|
}
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2011-08-19 00:33:05 +00:00
|
|
|
return $this->payload['args'][0];
|
2011-03-27 07:42:46 +00:00
|
|
|
}
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
/**
|
|
|
|
* Get the instantiated object for this job that will be performing work.
|
|
|
|
*
|
|
|
|
* @return object Instance of the object that this job belongs to.
|
|
|
|
*/
|
|
|
|
public function getInstance()
|
|
|
|
{
|
|
|
|
if (!is_null($this->instance)) {
|
|
|
|
return $this->instance;
|
|
|
|
}
|
|
|
|
|
2010-04-19 00:35:50 +00:00
|
|
|
if(!class_exists($this->payload['class'])) {
|
2010-04-18 13:58:43 +00:00
|
|
|
throw new Resque_Exception(
|
2010-04-19 00:35:50 +00:00
|
|
|
'Could not find job class ' . $this->payload['class'] . '.'
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-04-19 00:35:50 +00:00
|
|
|
if(!method_exists($this->payload['class'], 'perform')) {
|
2010-04-18 13:58:43 +00:00
|
|
|
throw new Resque_Exception(
|
2010-04-19 00:35:50 +00:00
|
|
|
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
|
2010-04-18 13:58:43 +00:00
|
|
|
);
|
|
|
|
}
|
2010-08-01 05:23:41 +00:00
|
|
|
|
2012-03-02 01:21:06 +00:00
|
|
|
$this->instance = new $this->payload['class']();
|
2011-03-27 07:42:46 +00:00
|
|
|
$this->instance->job = $this;
|
|
|
|
$this->instance->args = $this->getArguments();
|
2012-03-22 04:20:16 +00:00
|
|
|
$this->instance->queue = $this->queue;
|
2011-03-27 07:42:46 +00:00
|
|
|
return $this->instance;
|
|
|
|
}
|
2010-08-01 05:23:41 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
/**
|
|
|
|
* Actually execute a job by calling the perform method on the class
|
|
|
|
* associated with the job with the supplied arguments.
|
|
|
|
*
|
2012-03-02 01:21:06 +00:00
|
|
|
* @return bool
|
2011-03-27 07:42:46 +00:00
|
|
|
* @throws Resque_Exception When the job's class could not be found or it does not contain a perform method.
|
|
|
|
*/
|
|
|
|
public function perform()
|
|
|
|
{
|
|
|
|
$instance = $this->getInstance();
|
|
|
|
try {
|
|
|
|
Resque_Event::trigger('beforePerform', $this);
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
if(method_exists($instance, 'setUp')) {
|
|
|
|
$instance->setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
$instance->perform();
|
2010-04-18 13:58:43 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
if(method_exists($instance, 'tearDown')) {
|
|
|
|
$instance->tearDown();
|
|
|
|
}
|
2010-08-01 05:23:41 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
Resque_Event::trigger('afterPerform', $this);
|
|
|
|
}
|
|
|
|
// beforePerform/setUp have said don't perform this job. Return.
|
|
|
|
catch(Resque_Job_DontPerform $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-07 09:14:17 +00:00
|
|
|
|
2011-03-27 07:42:46 +00:00
|
|
|
return true;
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the current job as having failed.
|
2012-03-02 01:21:06 +00:00
|
|
|
*
|
|
|
|
* @param $exception
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public function fail($exception)
|
|
|
|
{
|
2011-03-27 08:18:16 +00:00
|
|
|
Resque_Event::trigger('onFailure', array(
|
|
|
|
'exception' => $exception,
|
|
|
|
'job' => $this,
|
|
|
|
));
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
$this->updateStatus(Resque_Job_Status::STATUS_FAILED);
|
2010-04-20 00:59:06 +00:00
|
|
|
require_once dirname(__FILE__) . '/Failure.php';
|
2010-04-18 13:58:43 +00:00
|
|
|
Resque_Failure::create(
|
|
|
|
$this->payload,
|
|
|
|
$exception,
|
|
|
|
$this->worker,
|
|
|
|
$this->queue
|
|
|
|
);
|
|
|
|
Resque_Stat::incr('failed');
|
|
|
|
Resque_Stat::incr('failed:' . $this->worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-queue the current job.
|
2012-03-02 01:21:06 +00:00
|
|
|
* @return string
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
|
|
|
public function recreate()
|
|
|
|
{
|
2010-04-19 00:35:50 +00:00
|
|
|
$status = new Resque_Job_Status($this->payload['id']);
|
2010-04-18 13:58:43 +00:00
|
|
|
$monitor = false;
|
|
|
|
if($status->isTracking()) {
|
|
|
|
$monitor = true;
|
|
|
|
}
|
|
|
|
|
2012-11-01 13:28:47 +00:00
|
|
|
return self::create($this->queue, $this->payload['class'], $this->payload['args'][0], $monitor);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a string representation used to describe the current job.
|
|
|
|
*
|
|
|
|
* @return string The string representation of the job.
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
$name = array(
|
|
|
|
'Job{' . $this->queue .'}'
|
|
|
|
);
|
2010-04-19 00:35:50 +00:00
|
|
|
if(!empty($this->payload['id'])) {
|
|
|
|
$name[] = 'ID: ' . $this->payload['id'];
|
|
|
|
}
|
|
|
|
$name[] = $this->payload['class'];
|
|
|
|
if(!empty($this->payload['args'])) {
|
|
|
|
$name[] = json_encode($this->payload['args']);
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
return '(' . implode(' | ', $name) . ')';
|
|
|
|
}
|
|
|
|
}
|
2011-07-21 03:49:18 +00:00
|
|
|
?>
|