Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f757a6

Browse files
author
Joris Steyn
committed
[Messenger] Allow retries to be logged as warnings
This commit introduces a marker interface for exceptions to allow retries to be logged as warnings instead of the default error severity.This behaviour is already implemented for recoverable exceptions, but those are retried ad infinitum regardless of the retry strategy. With the new marker interface, it's possible to log exceptions as warnings while still using the retry strategy.The RecoverableExceptionInterface now extends this interface to achieve the existing behaviour using the new more flexible approach.
1 parent6b1d9b8 commit8f757a6

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add support for resetting container services after each messenger message.
99
* Added`WorkerMetadata` class which allows you to access the configuration details of a worker, like`queueNames` and`transportNames` it consumes from.
1010
* New method`getMetadata()` was added to`Worker` class which returns the`WorkerMetadata` object.
11+
* Add the`LogRetryAsWarningInterface` for exceptions to log retries as warnings instead of errors allowing the same behaviour as RecoverableExceptionInterface while still using the retry strategy
1112

1213
5.3
1314
---

‎src/Symfony/Component/Messenger/EventListener/SendFailedMessageForRetryListener.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
useSymfony\Component\Messenger\Event\WorkerMessageFailedEvent;
1919
useSymfony\Component\Messenger\Event\WorkerMessageRetriedEvent;
2020
useSymfony\Component\Messenger\Exception\HandlerFailedException;
21+
useSymfony\Component\Messenger\Exception\LogRetryAsWarningInterface;
2122
useSymfony\Component\Messenger\Exception\RecoverableExceptionInterface;
2223
useSymfony\Component\Messenger\Exception\RuntimeException;
2324
useSymfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
@@ -72,11 +73,11 @@ public function onMessageFailed(WorkerMessageFailedEvent $event)
7273

