L
L
lazari242021-04-11 17:07:05
symfony
lazari24, 2021-04-11 17:07:05

Why are entities processed in Symfony Messenger not flushed?

Hello. I'm having a problem while working with queues in symphony. Queues use the Symfony Messenger + RabbitMQ bundle.
Essence of the question:

bus:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                serializer: messenger.transport.symfony_serializer
                options:
                    exchange:
                        name: bus
                        type: direct
                        default_publish_routing_key: create
                    queues:
                        create:
                            binding_keys: [ create ]
                        update:
                            binding_keys: [ update ]


There are 2 queues for one consumer - creation and updating of the entity. The problem is that if I first create an entity persist + flush (Everything is ok, it is saved to the database), and then a message arrives to edit the same entity, I change a couple of fields for it and after the flush, the changes are not saved to the database. However, if you call the repository in the handler of the same message, then the value is already correct, which means that the EntityManager still sees the changes and saves them in its memory. But with a flash it does not write to the database.
If you restart the consumer, then the message for editing is processed correctly.
I tried to write my own mildweir to clear EM but it didn't help.

class DoctrineEntityManagerClearMiddleware implements MiddlewareInterface
{
    private ManagerRegistry $managerRegistry;
    private ?string $entityManagerName;

    public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
    {
        $this->managerRegistry = $managerRegistry;
        $this->entityManagerName = $entityManagerName;
    }

    public function handle(Envelope $envelope, StackInterface $stack): Envelope
    {
        try {
            $entityManager = $this->managerRegistry->getManager($this->entityManagerName);
        } catch (\InvalidArgumentException $e) {
            throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
        }

        $entityManager->clear();

        return $stack->next()->handle($envelope, $stack);
    }
}


buses:
            command.bus:
                middleware:
                    - doctrine_ping_connection
                    - doctrine_close_connection
                    - 'App\Middleware\DoctrineEntityManagerClearMiddleware'


Please help me solve this problem :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question