Anonymous
Зачем повторно обрабатывать, что не увеличивается при брошении на исключение?
Сообщение
Anonymous » 25 сен 2025, 12:05
В Symfony я использую пользовательский сериализатор при транспортировке: < /p>
Код: Выделить всё
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: App\Infrastructure\Messenger\ChannelManagerSerializer
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:
< /code>
Я также сделал этот сериализатор: < /p>
declare(strict_types=1);
namespace App\Infrastructure\Messenger;
use App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\BusNameStamp;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
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),
];
}
}
< /code>
Затем я сделал этот обработчик, эмулирующий ошибку: < /p>
declare(strict_types=1);
namespace App\Application\EventListener;
use App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived;
use Exception;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
class ChannelManagerSubscriber extends BaseEventListener
{
#[AsMessageHandler]
public function onChannelManagerEvent(ChannelManagerEventHasReceived $event): void
{
throw new Exception();
dump($event);
}
}
< /code>
Но запуск очереди: < /p>
php -d memory_limit=-1 bin/console messenger:consume sqs_channel_manager -vv
< /code>
не увеличивает счет: < /p>
12:01:20 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"]
12:01:20 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 930 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 930,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
12:01:21 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"]
12:01:21 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 1005 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1005,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
12:01:24 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"]
12:01:24 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 983 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 983,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
12:01:26 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"]
12:01:26 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 929 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 929,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
12:01:27 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"]
12:01:27 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 1023 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1023,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}]
Как видите, что сбой вызывает "retrycount" => 1 и не будет увеличен. Вы знаете, почему?
Подробнее здесь:
https://stackoverflow.com/questions/797 ... ion-thrown
1758791121
Anonymous
В Symfony я использую пользовательский сериализатор при транспортировке: < /p> [code]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: App\Infrastructure\Messenger\ChannelManagerSerializer 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: < /code> Я также сделал этот сериализатор: < /p> declare(strict_types=1); namespace App\Infrastructure\Messenger; use App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Stamp\BusNameStamp; use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; 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), ]; } } < /code> Затем я сделал этот обработчик, эмулирующий ошибку: < /p> declare(strict_types=1); namespace App\Application\EventListener; use App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived; use Exception; use Symfony\Component\Messenger\Attribute\AsMessageHandler; class ChannelManagerSubscriber extends BaseEventListener { #[AsMessageHandler] public function onChannelManagerEvent(ChannelManagerEventHasReceived $event): void { throw new Exception(); dump($event); } } < /code> Но запуск очереди: < /p> php -d memory_limit=-1 bin/console messenger:consume sqs_channel_manager -vv < /code> не увеличивает счет: < /p> 12:01:20 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"] 12:01:20 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 930 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 930,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 12:01:21 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"] 12:01:21 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 1005 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1005,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 12:01:24 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"] 12:01:24 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 983 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 983,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 12:01:26 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"] 12:01:26 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 929 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 929,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] 12:01:27 INFO [messenger] Received message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived"] 12:01:27 WARNING [messenger] Error thrown while handling message App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived. Sending for retry #1 using 1023 ms delay. Error: "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: " ["class" => "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived","message_id" => null,"retryCount" => 1,"delay" => 1023,"error" => "Handling "App\Domain\Event\ChannelManager\ChannelManagerEventHasReceived" failed: ","exception" => Symfony\Component\Messenger\Exception\HandlerFailedException^ { …}] [/code] Как видите, что сбой вызывает "retrycount" => 1 и не будет увеличен. Вы знаете, почему? Подробнее здесь: [url]https://stackoverflow.com/questions/79774606/why-retry-cound-does-not-increase-upon-exception-thrown[/url]