1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueueableAction; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Bus\Queueable; |
7
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
8
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
9
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
10
|
|
|
use Illuminate\Queue\SerializesModels; |
11
|
|
|
use PDO; |
12
|
|
|
|
13
|
|
|
class ActionJob implements ShouldQueue |
14
|
|
|
{ |
15
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $actionClass; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $parameters; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $tags = ['action_job']; |
25
|
|
|
|
26
|
|
|
/** @var callable */ |
27
|
|
|
protected $onFailCallback; |
28
|
|
|
|
29
|
|
|
public function __construct($action, array $parameters = []) |
30
|
|
|
{ |
31
|
|
|
$this->actionClass = is_string($action) ? $action : get_class($action); |
32
|
|
|
$this->parameters = $parameters; |
33
|
|
|
|
34
|
|
|
if (is_object($action)) { |
35
|
|
|
$this->tags = $action->tags(); |
36
|
|
|
|
37
|
|
|
if (method_exists($action, 'failed')) { |
38
|
|
|
$this->onFailCallback = [$action, 'failed']; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->resolveQueueableProperties($action); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function displayName(): string |
46
|
|
|
{ |
47
|
|
|
return $this->actionClass; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function tags() |
51
|
|
|
{ |
52
|
|
|
return $this->tags; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function failed(Exception $exception) |
56
|
|
|
{ |
57
|
|
|
if ($this->onFailCallback) { |
58
|
|
|
return ($this->onFailCallback)($exception); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function handle() |
63
|
|
|
{ |
64
|
|
|
$action = app($this->actionClass); |
65
|
|
|
|
66
|
|
|
$action->execute(...$this->parameters); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function resolveQueueableProperties($action) |
70
|
|
|
{ |
71
|
|
|
$queueableProperties = [ |
72
|
|
|
'connection', |
73
|
|
|
'queue', |
74
|
|
|
'chainConnection', |
75
|
|
|
'chainQueue', |
76
|
|
|
'delay', |
77
|
|
|
'chained', |
78
|
|
|
'tries', |
79
|
|
|
'timeout', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
foreach ($queueableProperties as $queueableProperty) { |
83
|
|
|
if (property_exists($action, $queueableProperty)) { |
84
|
|
|
$this->{$queueableProperty} = $action->{$queueableProperty}; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|