1 | <?php |
||
14 | class Consumer implements EventSubscriberInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var ConsumerInterface |
||
18 | */ |
||
19 | private $consumer; |
||
20 | |||
21 | /** |
||
22 | * @var OutputInterface |
||
23 | */ |
||
24 | private $output; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $consumerTag; |
||
30 | |||
31 | /** |
||
32 | * @var LimiterInterface[] |
||
33 | */ |
||
34 | private $limiters = []; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $startTime; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $minDuration = 15; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $processed = 0; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $batchSize = 25; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $coolDownTime = 0; |
||
60 | |||
61 | /** |
||
62 | * @param ConsumerInterface $consumer |
||
63 | * @param OutputInterface $output |
||
64 | * @param string $consumerTag |
||
65 | */ |
||
66 | public function __construct(ConsumerInterface $consumer, OutputInterface $output, $consumerTag = null) |
||
67 | { |
||
68 | $this->consumer = $consumer; |
||
69 | $this->output = $output; |
||
70 | $this->consumerTag = $consumerTag; |
||
71 | |||
72 | $this->consumer->getEventDispatcher()->addSubscriber($this); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public static function getSubscribedEvents() |
||
79 | { |
||
80 | return [ |
||
81 | QueueEvents::CONSUME_MESSAGE => 'onConsumeMessage', |
||
82 | QueueEvents::CONSUMED_MESSAGE => 'onMessageConsumed', |
||
83 | QueueEvents::CONSUME_EXCEPTION => 'onConsumeException', |
||
84 | ]; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param LimiterInterface $limiter |
||
89 | */ |
||
90 | public function addLimiter(LimiterInterface $limiter) |
||
91 | { |
||
92 | $this->limiters[] = $limiter; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param int $duration |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function mustRunFor($duration) |
||
101 | { |
||
102 | $this->minDuration = $duration; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param int $batchSize |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function flushAfter($batchSize) |
||
113 | { |
||
114 | $this->batchSize = $batchSize; |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param int $coolDownTime |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function waitBetweenMessages($coolDownTime) |
||
125 | { |
||
126 | $this->coolDownTime = $coolDownTime; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return int |
||
133 | */ |
||
134 | public function getProcessed() |
||
135 | { |
||
136 | return $this->processed; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function getStartTime() |
||
143 | { |
||
144 | return $this->startTime; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getDuration() |
||
151 | { |
||
152 | return time() - $this->startTime; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | public function consume() |
||
159 | { |
||
160 | $this->startTime = time(); |
||
161 | |||
162 | try { |
||
163 | $this->consumer->consume($this->consumerTag); |
||
164 | |||
165 | $this->shutdown(); |
||
166 | } catch (\Exception $e) { |
||
167 | $this->output->writeln( |
||
168 | sprintf('Uncaught %s thrown by consumer, shutting down gracefully', get_class($e)), |
||
169 | OutputInterface::VERBOSITY_VERBOSE |
||
170 | ); |
||
171 | |||
172 | $this->shutdown(); |
||
173 | |||
174 | throw $e; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param ConsumeEvent $event |
||
180 | */ |
||
181 | public function onConsumeMessage(ConsumeEvent $event) |
||
194 | |||
195 | /** |
||
196 | * @param ConsumeEvent $event |
||
197 | */ |
||
198 | public function onMessageConsumed(ConsumeEvent $event) |
||
231 | |||
232 | /** |
||
233 | * @param ConsumeExceptionEvent $event |
||
234 | */ |
||
235 | public function onConsumeException(ConsumeExceptionEvent $event) |
||
249 | |||
250 | /** |
||
251 | * @param string $payload |
||
252 | * @param bool $fullPayload |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | private function getPayloadOutput($payload, $fullPayload = false) |
||
269 | |||
270 | /** |
||
271 | * Dispatches flush event. |
||
272 | */ |
||
273 | private function flush() |
||
282 | |||
283 | /** |
||
284 | * Shutdown procedure |
||
285 | */ |
||
286 | private function shutdown() |
||
310 | } |
||
311 |