7374
if (null !==$this->logger) {
7475
$logLevel = LogLevel::ERROR;
75-
if ($throwableinstanceofRecoverableExceptionInterface) {
76+
if ($throwableinstanceofLogRetryAsWarningInterface) {
7677
$logLevel = LogLevel::WARNING;
7778
}elseif ($throwableinstanceof HandlerFailedException) {
7879
foreach ($throwable->getNestedExceptions()as$nestedException) {
79-
if ($nestedExceptioninstanceofRecoverableExceptionInterface) {
80+
if ($nestedExceptioninstanceofLogRetryAsWarningInterface) {
8081
$logLevel = LogLevel::WARNING;
8182
break;
8283
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Exception;
13+
14+
/**
15+
* Marker interface for exceptions to indicate a retry should be logged as warning instead of error.
16+
*
17+
* @author Joris Steyn <j.steyn@hoffelijk.nl>
18+
*/
19+
interface LogRetryAsWarningInterfaceextends \Throwable
20+
{
21+
}

‎src/Symfony/Component/Messenger/Exception/RecoverableExceptionInterface.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
*
2020
* @author Jérémy Derussé <jeremy@derusse.com>
2121
*/
22-
interface RecoverableExceptionInterfaceextends\Throwable
22+
interface RecoverableExceptionInterfaceextendsLogRetryAsWarningInterface
2323
{
2424
}

‎src/Symfony/Component/Messenger/Tests/EventListener/SendFailedMessageForRetryListenerTest.php‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
usePHPUnit\Framework\TestCase;
1515
usePsr\Container\ContainerInterface;
16+
usePsr\Log\LoggerInterface;
17+
usePsr\Log\LogLevel;
1618
useSymfony\Component\Messenger\Envelope;
1719
useSymfony\Component\Messenger\Event\WorkerMessageFailedEvent;
1820
useSymfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener;
1921
useSymfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
2022
useSymfony\Component\Messenger\Retry\RetryStrategyInterface;
2123
useSymfony\Component\Messenger\Stamp\DelayStamp;
2224
useSymfony\Component\Messenger\Stamp\RedeliveryStamp;
25+
useSymfony\Component\Messenger\Tests\Fixtures\RetryWithWarningException;
2326
useSymfony\Component\Messenger\Transport\Sender\SenderInterface;
2427
useSymfony\Contracts\EventDispatcher\EventDispatcherInterface;
2528

@@ -108,6 +111,60 @@ public function testEnvelopeIsSentToTransportOnRetry()
108111
$retryStrategyLocator->expects($this->once())->method('has')->willReturn(true);
109112
$retryStrategyLocator->expects($this->once())->method('get')->willReturn($retryStategy);
110113

114+
$logger =$this->createMock(LoggerInterface::class);
115+
$logger->expects($this->once())->method('log')->with(LogLevel::ERROR);
116+
117+
$eventDispatcher =$this->createMock(EventDispatcherInterface::class);
118+
$eventDispatcher->expects($this->once())->method('dispatch');
119+
120+
$listener =newSendFailedMessageForRetryListener($senderLocator,$retryStrategyLocator,$logger,$eventDispatcher);
121+
122+
$event =newWorkerMessageFailedEvent($envelope,'my_receiver',$exception);
123+
124+
$listener->onMessageFailed($event);
125+
}
126+
127+
publicfunctionexceptionLogSeverityProvider()
128+
{
129+
return [
130+
[new \Exception('test'), LogLevel::ERROR],
131+
[newRecoverableMessageHandlingException('test'), LogLevel::WARNING],
132+
[newRetryWithWarningException(), LogLevel::WARNING],
133+
];
134+
}
135+
136+
/**
137+
* @dataProvider exceptionLogSeverityProvider
138+
*/
139+
publicfunctiontestRetriesAreLoggedWithAppropriateLogSeverity(\Exception$exception,string$expectedSeverity)
140+
{
141+
$envelope =newEnvelope(new \stdClass());
142+
143+
$sender =$this->createMock(SenderInterface::class);
144+
$sender->expects($this->once())->method('send')->willReturnCallback(function (Envelope$envelope) {
145+
/** @var DelayStamp $delayStamp */
146+
$delayStamp =$envelope->last(DelayStamp::class);
147+
/** @var RedeliveryStamp $redeliveryStamp */
148+
$redeliveryStamp =$envelope->last(RedeliveryStamp::class);
149+
150+
$this->assertInstanceOf(DelayStamp::class,$delayStamp);
151+
$this->assertSame(1000,$delayStamp->getDelay());
152+
153+
$this->assertInstanceOf(RedeliveryStamp::class,$redeliveryStamp);
154+
$this->assertSame(1,$redeliveryStamp->getRetryCount());
155+
156+
return$envelope;
157+
});
158+
$senderLocator =$this->createMock(ContainerInterface::class);
159+
$senderLocator->expects($this->once())->method('has')->willReturn(true);
160+
$senderLocator->expects($this->once())->method('get')->willReturn($sender);
161+
$retryStategy =$this->createMock(RetryStrategyInterface::class);
162+
$retryStategy->expects($this->any())->method('isRetryable')->willReturn(true);
163+
$retryStategy->expects($this->any())->method('getWaitingTime')->willReturn(1000);
164+
$retryStrategyLocator =$this->createMock(ContainerInterface::class);
165+
$retryStrategyLocator->expects($this->once())->method('has')->willReturn(true);
166+
$retryStrategyLocator->expects($this->once())->method('get')->willReturn($retryStategy);
167+
111168
$eventDispatcher =$this->createMock(EventDispatcherInterface::class);
112169
$eventDispatcher->expects($this->once())->method('dispatch');
113170

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Tests\Fixtures;
13+
14+
useException;
15+
useSymfony\Component\Messenger\Exception\LogRetryAsWarningInterface;
16+
17+
class RetryWithWarningExceptionextends Exceptionimplements LogRetryAsWarningInterface
18+
{
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp