Skip to content

Commit 339882c

Browse files
Merge pull request #18 from run-as-root/develop
Improvements - Replace Rewrite by Around Plugin on the message reject function - Fix Retries Count - Refactory - Removes the unused PHPUnit test coverage pipeline
2 parents f618129 + 606669c commit 339882c

20 files changed

+469
-586
lines changed

.github/workflows/test_extension.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,3 @@ jobs:
8080

8181
- name: PHP Unit
8282
run: composer test
83-
84-
- name: phpunit-coverage-badge
85-
uses: timkrase/phpunit-coverage-badge@v1.2.1
86-
with:
87-
report: reports/test-reports/clover.xml
88-
coverage_badge_path: output/coverage.svg
89-
push_badge: true
90-
commit_message: "Update coverage badge"
91-
repo_token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# run-as-root/magento2-message-queue-retry
77

8-
It gives the possibility to process the same queue message more than once,
8+
It gives the possibility to process the same queue message more than once,
99
utilizing The RabbitMQ's [dead letter exchange](https://www.rabbitmq.com/dlx.html) feature.
1010

1111
## Table of Contents
@@ -25,7 +25,7 @@ utilizing The RabbitMQ's [dead letter exchange](https://www.rabbitmq.com/dlx.htm
2525
To be able to use this module, you have to manually configure the dead letter exchange(s) for the queue(s) you want to enable the retry mechanismm through the `queue_topology.xml` file.
2626
An example will be given in the [Configuration](#configuration) section.
2727

28-
Other requisite is that your exchanges have to have a relation from one exchange to only one topic and queue,
28+
Other requisite is that your exchanges have to have a relation from one exchange to only one topic and queue,
2929

3030
For example:
3131

@@ -95,7 +95,7 @@ Is possible to configure the ACL for each action in the grid and the module conf
9595

9696
Two steps are necessary to configure the retry for a queue:
9797
1. Configure the dead letter exchange
98-
1. Enable the message queue retry and delclare the retry limit configuration
98+
2. Enable the message queue retry and declare the retry limit configuration
9999

100100
Let's imagine a scenario that the `erp_order_export` queue already exists in your project and to simplify the example the topic name, exchange name and queue name are the same: `erp_order_export`.
101101

@@ -178,7 +178,7 @@ System > Configuration > RUN-AS-ROOT > Message Queue Retry
178178

179179
![img.png](docs/configuration.png)
180180

181-
Note that if the queue is not declared in the configuration it will the default Magento consumer behavior.
181+
**Important note:** Make sure to configure the retry limit of your queue in the module configuration. If you configure the dead letter exchange and do not set the retry limit in the configuration(System > Configuration > RUN-AS-ROOT > Message Queue Retry), the message will be in a retry loop, that is, execute until the consumer process the message without throwing an exception. This is the default behavior for the RabbitMQ dead letter exchange and will work this way even if this module is not installed.
182182

183183
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/).
184184

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
"sniffer": "vendor/bin/phpcs --colors -p ./src --standard=phpcs-ruleset.xml",
6464
"fix-style": "vendor/bin/phpcbf --colors -p ./src --standard=phpcs-ruleset.xml",
6565
"sniffer:php8.1": "vendor/bin/phpcs -p ./src --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.1",
66-
"mess-detector": "vendor/bin/phpmd src html phpmd-ruleset.xml --exclude \"Test,src/Queue/Consumer.php\" --strict --reportfile reports/phpmd/phpmd.html"
66+
"mess-detector": "vendor/bin/phpmd src html phpmd-ruleset.xml --exclude \"Test\" --strict --reportfile reports/phpmd/phpmd.html"
6767
}
6868
}

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ parameters:
55
excludePaths:
66
analyseAndScan:
77
- src/Test
8-
- src/Queue/Consumer.php
98
ignoreErrors:
109
- '#Method .*construct\(\) has parameter \$data with no value type specified in iterable type array#'
1110

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunAsRoot\MessageQueueRetry\Plugin;
6+
7+
use JsonException;
8+
use Magento\Framework\MessageQueue\EnvelopeInterface;
9+
use Magento\Framework\MessageQueue\QueueInterface;
10+
use RunAsRoot\MessageQueueRetry\Exception\MessageCouldNotBeCreatedException;
11+
use RunAsRoot\MessageQueueRetry\Service\IsMessageShouldBeSavedForRetryService;
12+
use RunAsRoot\MessageQueueRetry\Service\SaveFailedMessageService;
13+
14+
class HandleQueueMessageRejectPlugin
15+
{
16+
public function __construct(
17+
private IsMessageShouldBeSavedForRetryService $isMessageShouldBeSavedForRetryService,
18+
private SaveFailedMessageService $saveFailedMessageService
19+
) {
20+
}
21+
22+
/**
23+
* @throws MessageCouldNotBeCreatedException
24+
* @throws JsonException
25+
*/
26+
public function aroundReject(
27+
QueueInterface $subject,
28+
callable $proceed,
29+
EnvelopeInterface $envelope,
30+
bool $requeue,
31+
string $error
32+
): void {
33+
if (!$error) {
34+
$proceed($envelope, $requeue, $error);
35+
return;
36+
}
37+
38+
$shouldBeSavedForRetry = $this->isMessageShouldBeSavedForRetryService->execute($envelope);
39+
40+
if (!$shouldBeSavedForRetry) {
41+
$proceed($envelope, $requeue, $error);
42+
return;
43+
}
44+
45+
$this->saveFailedMessageService->execute($envelope, $error);
46+
$subject->acknowledge($envelope);
47+
}
48+
}

src/Queue/Consumer.php

Lines changed: 0 additions & 181 deletions
This file was deleted.

src/Repository/Command/DeleteQueueLockByIdCommand.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Repository/QueueLockRepository.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunAsRoot\MessageQueueRetry\Service;
6+
7+
use Magento\Framework\MessageQueue\EnvelopeInterface;
8+
use PhpAmqpLib\Wire\AMQPTable;
9+
10+
class GetMessageRetriesCountService
11+
{
12+
public function execute(EnvelopeInterface $message): int
13+
{
14+
$messageProperties = $message->getProperties();
15+
$applicationHeaders = $messageProperties['application_headers'] ?? null;
16+
17+
// If there are no application headers, then it is the first time the message has been processed.
18+
if (!$applicationHeaders instanceof AMQPTable) {
19+
return 0;
20+
}
21+
22+
if (isset($applicationHeaders->getNativeData()['x-death'][0]['count'])) {
23+
return $applicationHeaders->getNativeData()['x-death'][0]['count'];
24+
}
25+
26+
return 0;
27+
}
28+
}

0 commit comments

Comments
 (0)