|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JobQueue\Domain; |
|
4
|
|
|
|
|
5
|
|
|
use JobQueue\Domain\Task\Profile; |
|
6
|
|
|
use JobQueue\Domain\Task\Queue; |
|
7
|
|
|
use JobQueue\Domain\Task\Status; |
|
8
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
10
|
|
|
|
|
11
|
|
|
final class Worker implements LoggerAwareInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use LoggerAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $name; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var Queue |
|
24
|
|
|
*/ |
|
25
|
|
|
private $queue; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @var Profile |
|
30
|
|
|
*/ |
|
31
|
|
|
private $profile; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @param $name |
|
36
|
|
|
* @param Queue $queue |
|
37
|
|
|
* @param Profile $profile |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function __construct(string $name, Queue $queue, Profile $profile) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->name = $name; |
|
42
|
1 |
|
$this->queue = $queue; |
|
43
|
1 |
|
$this->profile = $profile; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getName(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* @return Profile |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getProfile(): Profile |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->profile; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @param int|null $quantity |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function consume(int $quantity = null) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$i = 0; |
|
71
|
2 |
|
while ($task = $this->queue->fetch($this->profile)) { |
|
72
|
|
|
// Set up the job |
|
73
|
2 |
|
$job = $task->getJob(); |
|
74
|
2 |
|
if ($this->logger) { |
|
75
|
|
|
$job->setLogger($this->logger); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
try { |
|
79
|
|
|
// Execute the job |
|
80
|
2 |
|
$job->setUp($task); |
|
81
|
2 |
|
$job->perform($task); |
|
82
|
1 |
|
$job->tearDown($task); |
|
83
|
|
|
|
|
84
|
1 |
|
$this->queue->updateStatus($task, new Status(Status::FINISHED)); |
|
85
|
|
|
|
|
86
|
1 |
|
} catch (\Exception $e) { |
|
87
|
|
|
// Report error to logger if exists |
|
88
|
1 |
|
if ($this->logger) { |
|
89
|
|
|
$this->logger->error($e->getMessage(), [ |
|
90
|
|
|
'worker' => $this->getName(), |
|
91
|
|
|
'profile' => (string) $task->getProfile(), |
|
92
|
|
|
'job' => $task->getJobName(true), |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Mark task as failed |
|
97
|
1 |
|
$this->queue->updateStatus($task, new Status(Status::FAILED)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Exit worker if a quantity has been set |
|
101
|
2 |
|
$i++; |
|
102
|
2 |
|
if ($quantity === $i) { |
|
103
|
2 |
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
2 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|