Skip to content

Problem with defining Doctrine middleware for different connections in Messenger #37568

@X-Coder264

Description

@X-Coder264

Symfony version(s) affected: 4.4.9

Description
I have multiple connections in my app so I've added the Doctrine close and ping connection middleware to my Messenger bus like this:

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - doctrine_ping_connection: ['main']
                    - doctrine_ping_connection: ['stats']
                    - doctrine_ping_connection: ['logs']
                    - doctrine_close_connection: ['main']
                    - doctrine_close_connection: ['stats']
                    - doctrine_close_connection: ['logs']

After deploying this to production we started getting MySQL server has gone away errors. Turns out that instead of the three connections being closed once the last connection (logs) was closed three times.

The problem is in the MessengerPass -> https://github.com/symfony/symfony/blob/v4.4.9/src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php#L318-L334 because it doesn't take arguments into account to create an unique middleware service ID.

The documentation about middleware is really scarce (https://symfony.com/doc/current/messenger.html#middleware-for-doctrine) so even though this is not documented I've expected that this would work.

As a temporary workaround I've registered new services for this:

framework:
    messenger:
        buses:
            messenger.bus.default:
                middleware:
                    - messenger.middleware.doctrine_ping_connection_main
                    - messenger.middleware.doctrine_ping_connection_stats
                    - messenger.middleware.doctrine_ping_connection_logs
                    - messenger.middleware.doctrine_close_connection_main
                    - messenger.middleware.doctrine_close_connection_stats
                    - messenger.middleware.doctrine_close_connection_logs

    # in services.yaml
    messenger.middleware.doctrine_ping_connection_main:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'main'

    messenger.middleware.doctrine_ping_connection_stats:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'stats'

    messenger.middleware.doctrine_ping_connection_logs:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware
        arguments:
            $entityManagerName: 'logs'

    messenger.middleware.doctrine_close_connection_main:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'main'

    messenger.middleware.doctrine_close_connection_stats:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'stats'

    messenger.middleware.doctrine_close_connection_logs:
        class: Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware
        arguments:
            $entityManagerName: 'logs'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions