Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeping track of Process status as well as result #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/Resque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,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 = "")
{
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 @@ -186,6 +186,7 @@ public function getInstance()
*/
public function perform()
{
$result = true;
$instance = $this->getInstance();
try {
Resque_Event::trigger('beforePerform', $this);
Expand All @@ -194,7 +195,7 @@ public function perform()
$instance->setUp();
}

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

if(method_exists($instance, 'tearDown')) {
$instance->tearDown();
Expand All @@ -207,7 +208,7 @@ public function perform()
return false;
}

return true;
return $result;
}

/**
Expand Down
37 changes: 36 additions & 1 deletion 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 = "")
{
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 Down Expand Up @@ -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
* 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 @@ -236,17 +236,18 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
*/
public function perform(Resque_Job $job)
{
$result = "";
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->getMessage()));
$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