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
3
Answers
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:
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
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 :
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 :0
messenger-consume_00
ormessenger-consume_01
. (In my config I have numprocs=2)I fixed this problem by adding the following at the top of my
config/packages/messenger.yaml
file: