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 |
29
|
|
|
* that was running this job when it failed. |
30
|
|
|
* @param string $queue The name of the queue that this job was fetched from. |
31
|
|
|
*/ |
32
|
|
|
public static function create($payload, Exception $exception, ResqueWorker $worker, $queue) |
33
|
|
|
{ |
34
|
|
|
$backend = self::getBackend(); |
35
|
|
|
new $backend($payload, $exception, $worker, $queue); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new failed job on the backend from PHP 7 errors. |
40
|
|
|
* |
41
|
|
|
* @param object $payload The contents of the job that has just failed. |
42
|
|
|
* @param \Error $exception The PHP 7 error generated when the job failed to run. |
43
|
|
|
* @param \Resque\Worker\ResqueWorker $worker Instance of Resque\Worker\ResqueWorker |
44
|
|
|
* that was running this job when it failed. |
45
|
|
|
* @param string $queue The name of the queue that this job was fetched from. |
46
|
|
|
*/ |
47
|
|
|
public static function createFromError($payload, Error $exception, ResqueWorker $worker, $queue) |
48
|
|
|
{ |
49
|
|
|
$backend = self::getBackend(); |
50
|
|
|
new $backend($payload, $exception, $worker, $queue); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return an instance of the backend for saving job failures. |
55
|
|
|
* |
56
|
|
|
* @return object Instance of backend object. |
57
|
|
|
*/ |
58
|
|
|
public static function getBackend() |
59
|
|
|
{ |
60
|
|
|
if (self::$backend === null) { |
|
|
|
|
61
|
|
|
self::$backend = 'Resque\Failure\RedisFailure'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return self::$backend; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the backend to use for raised job failures. The supplied backend |
69
|
|
|
* should be the name of a class to be instantiated when a job fails. |
70
|
|
|
* It is your responsibility to have the backend class loaded (or autoloaded) |
71
|
|
|
* |
72
|
|
|
* @param string $backend The class name of the backend to pipe failures to. |
73
|
|
|
*/ |
74
|
|
|
public static function setBackend($backend) |
75
|
|
|
{ |
76
|
|
|
self::$backend = $backend; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|