-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathWorker.php
More file actions
199 lines (170 loc) · 5.72 KB
/
Worker.php
File metadata and controls
199 lines (170 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php declare(strict_types=1);
namespace MaxBrokman\SafeQueue;
use Doctrine\ORM\EntityManager;
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Queue\Failed\FailedJobProviderInterface;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\Worker as IlluminateWorker;
use Illuminate\Queue\WorkerOptions;
use MaxBrokman\SafeQueue\Exceptions\EntityManagerClosedException;
use MaxBrokman\SafeQueue\Exceptions\QueueFailureException;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Throwable;
/*final*/ class Worker extends IlluminateWorker
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var Stopper
*/
private $stopper;
/**
* Worker constructor.
* @param QueueManager $manager
* @param FailedJobProviderInterface $failer
* @param Dispatcher $events
* @param EntityManager $entityManager
* @param Stopper $stopper
* @param ExceptionHandler $exceptions
*/
public function __construct(
QueueManager $manager,
FailedJobProviderInterface $failer,
Dispatcher $events,
EntityManager $entityManager,
Stopper $stopper,
ExceptionHandler $exceptions
) {
parent::__construct($manager, $events, $exceptions);
$this->entityManager = $entityManager;
$this->stopper = $stopper;
}
/**
* Listen to the given queue and work jobs from it without re-booting the framework.
*
* This is a slight re-working of the parent implementation to aid testing.
*
* @param string $connectionName
* @param null $queue
* @param \Illuminate\Queue\WorkerOptions $options
* @return void
* @throws Exception
*/
public function daemon($connectionName, $queue, WorkerOptions $options)
{
$lastRestart = $this->getTimestampOfLastQueueRestart();
while (true) {
$this->registerTimeoutHandler($options);
if ($this->daemonShouldRun($options)) {
$canContinue = $this->runNextJob(
$connectionName, $queue, $options
);
if ($canContinue === false) {
break;
}
} else {
$this->sleep($options->sleep);
}
if ($this->memoryExceeded($options->memory) || $this->queueShouldRestart($lastRestart)) {
break;
}
}
$this->stop();
}
/**
* Overridden to allow testing.
*/
public function stop()
{
$this->events->fire(new WorkerStopping);
$this->stopper->stop();
}
/**
* We clear the entity manager, assert that it's open and also assert that
* the database has an open connection before running each job.
*
* @param string $connectionName
* @param string $queue
* @param \Illuminate\Queue\WorkerOptions $options
* @return bool
* @throws Exception
*/
public function runNextJob($connectionName, $queue, WorkerOptions $options)
{
$this->entityManager->clear();
try {
$this->assertEntityManagerOpen();
} catch (EntityManagerClosedException $e) {
if ($this->exceptions) {
$this->exceptions->report($e);
}
return false;
}
$this->assertGoodDatabaseConnection();
try {
$job = $this->fetchNewJob($connectionName, $queue);
// If we're able to pull a job off of the stack, we will process it and then return
// from this method. If there is no job on the queue, we will "sleep" the worker
// for the specified number of seconds, then keep processing jobs after sleep.
if ($job) {
$this->process(
$connectionName, $job, $options
);
return true;
}
} catch (Exception | Throwable $e) {
if ($this->exceptions) {
$this->exceptions->report(new FatalThrowableError($e));
}
if ($e instanceof QueueMustStop || $e instanceof QueueFailure) {
return false;
}
}
$this->sleep($options->sleep);
return true;
}
/**
* @throws EntityManagerClosedException
*/
private function assertEntityManagerOpen()
{
if ($this->entityManager->isOpen()) {
return;
}
throw new EntityManagerClosedException;
}
/**
* Some database systems close the connection after a period of time, in MySQL this is system variable
* `wait_timeout`. Given the daemon is meant to run indefinitely we need to make sure we have an open
* connection before working any job. Otherwise we would see `MySQL has gone away` type errors.
*/
private function assertGoodDatabaseConnection()
{
$connection = $this->entityManager->getConnection();
if ($connection->ping() === false) {
$connection->close();
$connection->connect();
}
}
/**
* @param string $connectionName
* @param null $queue
* @return \Illuminate\Contracts\Queue\Job|null
* @throws QueueFailureException
*/
private function fetchNewJob(string $connectionName, $queue): ?\Illuminate\Contracts\Queue\Job
{
try {
return $this->getNextJob(
$this->manager->connection($connectionName), $queue
);
} catch (Exception | Throwable $e) {
throw new QueueFailureException($e);
}
}
}