Skip to content

Commit f87b002

Browse files
committed
Apply old PR 134
Closes chrisboulton/php-resque#134
1 parent 429d07c commit f87b002

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lib/Resque/Job.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ public static function reserveBlocking(array $queues, $timeout = null)
123123
*
124124
* @param int $status Status constant from Resque_Job_Status indicating the current status of a job.
125125
*/
126-
public function updateStatus($status)
126+
public function updateStatus($status, $result = null)
127127
{
128128
if(empty($this->payload['id'])) {
129129
return;
130130
}
131131

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

136136
/**
@@ -183,6 +183,7 @@ public function getInstance()
183183
*/
184184
public function perform()
185185
{
186+
$result = true;
186187
try {
187188
Resque_Event::trigger('beforePerform', $this);
188189

@@ -191,7 +192,7 @@ public function perform()
191192
$instance->setUp();
192193
}
193194

194-
$instance->perform();
195+
$result = $instance->perform();
195196

196197
if(method_exists($instance, 'tearDown')) {
197198
$instance->tearDown();
@@ -201,10 +202,10 @@ public function perform()
201202
}
202203
// beforePerform/setUp have said don't perform this job. Return.
203204
catch(Resque_Job_DontPerform $e) {
204-
return false;
205+
$result = false;
205206
}
206207

207-
return true;
208+
return $result;
208209
}
209210

210211
/**

lib/Resque/Job/Status.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function isTracking()
8484
*
8585
* @param int The status of the job (see constants in Resque_Job_Status)
8686
*/
87-
public function update($status)
87+
public function update($status, $result = null)
8888
{
8989
if(!$this->isTracking()) {
9090
return;
@@ -93,6 +93,7 @@ public function update($status)
9393
$statusPacket = array(
9494
'status' => $status,
9595
'updated' => time(),
96+
'result' => $result,
9697
);
9798
Resque::redis()->set((string)$this, json_encode($statusPacket));
9899

@@ -105,7 +106,7 @@ public function update($status)
105106
/**
106107
* Fetch the status for the job being monitored.
107108
*
108-
* @return mixed False if the status is not being monitored, otherwise the status as
109+
* @return mixed False if the status is not being monitored, otherwise the status
109110
* as an integer, based on the Resque_Job_Status constants.
110111
*/
111112
public function get()
@@ -122,6 +123,40 @@ public function get()
122123
return $statusPacket['status'];
123124
}
124125

126+
/**
127+
* Fetch the result of the job being monitored.
128+
*
129+
* @return mixed False if the job is not being monitored, otherwise the result
130+
* as mixed
131+
*/
132+
public function getResult()
133+
{
134+
if(!$this->isTracking()) {
135+
return false;
136+
}
137+
138+
$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
139+
if(!$statusPacket) {
140+
return false;
141+
}
142+
143+
return $statusPacket['result'];
144+
}
145+
146+
/**
147+
* Delete the job monitoring from the queue
148+
*
149+
* @return boolean|int
150+
*/
151+
public function del()
152+
{
153+
if(!$this->isTracking()) {
154+
return false;
155+
}
156+
157+
return Resque::redis()->del((string)$this);
158+
}
159+
125160
/**
126161
* Stop tracking the status of a job.
127162
*/

lib/Resque/Worker.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,18 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
250250
*/
251251
public function perform(Resque_Job $job)
252252
{
253+
$result = null;
253254
try {
254255
Resque_Event::trigger('afterFork', $job);
255-
$job->perform();
256+
$result = $job->perform();
256257
}
257258
catch(Exception $e) {
258259
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e));
259260
$job->fail($e);
260261
return;
261262
}
262263

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

0 commit comments

Comments
 (0)