Skip to content

Commit f993b9f

Browse files
author
Jared King
committed
Catch PHP 7 errors in Resque_Worker::perform() and adhere to PSR-3 when logging exceptions
1 parent cf187fa commit f993b9f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

lib/Resque/Failure.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public static function create($payload, Exception $exception, Resque_Worker $wor
2828
new $backend($payload, $exception, $worker, $queue);
2929
}
3030

31+
/**
32+
* Create a new failed job on the backend from PHP 7 errors.
33+
*
34+
* @param object $payload The contents of the job that has just failed.
35+
* @param \Error $exception The PHP 7 error generated when the job failed to run.
36+
* @param \Resque_Worker $worker Instance of Resque_Worker that was running this job when it failed.
37+
* @param string $queue The name of the queue that this job was fetched from.
38+
*/
39+
public static function createFromError($payload, Error $exception, Resque_Worker $worker, $queue)
40+
{
41+
$backend = self::getBackend();
42+
new $backend($payload, $exception, $worker, $queue);
43+
}
44+
3145
/**
3246
* Return an instance of the backend for saving job failures.
3347
*

lib/Resque/Job.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,21 @@ public function fail($exception)
220220
));
221221

222222
$this->updateStatus(Resque_Job_Status::STATUS_FAILED);
223-
Resque_Failure::create(
224-
$this->payload,
225-
$exception,
226-
$this->worker,
227-
$this->queue
228-
);
223+
if ($exception instanceof Error) {
224+
Resque_Failure::createFromError(
225+
$this->payload,
226+
$exception,
227+
$this->worker,
228+
$this->queue
229+
);
230+
} else {
231+
Resque_Failure::create(
232+
$this->payload,
233+
$exception,
234+
$this->worker,
235+
$this->queue
236+
);
237+
}
229238
Resque_Stat::incr('failed');
230239
Resque_Stat::incr('failed:' . $this->worker);
231240
}

lib/Resque/Worker.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,12 @@ public function perform(Resque_Job $job)
240240
$job->perform();
241241
}
242242
catch(Exception $e) {
243-
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {stack}', array('job' => $job, 'stack' => $e));
243+
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {exception}', array('job' => $job, 'exception' => $e));
244+
$job->fail($e);
245+
return;
246+
}
247+
catch(Error $e) {
248+
$this->logger->log(Psr\Log\LogLevel::CRITICAL, '{job} has failed {exception}', array('job' => $job, 'exception' => $e));
244249
$job->fail($e);
245250
return;
246251
}

0 commit comments

Comments
 (0)