-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathConsumer.php
More file actions
202 lines (173 loc) · 5.27 KB
/
Consumer.php
File metadata and controls
202 lines (173 loc) · 5.27 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
200
201
202
<?php
namespace Bernard;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Bernard\Event\EnvelopeEvent;
use Bernard\Event\PingEvent;
use Bernard\Event\RejectEnvelopeEvent;
class Consumer
{
protected $router;
protected $dispatcher;
protected $shutdown = false;
protected $pause = false;
protected $configured = false;
protected $options = [
'max-runtime' => PHP_INT_MAX,
'max-messages' => null,
'stop-when-empty' => false,
'stop-on-error' => false,
];
/**
* @param Router $router
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(Router $router, EventDispatcherInterface $dispatcher)
{
$this->router = $router;
$this->dispatcher = $dispatcher;
}
/**
* Starts an infinite loop calling Consumer::tick();.
*
* @param Queue $queue
* @param array $options
* @throws \Throwable
*/
public function consume(Queue $queue, array $options = [])
{
$this->bind();
while ($this->tick($queue, $options)) {
pcntl_signal_dispatch();
}
}
/**
* Returns true do indicate it should be run again or false to indicate
* it should not be run again.
*
* @param Queue $queue
* @param array $options
*
* @return bool
* @throws \Throwable
*/
public function tick(Queue $queue, array $options = [])
{
$this->configure($options);
if ($this->shutdown) {
return false;
}
if (microtime(true) > $this->options['max-runtime']) {
return false;
}
if ($this->pause) {
return true;
}
$this->dispatcher->dispatch(BernardEvents::PING, new PingEvent($queue));
if (!$envelope = $queue->dequeue()) {
return !$this->options['stop-when-empty'];
}
$this->invoke($envelope, $queue);
if (null === $this->options['max-messages']) {
return true;
}
return (bool) --$this->options['max-messages'];
}
/**
* Mark Consumer as shutdown.
*/
public function shutdown()
{
$this->shutdown = true;
}
/**
* Pause consuming.
*/
public function pause()
{
$this->pause = true;
}
/**
* Resume consuming.
*/
public function resume()
{
$this->pause = false;
}
/**
* Until there is a real extension point to doing invoked stuff, this can be used
* by wrapping the invoke method.
*
* @param Envelope $envelope
* @param Queue $queue
*
* @throws \Exception
* @throws \Throwable
*/
public function invoke(Envelope $envelope, Queue $queue)
{
try {
$this->dispatcher->dispatch(BernardEvents::INVOKE, new EnvelopeEvent($envelope, $queue));
$receiver = $this->router->route($envelope);
$receiver->receive($envelope->getMessage());
// We successfully processed the message.
$queue->acknowledge($envelope);
$this->dispatcher->dispatch(BernardEvents::ACKNOWLEDGE, new EnvelopeEvent($envelope, $queue));
} catch (\Throwable $error) {
// php 7
$this->rejectDispatch($error, $envelope, $queue);
} catch (\Exception $exception) {
// php 5
$this->rejectDispatch($exception, $envelope, $queue);
}
}
/**
* @param array $options
* @return array
*/
protected function configure(array $options)
{
if ($this->configured) {
return $this->options;
}
$this->options = array_filter($options) + $this->options;
$this->options['max-runtime'] += microtime(true);
$this->configured = true;
return $this->options;
}
/**
* Setup signal handlers for unix signals.
*
* If the process control extension does not exist (e.g. on Windows), ignore the signal handlers.
* The difference is that when terminating the consumer, running processes will not stop gracefully
* and will terminate immediately.
*/
protected function bind()
{
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, [$this, 'shutdown']);
pcntl_signal(SIGINT, [$this, 'shutdown']);
pcntl_signal(SIGQUIT, [$this, 'shutdown']);
pcntl_signal(SIGUSR2, [$this, 'pause']);
pcntl_signal(SIGCONT, [$this, 'resume']);
}
}
/**
* @param \Throwable|\Exception $exception note that the type-hint is missing due to PHP 5.x compat
* @param Envelope $envelope
* @param Queue $queue
*
* @throws \Exception
* @throws \Throwable
*/
private function rejectDispatch($exception, Envelope $envelope, Queue $queue)
{
// Make sure the exception is not interfering.
// Previously failing jobs handling have been moved to a middleware.
//
// Emit an event to let others log that exception
$this->dispatcher->dispatch(BernardEvents::REJECT, new RejectEnvelopeEvent($envelope, $queue, $exception));
if ($this->options['stop-on-error']) {
throw $exception;
}
}
}