In Symfony I use a custom Serializer upon a transport:
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
failed: 'doctrine://default?table_name=failed_messages'
sqs_channel_manager:
dsn: '%env(SQS_CHANNEL_MANAGER_TRANSPORT_DSN)%'
serializer: AppInfrastructureMessengerChannelManagerSerializer
options:
access_key: '%env(AWS_ACCESS_KEY_ID)%'
secret_key: '%env(AWS_SECRET_ACCESS_KEY)%'
region: '%env(AWS_REGION)%'
queue_name: '%env(CHANNEL_MANAGER_QUEUE_NAME)%'
failure_transport: failed
default_bus: command.bus
buses:
event.bus: ~
routing:
I also made this Serializer:
declare(strict_types=1);
namespace AppInfrastructureMessenger;
use AppDomainEventChannelManagerChannelManagerEventHasReceived;
use SymfonyComponentMessengerEnvelope;
use SymfonyComponentMessengerStampBusNameStamp;
use SymfonyComponentMessengerTransportSerializationSerializerInterface;
class ChannelManagerSerializer implements SerializerInterface
{
public function decode(array $encodedEnvelope): Envelope
{
$data = json_decode($encodedEnvelope['body'], true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidArgumentException(
'Invalid JSON received from SQS: ' . json_last_error_msg()
);
}
return (new Envelope(new ChannelManagerEventHasReceived($data)))
->with(new BusNameStamp('event.bus'));
}
public function encode(Envelope $envelope): array
{
$event = $envelope->getMessage();
return [
'body' => json_encode($event->channelManagerData, JSON_THROW_ON_ERROR),
];
}
}
Then I made this handler emulating an error:
declare(strict_types=1);
namespace AppApplicationEventListener;
use AppDomainEventChannelManagerChannelManagerEventHasReceived;
use Exception;
use SymfonyComponentMessengerAttributeAsMessageHandler;
class ChannelManagerSubscriber extends BaseEventListener
{
#[AsMessageHandler]
public function onChannelManagerEvent(ChannelManagerEventHasReceived $event): void
{
throw new Exception();
dump($event);
}
}
But running the queue:
php -d memory_limit=-1 bin/console messenger:consume sqs_channel_manager -vv
Does not increase the count:
12:01:20 INFO [messenger] Received message AppDomainEventChannelManagerChannelManagerEventHasReceived ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived"]
12:01:20 WARNING [messenger] Error thrown while handling message AppDomainEventChannelManagerChannelManagerEventHasReceived. Sending for retry #1 using 930 ms delay. Error: "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: " ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 930,"error" => "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: ","exception" => SymfonyComponentMessengerExceptionHandlerFailedException^ { …}]
12:01:21 INFO [messenger] Received message AppDomainEventChannelManagerChannelManagerEventHasReceived ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived"]
12:01:21 WARNING [messenger] Error thrown while handling message AppDomainEventChannelManagerChannelManagerEventHasReceived. Sending for retry #1 using 1005 ms delay. Error: "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: " ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1005,"error" => "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: ","exception" => SymfonyComponentMessengerExceptionHandlerFailedException^ { …}]
12:01:24 INFO [messenger] Received message AppDomainEventChannelManagerChannelManagerEventHasReceived ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived"]
12:01:24 WARNING [messenger] Error thrown while handling message AppDomainEventChannelManagerChannelManagerEventHasReceived. Sending for retry #1 using 983 ms delay. Error: "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: " ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 983,"error" => "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: ","exception" => SymfonyComponentMessengerExceptionHandlerFailedException^ { …}]
12:01:26 INFO [messenger] Received message AppDomainEventChannelManagerChannelManagerEventHasReceived ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived"]
12:01:26 WARNING [messenger] Error thrown while handling message AppDomainEventChannelManagerChannelManagerEventHasReceived. Sending for retry #1 using 929 ms delay. Error: "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: " ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 929,"error" => "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: ","exception" => SymfonyComponentMessengerExceptionHandlerFailedException^ { …}]
12:01:27 INFO [messenger] Received message AppDomainEventChannelManagerChannelManagerEventHasReceived ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived"]
12:01:27 WARNING [messenger] Error thrown while handling message AppDomainEventChannelManagerChannelManagerEventHasReceived. Sending for retry #1 using 1023 ms delay. Error: "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: " ["class" => "AppDomainEventChannelManagerChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1023,"error" => "Handling "AppDomainEventChannelManagerChannelManagerEventHasReceived" failed: ","exception" => SymfonyComponentMessengerExceptionHandlerFailedException^ { …}]
As you can see the failure causes "retryCount" => 1
and not be increased. Do you know why?