Repository navigation
Expand file tree
/
Copy pathWorker.php
More file actions
597 lines (497 loc) · 25.2 KB
/
Copy pathWorker.php
File metadata and controls
597 lines (497 loc) · 25.2 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Cooldown\CooldownStatus;
use Symfony\Component\Messenger\Cooldown\CooldownStrategyInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Event\WorkerRateLimitedEvent;
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
use Symfony\Component\Messenger\Event\WorkerTransportCoolingDownEvent;
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException;
use Symfony\Component\Messenger\Exception\EnvelopeAwareExceptionInterface;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\RejectRedeliveredMessageException;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Execution\DeferredBatchMessageQueue;
use Symfony\Component\Messenger\Execution\MessageExecutionCallback;
use Symfony\Component\Messenger\Execution\MessageExecutionStrategyInterface;
use Symfony\Component\Messenger\Execution\SyncMessageExecutionStrategy;
use Symfony\Component\Messenger\Stamp\AckStamp;
use Symfony\Component\Messenger\Stamp\BusNameStamp;
use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
use Symfony\Component\Messenger\Stamp\FlushBatchHandlersStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\NoAutoAckStamp;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Receiver\KeepaliveReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\QueueReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\RateLimiter\LimiterInterface;
/**
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Tobias Schultze <http://tobion.de>
*
* @final
*/
class Worker
{
private bool $shouldStop = false;
private WorkerMetadata $metadata;
private array $acks = [];
private ?DeferredBatchMessageQueue $unacks = null;
/**
* @var \SplObjectStorage<object, array{0: string, 1: Envelope}>
*/
private \SplObjectStorage $keepalives;
private \WeakMap $batchReceipts;
private readonly MessageExecutionCallback $completionCallback;
/**
* @var array<string, CooldownStatus> The statuses of the transports the worker skips because they are cooling down
*/
private array $coolingDownTransports = [];
private readonly MessageExecutionStrategyInterface $messageExecutionStrategy;
/**
* @param ReceiverInterface[] $receivers Where the key is the transport name
* @param CooldownStrategyInterface[] $cooldownStrategies Where the key is the transport name
*/
public function __construct(
private array $receivers,
private MessageBusInterface $bus,
private ?EventDispatcherInterface $eventDispatcher = null,
private ?LoggerInterface $logger = null,
private ?array $rateLimiters = null,
private ClockInterface $clock = new Clock(),
?MessageExecutionStrategyInterface $messageExecutionStrategy = null,
private array $cooldownStrategies = [],
) {
$this->metadata = new WorkerMetadata([
'transportNames' => array_keys($receivers),
]);
$this->keepalives = new \SplObjectStorage();
$this->batchReceipts = new \WeakMap();
$this->completionCallback = new MessageExecutionCallback($this->preAck(...), $this->enqueueAck(...));
$this->messageExecutionStrategy = $messageExecutionStrategy ?? new SyncMessageExecutionStrategy($this->bus, $this->enqueueAck(...));
}
/**
* Receive the messages and dispatch them to the bus.
*
* Valid options are:
* * sleep (default: 1000000): Time in microseconds to sleep after no messages are found
* * time_limit: The time limit in seconds the worker can handle new messages
* * stop_when_empty (default: false): Whether to stop once the receivers have no message left (a receiver cooling down or failing to fetch counts as having none), after handling the messages in flight and the pending batches
* * queues: The queue names to consume from, instead of consuming from all queues. When this is used, all receivers must implement the QueueReceiverInterface
*/
public function run(array $options = []): void
{
$options = array_merge([
'sleep' => 1000000,
'time_limit' => null,
'stop_when_empty' => false,
], $options);
$queueNames = $options['queues'] ?? null;
$endTime = null !== $options['time_limit'] ? $this->now() + $options['time_limit'] : null;
$bufferedEnvelopes = [];
$this->metadata->set(['queueNames' => $queueNames]);
$this->logger?->info('Starting worker.', ['transport_names' => $this->metadata->getTransportNames()]);
$this->eventDispatcher?->dispatch(new WorkerStartedEvent($this, null === $endTime ? null : (float) $endTime, $options['sleep']));
if ($queueNames) {
// if queue names are specified, all receivers must implement the QueueReceiverInterface
foreach ($this->receivers as $transportName => $receiver) {
if (!$receiver instanceof QueueReceiverInterface) {
throw new RuntimeException(\sprintf('Receiver for "%s" does not implement "%s".', $transportName, QueueReceiverInterface::class));
}
}
}
while (!$this->shouldStop) {
if (null !== $endTime && $this->now() >= $endTime) {
$this->logger?->info('Worker stopped due to time limit of {timeLimit}s exceeded', ['timeLimit' => $options['time_limit']]);
break;
}
$envelopeHandled = false;
$envelopeHandledStart = $this->now();
$fetchSize = max(1, $options['fetch_size'] ?? 1);
if ($this->messageExecutionStrategy->shouldPauseConsumption()
&& $this->messageExecutionStrategy->wait($this->completionCallback)
) {
if ($this->shouldStop) {
break;
}
continue;
}
// drain buffered envelopes first: they are already off the broker and must not
// be starved by fetching new messages from higher-priority receivers
$receivers = $bufferedEnvelopes ? array_intersect_key($this->receivers, $bufferedEnvelopes) + $this->receivers : $this->receivers;
foreach ($receivers as $transportName => $receiver) {
if ($envelopes = $bufferedEnvelopes[$transportName] ?? null) {
unset($bufferedEnvelopes[$transportName]);
// advance past the last handled envelope now that the worker is ready
// again; lazy receivers fetch from the broker only at this point
$envelopes->next();
// the buffer is drained: poll the receivers again, in their order of priority
if (!$envelopes->valid()) {
continue 2;
}
} else {
$status = ($this->cooldownStrategies[$transportName] ?? null)?->checkStatus($transportName);
if ($status?->isCoolingDown()) {
if (!isset($this->coolingDownTransports[$transportName])) {
$this->eventDispatcher?->dispatch(new WorkerTransportCoolingDownEvent($transportName, $status));
}
$this->coolingDownTransports[$transportName] = $status;
continue;
}
unset($this->coolingDownTransports[$transportName]);
$envelopes = $this->fetch($transportName, $receiver, $queueNames, $fetchSize);
$envelopes->rewind();
}
for (; $envelopes->valid(); $envelopes->next()) {
$envelope = $envelopes->current();
$envelopeHandled = true;
$receipt = new \stdClass();
if ($receiver instanceof KeepaliveReceiverInterface) {
$this->keepalives[$receipt] = [$transportName, $envelope];
}
$this->rateLimit($transportName);
$this->handleMessage($envelope, $transportName, $receipt, $options['bus_name'] ?? null);
$this->eventDispatcher?->dispatch(new WorkerRunningEvent($this, false));
if ($this->messageExecutionStrategy->shouldPauseConsumption()) {
$bufferedEnvelopes[$transportName] = $envelopes;
break 2;
}
if ($this->shouldStop) {
break 2;
}
}
// after handling a single receiver, quit and start the loop again
// this should prevent multiple lower priority receivers from
// blocking too long before the higher priority are checked
if ($envelopeHandled) {
break;
}
}
if (!$envelopeHandled && $this->flush(false)) {
continue;
}
if (!$this->flush(30.0) && !$envelopeHandled) {
if ($options['stop_when_empty']) {
// the messages in flight and the pending batches can dispatch new messages
if ($this->flush(true)) {
continue;
}
if ($this->coolingDownTransports) {
$this->logger?->info('Worker stopped because no message was available outside of the transports that are cooling down: {transports}', ['transports' => implode(', ', array_keys($this->coolingDownTransports))]);
} else {
$this->logger?->info('Worker stopped because no message was available');
}
break;
}
$this->eventDispatcher?->dispatch(new WorkerRunningEvent($this, true));
if ($this->shouldStop) {
continue;
}
if (0 < $sleep = (int) ($options['sleep'] - 1e6 * ($this->now() - $envelopeHandledStart))) {
if (null !== $endTime) {
$sleep = min($sleep, (int) ceil(1e6 * ($endTime - $this->now())));
}
foreach ($this->coolingDownTransports as $status) {
if ($end = $status->getEnd()) {
$sleep = (int) min($sleep, ceil(1e6 * ((float) $end->format('U.u') - $this->now())));
}
}
if (0 < $sleep) {
$sleepStart = $this->now();
// messages in flight are acknowledged as soon as they are handled, instead of after the sleep
if (!$this->messageExecutionStrategy->wait($this->completionCallback, $sleep / 1e6)) {
$sleep -= (int) (1e6 * ($this->now() - $sleepStart));
if (0 < $sleep) {
$this->clock->sleep($sleep / 1e6);
}
}
}
}
}
}
$this->flush(true);
$this->eventDispatcher?->dispatch(new WorkerStoppedEvent($this));
}
/**
* @return \Generator<Envelope>
*/
private function fetch(string $transportName, ReceiverInterface $receiver, ?array $queueNames, int $fetchSize): \Generator
{
try {
if ($queueNames) {
/** @var QueueReceiverInterface $receiver */
$envelopes = $receiver->getFromQueues($queueNames, $fetchSize);
} else {
$envelopes = $receiver->get($fetchSize);
}
yield from $envelopes;
} catch (TransportException $e) {
if (!$cooldownStrategy = $this->cooldownStrategies[$transportName] ?? null) {
throw $e;
}
$this->logger?->warning('Fetching messages from transport {transport} failed: {error}', ['transport' => $transportName, 'error' => $e->getMessage(), 'exception' => $e]);
$cooldownStrategy->recordFailure($transportName, null, $e);
}
}
private function handleMessage(Envelope $envelope, string $transportName, object $receipt, ?string $busName = null): void
{
$event = new WorkerMessageReceivedEvent($envelope, $transportName);
$this->eventDispatcher?->dispatch($event);
$envelope = $event->getEnvelope();
if (!$event->shouldHandle()) {
unset($this->keepalives[$receipt]);
return;
}
if (null !== $busName && $busName !== $envelope->last(BusNameStamp::class)?->getBusName()) {
$envelope = $envelope->withoutAll(BusNameStamp::class)->with(new BusNameStamp($busName));
}
$onHandled = new MessageExecutionCallback(
function (Envelope $envelope, string $transportName, bool &$acked, ?\Throwable $e = null) use ($receipt): void {
$this->preAck($envelope, $transportName, $acked, $e, $receipt);
},
function (string $transportName, Envelope $envelope, ?\Throwable $e = null, ?\Closure $combine = null) use ($receipt): void {
$this->acks[] = [$transportName, $envelope, $e, $combine, $receipt];
},
);
$this->messageExecutionStrategy->execute(
$envelope->with(new ReceivedStamp($transportName), new ConsumedByWorkerStamp()),
$transportName,
$onHandled,
);
}
private function enqueueAck(string $transportName, Envelope $envelope, ?\Throwable $e = null, ?\Closure $combine = null): void
{
$this->acks[] = [$transportName, $envelope, $e, $combine];
}
private function preAck(Envelope $envelope, string $transportName, bool &$acked, ?\Throwable $e = null, ?object $receipt = null): void
{
$receipt ??= $this->findReceipt($envelope, $transportName);
$noAutoAckStamp = $envelope->last(NoAutoAckStamp::class);
if (!$acked && !$noAutoAckStamp) {
$this->acks[] = [$transportName, $envelope, $e, null, $receipt];
} elseif ($noAutoAckStamp) {
$this->unacks ??= new DeferredBatchMessageQueue();
foreach ($envelope->all(NoAutoAckStamp::class) as $stamp) {
$this->batchReceipts[$stamp->getHandlerDescriptor()->getBatchHandler()] = $receipt;
$this->unacks->add($stamp->getHandlerDescriptor()->getBatchHandler(), $transportName, $envelope, $acked, $this->now());
}
}
$this->ack();
}
private function ack(): bool
{
$acks = $this->acks;
$this->acks = [];
foreach ($acks as $ack) {
$receipt = $ack[4] ?? $this->findReceipt($ack[1], $ack[0]);
if (isset($ack[3]) && !$ack = $ack[3]([$ack])) {
continue;
}
[$transportName, $envelope, $e] = $ack;
$receiver = $this->receivers[$transportName];
if (null !== $e) {
if ($rejectFirst = $e instanceof RejectRedeliveredMessageException) {
// redelivered messages are rejected first so that continuous failures in an event listener or while
// publishing for retry does not cause infinite redelivery loops
unset($this->keepalives[$receipt]);
$receiver->reject($envelope);
}
if ($e instanceof EnvelopeAwareExceptionInterface && null !== $e->getEnvelope()) {
$envelope = $e->getEnvelope();
}
if ($e instanceof DelayedMessageHandlingException) {
// a retry must run the handlers again, so that they dispatch the delayed messages that failed again
$envelope = $envelope->withoutAll(HandledStamp::class);
}
$failedEvent = new WorkerMessageFailedEvent($envelope, $transportName, $e);
$this->eventDispatcher?->dispatch($failedEvent);
$envelope = $failedEvent->getEnvelope();
if (!$rejectFirst) {
unset($this->keepalives[$receipt]);
if ($failedEvent->willRetry()) {
$receiver->ack($envelope);
} else {
$receiver->reject($envelope);
}
}
// the strategy gets the envelope stamped by the listeners of the failed event, the retry listener included
($this->cooldownStrategies[$transportName] ?? null)?->recordFailure($transportName, $envelope, $e);
continue;
}
$handledEvent = new WorkerMessageHandledEvent($envelope, $transportName);
$this->eventDispatcher?->dispatch($handledEvent);
$envelope = $handledEvent->getEnvelope();
if (null !== $this->logger) {
$message = $envelope->getMessage();
$context = [
'class' => $message::class,
'message_id' => $envelope->last(TransportMessageIdStamp::class)?->getId(),
];
$this->logger->info('{class} was handled successfully (acknowledging to transport).', $context);
}
unset($this->keepalives[$receipt]);
$receiver->ack($envelope);
($this->cooldownStrategies[$transportName] ?? null)?->recordSuccess($transportName, $envelope);
}
return (bool) $acks;
}
private function rateLimit(string $transportName): void
{
if (!$this->rateLimiters) {
return;
}
if (!\array_key_exists($transportName, $this->rateLimiters)) {
return;
}
/** @var LimiterInterface $rateLimiter */
$rateLimiter = $this->rateLimiters[$transportName]->create();
if ($rateLimiter->consume()->isAccepted()) {
return;
}
$this->logger?->info('Transport {transport} is being rate limited, waiting for token to become available...', ['transport' => $transportName]);
$this->eventDispatcher?->dispatch(new WorkerRateLimitedEvent($rateLimiter, $transportName));
$rateLimiter->reserve()->wait();
$rateLimiter->consume();
}
private function flush(bool|float $force): bool
{
$flushed = $this->messageExecutionStrategy->flush($this->completionCallback, $force);
if (!$this->unacks?->hasPending()) {
return $flushed;
}
$unacks = $this->unacks->popFlushable($force, $this->now());
if (!$unacks->count()) {
return $flushed;
}
foreach ($unacks as $handler) {
$deferredMessage = $unacks[$handler];
$receipt = $this->batchReceipts[$handler];
unset($this->batchReceipts[$handler]);
if ($deferredMessage->acked) {
continue;
}
$transportName = $deferredMessage->context;
$envelope = $deferredMessage->envelope;
$heldAcks = \count($this->acks);
try {
$e = null;
$this->bus->dispatch($envelope->withoutAll(AckStamp::class)->with(new FlushBatchHandlersStamp(true === $force || !\is_bool($force))));
} catch (\Throwable $e) {
$this->failHeldAcks($heldAcks);
if (!$deferredMessage->acked) {
$envelope = $envelope->withoutAll(NoAutoAckStamp::class);
if ($e instanceof HandlerFailedException) {
// a retry must run the batch handlers whose flush failed
$failed = $e->getWrappedExceptions();
$envelope = $envelope->withoutAll(HandledStamp::class)->with(...array_filter($envelope->all(HandledStamp::class), static fn (HandledStamp $stamp) => !isset($failed[$stamp->getHandlerName()])));
$e = new HandlerFailedException($envelope, $failed);
}
if ($ackStamp = $envelope->last(AckStamp::class)) {
// the acks that the batch sent during the flush come first, and this failure is ignored when they already gave the message its outcome
$ackStamp->ack($envelope, $e);
} else {
$this->acks[] = [$transportName, $envelope, $e, null, $receipt];
}
}
continue;
}
if ($deferredMessage->acked) {
continue;
}
$noAutoAckStamp = $envelope->last(NoAutoAckStamp::class);
if (!$noAutoAckStamp) {
$this->acks[] = [$transportName, $envelope, $e, null, $receipt];
} elseif ($noAutoAckStamp) {
$this->unacks ??= new DeferredBatchMessageQueue();
$this->batchReceipts[$noAutoAckStamp->getHandlerDescriptor()->getBatchHandler()] = $receipt;
$this->unacks->add($noAutoAckStamp->getHandlerDescriptor()->getBatchHandler(), $transportName, $envelope, $deferredMessage->acked, $this->now());
}
}
return $this->ack() || $flushed;
}
public function stop(): void
{
$this->logger?->info('Stopping worker.', ['transport_names' => $this->metadata->getTransportNames()]);
$this->shouldStop = true;
}
public function keepalive(?int $seconds): void
{
foreach ($this->keepalives as $message) {
[$transportName, $envelope] = $this->keepalives[$message];
$receiver = $this->receivers[$transportName];
if (!$receiver instanceof KeepaliveReceiverInterface) {
throw new RuntimeException(\sprintf('Receiver for "%s" does not implement "%s".', $transportName, KeepaliveReceiverInterface::class));
}
$context = [
'transport' => $transportName,
'message_id' => $envelope->last(TransportMessageIdStamp::class)?->getId(),
];
$this->logger?->info('Sending keepalive request.', $context);
try {
$receiver->keepalive($envelope, $seconds);
} catch (\Throwable $e) {
// the alarm is raised at any point of the program, so an error thrown from here
// surfaces wherever the worker happened to be: it stops the worker, or it is read
// as a failure of the message that was being handled at that moment
$this->logger?->warning('Keepalive request failed: {error}', $context + ['error' => $e->getMessage(), 'exception' => $e]);
}
}
}
public function getMetadata(): WorkerMetadata
{
return $this->metadata;
}
private function now(): float
{
$now = $this->clock->now();
return \PHP_INT_SIZE >= 8 ? $now->getTimestamp() + $now->getMicrosecond() / 1e6 : (float) $now->format('U.u');
}
/**
* Turns the acks that messages sent during a failed dispatch into failures.
*
* Their batch was flushed during that dispatch, so what the batch did for them may have been undone.
* The acks held from $offset on are the ones sent during that dispatch.
*/
private function failHeldAcks(int $offset): void
{
foreach (\array_slice($this->acks, $offset, null, true) as $i => [$transportName, $envelope, $e]) {
if (null !== $e || !$stamp = $envelope->last(HandledStamp::class)) {
continue;
}
$stamps = $envelope->all(HandledStamp::class);
array_pop($stamps);
$envelope = $envelope->withoutAll(HandledStamp::class)->with(...$stamps);
$e = new RuntimeException(\sprintf('The batch of "%s" was flushed during the dispatch of another message, and that dispatch failed.', $stamp->getHandlerName()));
$this->acks[$i] = [$transportName, $envelope, new HandlerFailedException($envelope, [$stamp->getHandlerName() => $e])] + $this->acks[$i];
}
}
private function findReceipt(Envelope $envelope, string $transportName): object
{
foreach ($this->keepalives as $receipt) {
[$name, $received] = $this->keepalives[$receipt];
if ($name === $transportName && $received->getMessage() === $envelope->getMessage()) {
return $receipt;
}
}
return new \stdClass();
}
}