1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resque; |
4
|
|
|
|
5
|
|
|
use Resque\Worker\ResqueWorker; |
6
|
|
|
use Exception; |
7
|
|
|
use Error; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Failed Resque job. |
11
|
|
|
* |
12
|
|
|
* @package Resque/FailureHandler |
13
|
|
|
* @author Chris Boulton <[email protected]> |
14
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php |
15
|
|
|
*/ |
16
|
|
|
class FailureHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string Class name representing the backend to pass failed jobs off to. |
20
|
|
|
*/ |
21
|
|
|
private static $backend; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new failed job on the backend. |
25
|
|
|
* |
26
|
|
|
* @param object $payload The contents of the job that has just failed. |
27
|
|
|
* @param \Exception $exception The exception generated when the job failed to run. |
28
|
|
|
* @param \Resque\Worker\ResqueWorker $worker Instance of Resque\Worker\ResqueWorker that was running this job when it failed. |
29
|
|
|
* @param string $queue The name of the queue that this job was fetched from. |
30
|
|
|
*/ |
31
|
|
|
public static function create($payload, Exception $exception, ResqueWorker $worker, $queue) |
32
|
|
|
{ |
33
|
|
|
$backend = self::getBackend(); |
34
|
|
|
new $backend($payload, $exception, $worker, $queue); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new failed job on the backend from PHP 7 errors. |
39
|
|
|
* |
40
|
|
|
* @param object $payload The contents of the job that has just failed. |
41
|
|
|
* @param \Error $exception The PHP 7 error generated when the job failed to run. |
42
|
|
|
* @param \Resque\Worker\ResqueWorker $worker Instance of Resque\Worker\ResqueWorker that was running this job when it failed. |
43
|
|
|
* @param string $queue The name of the queue that this job was fetched from. |
44
|
|
|
*/ |
45
|
|
|
public static function createFromError($payload, Error $exception, ResqueWorker $worker, $queue) |
46
|
|
|
{ |
47
|
|
|
$backend = self::getBackend(); |
48
|
|
|
new $backend($payload, $exception, $worker, $queue); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Return an instance of the backend for saving job failures. |
53
|
|
|
* |
54
|
|
|
* @return object Instance of backend object. |
55
|
|
|
*/ |
56
|
|
|
public static function getBackend() |
57
|
|
|
{ |
58
|
|
|
if (self::$backend === null) { |
|
|
|
|
59
|
|
|
self::$backend = 'Resque\Failure\RedisFailure'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return self::$backend; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the backend to use for raised job failures. The supplied backend |
67
|
|
|
* should be the name of a class to be instantiated when a job fails. |
68
|
|
|
* It is your responsibility to have the backend class loaded (or autoloaded) |
69
|
|
|
* |
70
|
|
|
* @param string $backend The class name of the backend to pipe failures to. |
71
|
|
|
*/ |
72
|
|
|
public static function setBackend($backend) |
73
|
|
|
{ |
74
|
|
|
self::$backend = $backend; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|