Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Generated redis transport consumer name #51604

Open
@danielburger1337

Description

@danielburger1337

Description

As you may now, the redis transport requires that each worker needs to have a unique connection consumer name. The suggested and very easy solution is to use the HOSTNAME env variable. This works perfectly in production where we use containerization via Docker, but there is a small issue when developing locally and not inside a devcontainer:

When developing locally in my WSL instance, for some reason the HOSTNAME env variable is empty when I try to use it via the '%env(HOSTNAME)%' notation in the Yaml configuration, even though the env variable does exist.

A tiny quality of life improvement would be to allow setting the consumer name to a generated random value.
AFAIK the consumer names dont have to be stable, so this would simplify configuration quite a bit when having multiple workers work on a single queue or multiple queues.

Example

# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
                options:
                    consumer: _RANDOM

and then

// Symfony\Component\Messenger\Bridge\Redis\Transport\Connection

public function __construct(array $options, \Redis|Relay|\RedisCluster $redis = null)
{
    // ...
    $this->consumer = $options['consumer'] === '_RANDOM' ? bin2hex(random_bytes(8)) : $options['consumer'];
    // ...
}

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