Skip to content

Commit

Permalink
Apply old PR 134
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Dec 11, 2018
1 parent 429d07c commit f87b002
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/Resque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ public static function reserveBlocking(array $queues, $timeout = null)
*
* @param int $status Status constant from Resque_Job_Status indicating the current status of a job.
*/
public function updateStatus($status)
public function updateStatus($status, $result = null)
{
if(empty($this->payload['id'])) {
return;
}

$statusInstance = new Resque_Job_Status($this->payload['id']);
$statusInstance->update($status);
$statusInstance->update($status, $result);
}

/**
Expand Down Expand Up @@ -183,6 +183,7 @@ public function getInstance()
*/
public function perform()
{
$result = true;
try {
Resque_Event::trigger('beforePerform', $this);

Expand All @@ -191,7 +192,7 @@ public function perform()
$instance->setUp();
}

$instance->perform();
$result = $instance->perform();

if(method_exists($instance, 'tearDown')) {
$instance->tearDown();
Expand All @@ -201,10 +202,10 @@ public function perform()
}
// beforePerform/setUp have said don't perform this job. Return.
catch(Resque_Job_DontPerform $e) {
return false;
$result = false;
}

return true;
return $result;
}

/**
Expand Down
39 changes: 37 additions & 2 deletions lib/Resque/Job/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function isTracking()
*
* @param int The status of the job (see constants in Resque_Job_Status)
*/
public function update($status)
public function update($status, $result = null)
{
if(!$this->isTracking()) {
return;
Expand All @@ -93,6 +93,7 @@ public function update($status)
$statusPacket = array(
'status' => $status,
'updated' => time(),
'result' => $result,
);
Resque::redis()->set((string)$this, json_encode($statusPacket));

Expand All @@ -105,7 +106,7 @@ public function update($status)
/**
* Fetch the status for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the status as
* @return mixed False if the status is not being monitored, otherwise the status
* as an integer, based on the Resque_Job_Status constants.
*/
public function get()
Expand All @@ -122,6 +123,40 @@ public function get()
return $statusPacket['status'];
}

/**
* Fetch the result of the job being monitored.
*
* @return mixed False if the job is not being monitored, otherwise the result
* as mixed
*/
public function getResult()
{
if(!$this->isTracking()) {
return false;
}

$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
if(!$statusPacket) {
return false;
}

return $statusPacket['result'];
}

/**
* Delete the job monitoring from the queue
*
* @return boolean|int
*/
public function del()
{
if(!$this->isTracking()) {
return false;
}

return Resque::redis()->del((string)$this);
}

/**
* Stop tracking the status of a job.
*/
Expand Down
5 changes: 3 additions & 2 deletions lib/Resque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,18 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
*/
public function perform(Resque_Job $job)
{
$result = null;
try {
Resque_Event::trigger('afterFork', $job);
$job->perform();
$result = $job->perform();
}
catch(Exception $e) {
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e));
$job->fail($e);
return;
}

$job->updateStatus(Resque_Job_Status::STATUS_COMPLETE);
$job->updateStatus(Resque_Job_Status::STATUS_COMPLETE, $result);
$this->logger->log(Psr\Log\LogLevel::NOTICE, '{job} has finished', array('job' => $job));
}

Expand Down

0 comments on commit f87b002

Please sign in to comment.