8000 [Messenger] Do not stack retry stamp by jderusse · Pull Request #36810 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Do not stack retry stamp #36810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;

/**
Expand All @@ -31,12 +32,14 @@ class SendFailedMessageForRetryListener implements EventSubscriberInterface
private $sendersLocator;
private $retryStrategyLocator;
private $logger;
private $historySize;

public function __construct(ContainerInterface $sendersLocator, ContainerInterface $retryStrategyLocator, LoggerInterface $logger = null)
public function __construct(ContainerInterface $sendersLocator, ContainerInterface $retryStrategyLocator, LoggerInterface $logger = null, int $historySize = 10)
{
$this->sendersLocator = $sendersLocator;
$this->retryStrategyLocator = $retryStrategyLocator;
$this->logger = $logger;
$this->historySize = $historySize;
}

public function onMessageFailed(WorkerMessageFailedEvent $event)
Expand Down Expand Up @@ -64,7 +67,7 @@ public function onMessageFailed(WorkerMessageFailedEvent $event)
}

// add the delay and retry stamp info
$retryEnvelope = $envelope->with(new DelayStamp($delay), new RedeliveryStamp($retryCount));
$retryEnvelope = $this->withLimitedHistory($envelope, new DelayStamp($delay), new RedeliveryStamp($retryCount));

// re-send the message for retry
$this->getSenderForTransport($event->getReceiverName())->send($retryEnvelope);
Expand All @@ -75,6 +78,30 @@ public function onMessageFailed(WorkerMessageFailedEvent $event)
}
}

/**
* Adds stamps to the envelope by keeping only the First + Last N stamps
*/
private function withLimitedHistory(Envelope $envelope, StampInterface ...$stamps): Envelope
{
foreach ($stamps as $stamp) {
$history = $envelope->all(get_class($stamp));
if (\count($history) < $this->historySize) {
$envelope = $envelope->with($stamp);
continue;
}

$history = \array_merge(
[$history[0]],
\array_slice($history, -$this->historySize + 2),
[$stamp]
);

$envelope = $envelope->withoutAll(get_class($stamp))->with(...$history);
}

return $envelope;
}

public static function getSubscribedEvents()
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,40 @@ public function testEnvelopeIsSentToTransportOnRetry()

$listener->onMessageFailed($event);
}

public function testEnvelopeKeepOnlyTheLast10Stamps()
{
$exception = new \Exception('no!');
$stamps = \array_merge(
\array_fill(0, 15, new DelayStamp(1)),
\array_fill(0, 3, new RedeliveryStamp(1))
);
$envelope = new Envelope(new \stdClass(), $stamps);

$sender = $this->createMock(SenderInterface::class);
$sender->expects($this->once())->method('send')->willReturnCallback(function (Envelope $envelope) {
$delayStamps = $envelope->all(DelayStamp::class);
$redeliveryStamps = $envelope->all(RedeliveryStamp::class);

$this->assertCount(10, $delayStamps);
$this->assertCount(4, $redeliveryStamps);

return $envelope;
});
$senderLocator = $this->createMock(ContainerInterface::class);
$senderLocator->expects($this->once())->method('has')->willReturn(true);
$senderLocator->expects($this->once())->method('get')->willReturn($sender);
$retryStategy = $this->createMock(RetryStrategyInterface::class);
$retryStategy->expects($this->once())->method('isRetryable')->willReturn(true);
$retryStategy->expects($this->once())->method('getWaitingTime')->willReturn(1000);
$retryStrategyLocator = $this->createMock(ContainerInterface::class);
$retryStrategyLocator->expects($this->once())->method('has')->willReturn(true);
$retryStrategyLocator->expects($this->once())->method('get')->willReturn($retryStategy);

$listener = new SendFailedMessageForRetryListener($senderLocator, $retryStrategyLocator);

$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);

$listener->onMessageFailed($event);
}
}
0