diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index ddc37d04..722a66e0 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -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); } /** @@ -186,6 +186,7 @@ public function getInstance() */ public function perform() { + $result = true; $instance = $this->getInstance(); try { Resque_Event::trigger('beforePerform', $this); @@ -194,7 +195,7 @@ public function perform() $instance->setUp(); } - $instance->perform(); + $result = $instance->perform(); if(method_exists($instance, 'tearDown')) { $instance->tearDown(); @@ -207,7 +208,7 @@ public function perform() return false; } - return true; + return $result; } /** diff --git a/lib/Resque/Job/Status.php b/lib/Resque/Job/Status.php index ffa351ba..4fc54551 100644 --- a/lib/Resque/Job/Status.php +++ b/lib/Resque/Job/Status.php @@ -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; @@ -93,6 +93,7 @@ public function update($status) $statusPacket = array( 'status' => $status, 'updated' => time(), + 'result' => $result ); Resque::redis()->set((string)$this, json_encode($statusPacket)); @@ -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. */ diff --git a/lib/Resque/Worker.php b/lib/Resque/Worker.php index d94aef54..88d099c9 100644 --- a/lib/Resque/Worker.php +++ b/lib/Resque/Worker.php @@ -236,9 +236,10 @@ 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())); @@ -246,7 +247,7 @@ public function perform(Resque_Job $job) 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)); }