1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Resque; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Resque\Dispatchers\Noop; |
9
|
|
|
use Resque\Exceptions\PayloadCorrupt; |
10
|
|
|
use Resque\Interfaces\Dispatcher; |
11
|
|
|
use Resque\Tasks\AfterUserJobPerform; |
12
|
|
|
use Resque\Tasks\BeforeUserJobPerform; |
13
|
|
|
use Resque\Tasks\FailedUserJobPerform; |
14
|
|
|
|
15
|
|
|
class Job |
16
|
|
|
{ |
17
|
|
|
private $queueName; |
18
|
|
|
private $payload; |
19
|
|
|
private $serviceLocator; |
20
|
|
|
private $dispatcher; |
21
|
|
|
private $failed = false; |
22
|
|
|
|
23
|
6 |
|
public function __construct(string $queueName, array $payload, ContainerInterface $serviceLocator) |
24
|
|
|
{ |
25
|
6 |
|
$this->queueName = $queueName; |
26
|
6 |
|
$this->payload = $payload; |
27
|
6 |
|
$this->serviceLocator = $serviceLocator; |
28
|
6 |
|
$this->dispatcher = new Noop(); |
29
|
6 |
|
} |
30
|
|
|
|
31
|
6 |
|
public function setDispatcher(Dispatcher $dispatcher): void |
32
|
|
|
{ |
33
|
6 |
|
$this->dispatcher = $dispatcher; |
34
|
6 |
|
} |
35
|
|
|
|
36
|
2 |
|
public function getPayloadClassName(): string |
37
|
|
|
{ |
38
|
2 |
|
if (empty($this->payload['class'])) { |
39
|
|
|
throw new PayloadCorrupt(); |
40
|
|
|
} |
41
|
2 |
|
return $this->payload['class']; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function getPayloadArguments(): array |
45
|
|
|
{ |
46
|
2 |
|
if (empty($this->payload['args'])) { |
47
|
|
|
throw new PayloadCorrupt(); |
48
|
|
|
} |
49
|
2 |
|
return $this->payload['args']; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
public function getQueueName(): string |
53
|
|
|
{ |
54
|
6 |
|
return $this->queueName; |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
public function getPayload(): array |
58
|
|
|
{ |
59
|
6 |
|
return $this->payload; |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
public function hasFailed(): bool |
63
|
|
|
{ |
64
|
5 |
|
return $this->failed; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function perform(): void |
68
|
|
|
{ |
69
|
2 |
|
$this->dispatcher->dispatch(BeforeUserJobPerform::class, $this->payload); |
70
|
|
|
try { |
71
|
2 |
|
$userJob = $this->serviceLocator->get($this->getPayloadClassName()); |
72
|
2 |
|
$userJob->perform($this->getPayloadArguments()); |
73
|
1 |
|
$this->dispatcher->dispatch(AfterUserJobPerform::class, $this->payload); |
74
|
1 |
|
} catch (\Exception $e) { |
75
|
1 |
|
$this->handleFailedJob(); |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
1 |
|
private function handleFailedJob() |
80
|
|
|
{ |
81
|
1 |
|
$this->failed = true; |
82
|
1 |
|
$this->dispatcher->dispatch(FailedUserJobPerform::class, $this->payload); |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|