|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JobQueue\Domain; |
|
4
|
|
|
|
|
5
|
|
|
use JobQueue\Domain\Job\ExecutableJob; |
|
6
|
|
|
use JobQueue\Domain\Task\Profile; |
|
7
|
|
|
use JobQueue\Domain\Task\Queue; |
|
8
|
|
|
use JobQueue\Domain\Task\Status; |
|
9
|
|
|
use JobQueue\Domain\Task\Task; |
|
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class Worker implements LoggerAwareInterface |
|
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
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @param $name |
|
42
|
|
|
* @param Queue $queue |
|
43
|
|
|
* @param Profile $profile |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function __construct(string $name, Queue $queue, Profile $profile) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->name = $name; |
|
48
|
3 |
|
$this->queue = $queue; |
|
49
|
3 |
|
$this->profile = $profile; |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getName(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @return Profile |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getProfile(): Profile |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->profile; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* @param LoggerInterface $logger |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setLogger(LoggerInterface $logger) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($logger instanceof \Monolog\Logger) { |
|
|
|
|
|
|
77
|
|
|
$profile = $this->getProfile(); |
|
78
|
|
|
$logger->pushProcessor(function ($record) use ($profile) { |
|
|
|
|
|
|
79
|
|
|
$record['extra']['profile'] = $profile; |
|
80
|
|
|
return $record; |
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->logger = $logger; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* @param int|null $quantity |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function consume(int $quantity = null) |
|
92
|
|
|
{ |
|
93
|
4 |
|
$i = 0; |
|
94
|
4 |
|
while ($task = $this->queue->fetch($this->profile)) { |
|
95
|
|
|
// Configure the job |
|
96
|
4 |
|
$job = $this->getAndConfigureJob($task); |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
// Execute the job |
|
100
|
4 |
|
$job->setUp($task); |
|
101
|
4 |
|
$job->perform($task); |
|
102
|
2 |
|
$job->tearDown($task); |
|
103
|
|
|
|
|
104
|
2 |
|
$this->queue->updateStatus($task, new Status(Status::FINISHED)); |
|
105
|
|
|
|
|
106
|
2 |
|
} catch (\Exception $e) { |
|
107
|
|
|
// Report error to logger if exists |
|
108
|
2 |
|
if ($this->logger) { |
|
109
|
|
|
$this->logger->error($e->getMessage(), [ |
|
110
|
|
|
'worker' => $this->getName(), |
|
111
|
|
|
'profile' => (string) $task->getProfile(), |
|
112
|
|
|
'job' => $task->getJobName(true), |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Mark task as failed |
|
117
|
2 |
|
$this->queue->updateStatus($task, new Status(Status::FAILED)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Exit worker if a quantity has been set |
|
121
|
4 |
|
$i++; |
|
122
|
4 |
|
if ($quantity === $i) { |
|
123
|
4 |
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
4 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* |
|
130
|
|
|
* @param Task $task |
|
131
|
|
|
* @return ExecutableJob |
|
132
|
|
|
*/ |
|
133
|
4 |
|
private function getAndConfigureJob(Task $task): ExecutableJob |
|
134
|
|
|
{ |
|
135
|
4 |
|
$job = $task->getJob(); |
|
136
|
|
|
|
|
137
|
4 |
|
$logger = $this->logger; |
|
138
|
|
|
|
|
139
|
4 |
|
if ($logger instanceof \Monolog\Logger) { |
|
140
|
|
|
$logger->pushProcessor(function ($record) use ($task) { |
|
141
|
|
|
$record['extra']['job'] = $task->getJobName(true); |
|
142
|
|
|
return $record; |
|
143
|
|
|
}); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
4 |
|
if (!is_null($logger)) { |
|
147
|
|
|
$job->setLogger($logger); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
4 |
|
return $job; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths