2

I'm trying to use multiple consumers with the same Redis transport using the Symfony Messenger component. As mentioned in the Symfony guide, we can have problems if we use the same values for stream/group/messenger, cause the same message can be handled by multiple consumers. So I have updated my supervisor config as follow:

environment=MESSENGER_CONSUMER_NAME=%(program_name)s_%(process_num)02d

Then, I have updated my messenger.yaml file as follow:

redis: 
    dsn: '%env(MESSENGER_TRANSPORT_REDIS)%'
    options:
        consumer: '%env(MESSENGER_CONSUMER_NAME)%'

I have reloaded the supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start messenger-consume:*

but I still get the error:

[2021-12-25T18:33:08.954217+01:00] console.CRITICAL: Error thrown while running command "messenger-dispatcher --count=100". Message: "Environment variable not found: "MESSENGER_CONSUMER_NAME"." {"exception":"[object] (Symfony\\Component\\DependencyInjection\\Exception\\EnvNotFoundException(code: 0): Environment variable not found: \"MESSENGER_CONSUMER_NAME\". at /var/www/vendor/symfony/dependency-injection/EnvVarProcessor.php:172)","command":"messenger-dispatcher --count=100","message":"Environment variable not found: \"MESSENGER_CONSUMER_NAME\"."} []

I follow the guidelines but there is something missing somewhere ... but where? Why does my app not read env var?

If I call my consumer:

MESSENGER_CONSUMER_NAME=myconsumer ./bin/console messenger:consume redis

it works as expected; it does not work only with supervisor vars.

Thanks in advance

UPDATE This is the complete section config of my supervisor file:

[program:consumer-redis]
command=php /var/www/bin/console messenger:consume redis --limit=5 --time-limit=3600
user=root
numprocs=6
startsecs=0
autostart=true
autorestart=true
process_name=%(program_name)s_%(process_num)02d
environment=MESSENGER_CONSUMER_NAME=%(program_name)s_%(process_num)02d
Meh Di
  • 93
  • 1
  • 10
GFCoder977
  • 131
  • 1
  • 11
  • And if you put the value of the environment variable in quotes, as is done in the documentation, does the problem remain? `environment=MESSENGER_CONSUMER_NAME="%(program_name)s_%(process_num)02d"` – 7-zete-7 Dec 25 '21 at 17:52
  • mmm ... in the documentation there are no quotes. I try with quotes right now and it still does not work ... – GFCoder977 Dec 25 '21 at 19:21
  • can you share all the config section of your supervisor. The problem is probably there. – zizoujab Dec 25 '21 at 23:21
  • I have update the post with the complete config of my supervisord. Thanks in advance – GFCoder977 Dec 26 '21 at 12:21
  • I see no problem with your config. The only strange thing is how you start your consumers. Shouldn't it be `sudo supervisorctl start consumer-redis:*` ? – zizoujab Dec 26 '21 at 18:42
  • I have got a similar issue with env vars when I was using docker, it was reading vars from my docker Env Vars instead. – Houssem Dec 30 '21 at 22:59

3 Answers3

0

Today I had a similar issue, but you need to explain a little more if my answer match with your problem if not please replay me to delete my answer.

So the problem is, when you run the command:

php /var/www/bin/console messenger:consume redis

Symfony asumes the APP_ENV from 2 parts, if you are using a web server, the variable is taken from the apache or nginx or php/apache.conf file configuration, but in command line if you server has no configured the variable APP_ENV, symfony is going to check the .env file and then .env.local or .env.local.php, so, if that variable doesn't exists, Symfony is not going to take any files like .env.prod.local or .env.prod because is missing that variable. If you are using

0

I found the answer thanks to this article.

Your configuration is all good. You simply need to add an environment variable with a default value, so that Symfony doesn't generate errors :

# .env.local
MESSENGER_CONSUMER_NAME=0

This env variable will be overwritten in processes ran by supervisor. To test it, I simply log $_ENV['MESSENGER_CONSUMER_NAME'] in a function. Here's what I get when I call it :

  • Not using Messenger (synchronously) : 0
  • Using Messenger : either messenger-consume_00 or messenger-consume_01. (In my config I have numprocs=2)
Victor Weiss
  • 366
  • 3
  • 5
0

I fixed this problem by adding the following at the top of my config/packages/messenger.yaml file:

parameters:
    env(MESSENGER_CONSUMER_NAME): '00'
lfjeff
  • 1,840
  • 2
  • 17
  • 19