简体   繁体   English

主管环境变量不适用于 Symfony Messenger

[英]Supervisor env var not working with Symfony Messenger

I'm trying to use multiple consumers with the same Redis transport using the Symfony Messenger component.我正在尝试使用 Symfony Messenger 组件使用具有相同 Redis 传输的多个消费者。 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.正如 Symfony 指南中所述,如果我们对流/组/信使使用相同的值,我们可能会遇到问题,因为相同的消息可以由多个消费者处理。 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:然后,我更新了我的 messenger.yaml 文件如下:

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?为什么我的应用程序不读取 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.它不仅适用于主管 var。

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

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. 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将检查 .env 文件,然后检查 .env.local 或 .env.local.php,因此,如果该变量不存在,则 Symfony 不会获取任何文件,例如 .env.prod.local 或 .env。 prod 因为缺少该变量。 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:您只需添加一个具有默认值的环境变量,这样 Symfony 就不会产生错误:

# .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.为了测试它,我只需在 function 中记录$_ENV['MESSENGER_CONSUMER_NAME'] Here's what I get when I call it:这是我调用它时得到的:

  • Not using Messenger (synchronously) : 0不使用 Messenger (同步)0
  • Using Messenger: either messenger-consume_00 or messenger-consume_01 .使用 Messenger: messenger-consume_00messenger-consume_01 (In my config I have numprocs=2) (在我的配置中我有 numprocs=2)

I fixed this problem by adding the following at the top of my config/packages/messenger.yaml file:我通过在config/packages/messenger.yaml文件顶部添加以下内容来解决此问题:

parameters:
    env(MESSENGER_CONSUMER_NAME): '00'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM