Skip to content
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

bug: When dispatch to Rabbimq failing, Messages get lost #88

Open
notdefine opened this issue Feb 2, 2024 · 0 comments
Open

bug: When dispatch to Rabbimq failing, Messages get lost #88

notdefine opened this issue Feb 2, 2024 · 0 comments

Comments

@notdefine
Copy link
Contributor

We found a constelation, when using RabbitMQ with the transport option

    'messenger' => [
        'transports' => [
             'options' => ['confirm_timeout' => 1]
        ]
]

no retry is made. So we add a middleware to retry this dispatch. This middleware must be added to the command bus configuration.

<?php

declare(strict_types=1);

namespace Mehrkanal\Messenger\Middleware;

use AMQPQueueException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Throwable;

final class ConfirmTimeoutMiddleware implements MiddlewareInterface
{
    public const MAX_PUBLISH_TRIES = 5;

    public function __construct(private LoggerInterface $logger)
    {
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        for ($try = 0; $try < self::MAX_PUBLISH_TRIES; $try++) {
            try {
                return $stack->next()
                    ->handle($envelope, $stack);
            } catch (AMQPQueueException|TransportException $exception) {
                $this->logger->warning(
                    'RabbitMQ publishing retry.',
                    [
                        'class' => self::class,
                        'exception_class' => $exception::class,
                        'exception_message' => $exception->getMessage(),
                        'exception_trace' => $exception->getTrace(),
                        'envelop' => $envelope,
                        'try' => $try,
                    ]
                );
            } catch (Throwable $throwable) {
                $this->logger->critical('RabbitMQ publishing exception.', [
                    'class' => self::class,
                    'envelop' => $envelope,
                    'exception_class' => $throwable::class,
                    'exception_message' => $throwable->getMessage(),
                    'exception_trace' => $throwable->getTrace(),
                ]);
                throw new RuntimeException('Publishing failed', previous: $throwable);
            }
        }
        $this->logger->critical('RabbitMQ publishing failed.', [
            'class' => self::class,
            'envelop' => $envelope,
        ]);
        throw new RuntimeException('Publishing failed');
    }
}

Since this project has no recent activity, and there are no responses to Merge Requests I place my knowlege her. Because we had some trouble with missing Messages.

greetings Thomas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant