1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JobQueue\Domain\Task; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
|
10
|
|
|
final class TaskHandler implements EventSubscriberInterface, LoggerAwareInterface |
11
|
|
|
{ |
12
|
|
|
use LoggerAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @var Queue |
17
|
|
|
*/ |
18
|
|
|
private $queue; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var EventDispatcher |
23
|
|
|
*/ |
24
|
|
|
private $dispatcher; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @param Queue $queue |
29
|
|
|
* @param EventDispatcher $dispatcher |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct(Queue $queue, EventDispatcher $dispatcher) |
32
|
|
|
{ |
33
|
4 |
|
$this->queue = $queue; |
34
|
4 |
|
$this->dispatcher = $dispatcher; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
4 |
|
public static function getSubscribedEvents(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
4 |
|
TaskWasFetched::NAME => [ |
45
|
|
|
['onTaskFetchedConfigureJob', 100], |
46
|
|
|
['onTaskFetchedExecuteJob', -100], |
47
|
|
|
], |
48
|
4 |
|
TaskWasExecuted::NAME => 'onTaskExecuted', |
49
|
4 |
|
TaskHasFailed::NAME => 'onTaskFailed', |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @param TaskWasFetched $event |
56
|
|
|
*/ |
57
|
4 |
|
public function onTaskFetchedConfigureJob(TaskWasFetched $event) |
58
|
|
|
{ |
59
|
4 |
|
$task = $event->getTask(); |
60
|
4 |
|
$job = $task->getJob(); |
61
|
|
|
|
62
|
|
|
// Configure the job |
63
|
4 |
|
if ($this->logger) { |
64
|
|
|
$job->setLogger($this->logger); |
65
|
|
|
} |
66
|
4 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @param TaskWasFetched $event |
71
|
|
|
*/ |
72
|
4 |
|
public function onTaskFetchedExecuteJob(TaskWasFetched $event) |
73
|
|
|
{ |
74
|
4 |
|
$task = $event->getTask(); |
75
|
4 |
|
$job = $task->getJob(); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
// Execute the job |
79
|
4 |
|
$job->setUp($task); |
80
|
4 |
|
$job->perform($task); |
81
|
2 |
|
$job->tearDown($task); |
82
|
|
|
|
83
|
2 |
|
$this->dispatcher->dispatch(TaskWasExecuted::NAME, new TaskWasExecuted($task)); |
84
|
|
|
|
85
|
2 |
|
} catch (\Exception $e) { |
86
|
2 |
|
$this->dispatcher->dispatch(TaskHasFailed::NAME, new TaskHasFailed($task)); |
87
|
|
|
} |
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @param TaskWasExecuted $event |
93
|
|
|
*/ |
94
|
2 |
|
public function onTaskExecuted(TaskWasExecuted $event) |
95
|
|
|
{ |
96
|
2 |
|
$this->queue->updateStatus($event->getTask(), new Status(Status::FINISHED)); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* |
101
|
|
|
* @param TaskHasFailed $event |
102
|
|
|
*/ |
103
|
2 |
|
public function onTaskFailed(TaskHasFailed $event) |
104
|
|
|
{ |
105
|
2 |
|
$this->queue->updateStatus($event->getTask(), new Status(Status::FAILED)); |
106
|
2 |
|
} |
107
|
|
|
} |
108
|
|
|
|