Skip to content

Commit cd93168

Browse files
Merge pull request #29 from run-as-root/develop
Adds skip retry check
2 parents 5c79e91 + e5bf39e commit cd93168

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ utilizing The RabbitMQ's [dead letter exchange](https://www.rabbitmq.com/dlx.htm
1414
- [Features](#features)
1515
- [How it works](#how-it-works)
1616
- [Configuration](#configuration)
17+
- [Skipping the retry](#skipping-the-retry)
1718
- [License](#licence)
1819

20+
---
21+
1922
## Prerequisites
2023

2124
- Magento 2.4.5 or higher
@@ -43,6 +46,8 @@ To enable the module:
4346
bin/magento module:enable RunAsRoot_MessageQueueRetry
4447
```
4548

49+
---
50+
4651
## Features
4752

4853
- Toggle activation
@@ -52,6 +57,7 @@ bin/magento module:enable RunAsRoot_MessageQueueRetry
5257
- Delete the message
5358
- Download the message body
5459

60+
---
5561
## How it works
5662

5763
The default Magento's consumer behavior is to reject the message when an exception is thrown during the consumer's execution.
@@ -91,6 +97,8 @@ Is possible to configure the ACL for each action in the grid and the module conf
9197

9298
![img.png](docs/acl.png)
9399

100+
---
101+
94102
### Configuration
95103

96104
Two steps are necessary to configure the retry for a queue:
@@ -195,11 +203,21 @@ System > Configuration > RUN-AS-ROOT > Message Queue Retry
195203

196204
![img.png](docs/module-configuration.png)
197205

206+
---
207+
208+
### Skipping the retry
209+
210+
In case you have a queue configured for retry but there is some scenario that doesn't need the message to be processed again, just add concatenate the `MESSAGE_QUEUE_SKIP_RETRY` string in the exception message. With it the message will not enter in the retry loop.
211+
212+
---
213+
198214
**Important note:** Make sure to configure the retry limit of your queue with the `queue_retry.xml` file and enable the message queue retry configuration.
199215
If you configure the dead letter exchange and do not do the steps mentioned, the message will be in a retry loop. In other words, it will execute until the consumer processes the message without throwing an exception.
200216
This is the default behavior for the RabbitMQ dead letter exchange and will work this way even if this module is not installed.
201217

202218
For more information of how to configure message queues in Magento 2, you can take a look [here](https://developer.adobe.com/commerce/php/development/components/message-queues/configuration/).
203219

220+
---
221+
204222
## License
205223
[MIT](https://opensource.org/licenses/MIT)

src/Plugin/HandleQueueMessageRejectPlugin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function aroundReject(
3535
return;
3636
}
3737

38+
if (str_contains($rejectionMessage, 'MESSAGE_QUEUE_SKIP_RETRY')) {
39+
$proceed($envelope, $requeue, $rejectionMessage);
40+
return;
41+
}
42+
3843
$shouldBeSavedForRetry = $this->isMessageShouldBeSavedForRetryService->execute($envelope);
3944

4045
if (!$shouldBeSavedForRetry) {

src/Test/Unit/Plugin/HandleQueueMessageRejectPluginTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,33 @@ public function testItProceedIfThereIsNoError(): void
9090

9191
self::assertTrue($this->isProceedCalled);
9292
}
93+
94+
/**
95+
* @dataProvider skipRetryDataProvider
96+
*/
97+
public function testItShouldSkipRetryWhenExceptionMessageContainsAnUniqueIdentifier(string $exceptionMessage): void
98+
{
99+
$this->saveFailedMessageServiceMock->expects($this->never())->method('execute');
100+
$this->isMessageShouldBeSavedForRetryServiceMock->expects($this->never())->method('execute');
101+
102+
$this->sut->aroundReject(
103+
$this->queueMock,
104+
$this->testProceedFn,
105+
new Envelope('body'),
106+
false,
107+
$exceptionMessage
108+
);
109+
110+
self::assertTrue($this->isProceedCalled);
111+
}
112+
113+
public function skipRetryDataProvider(): array
114+
{
115+
return [
116+
['Some Error MESSAGE_QUEUE_SKIP_RETRY'],
117+
['MESSAGE_QUEUE_SKIP_RETRY'],
118+
['Some Error MESSAGE_QUEUE_SKIP_RETRY Some Error'],
119+
['Some Error MESSAGE_QUEUE_SKIP_RETRY Some Error MESSAGE_QUEUE_SKIP_RETRY'],
120+
];
121+
}
93122
}

0 commit comments

Comments
 (0)