简体   繁体   English

为 Amazon Beanstalk 上的 Laravel Queue Worker 提供环境变量

[英]Provide environment variables to Laravel Queue Worker on Amazon Beanstalk

So I have this weird issue with mail notifications.所以我对邮件通知有这个奇怪的问题。 I have deployment on Amazon Elastic Beanstalk , and I am using Amazon SQS as queueing service.我在Amazon Elastic Beanstalk上进行了部署,我使用Amazon SQS作为排队服务。 For mail I am using Mailgun .对于邮件,我使用Mailgun Now the problem is when the mail notification is queued and processed it fails.现在的问题是当邮件通知排队并处理它失败时。

And here is the interesting part, when I send an email notification that is not queued, it is sent correctly, and after that the queue emails are sent as well for sometime and then again they start to fail.这是有趣的部分,当我发送未排队的电子邮件通知时,它会正确发送,之后队列电子邮件也会发送一段时间,然后又开始失败。

I've added SerializeModels trait to notifications as well.我还向通知添加了SerializeModels特征。 However it does not work until I send one email that is not queued.但是,在我发送一封未排队的电子邮件之前,它不起作用。

My User class has also properly implemented the method routeNotificationForMail and is returning the user's email.我的User类也正确实现了routeNotificationForMail方法并返回用户的电子邮件。

Anyone faced similar issue?有人遇到过类似的问题吗?

##EDIT ##编辑

So I have drilled it down to where the problem is, the worker process running in systemd is somehow unable to translate or transfer the environment variables from file /opt/elasticbeanstalk/deployment/env .所以我已经深入到问题所在,在systemd中运行的工作进程以某种方式无法从文件/opt/elasticbeanstalk/deployment/env转换或传输环境变量。 Now when I run this in terminal the queue is processing just fine.现在,当我在终端中运行它时,队列处理得很好。 But when the queue worker is restarted nothing works.但是当队列工作器重新启动时,什么都不起作用。

I am using the EnvironmentFile=/opt/elasticbeanstalk/deployment/env in my laravel_worker.service我现在用的是EnvironmentFile=/opt/elasticbeanstalk/deployment/envlaravel_worker.service

Does anyone have any idea how I can go about this?有谁知道我该怎么做?

So after trail and error... banging my head here and there... the solution was pretty simple.所以经过跟踪和错误......在这里和那里敲我的头......解决方案非常简单。

I knew the command to supply environment variables to tinker , so I created a shell script with the following content.我知道向tinker提供环境变量的命令,所以我创建了一个包含以下内容的 shell 脚本。


#!/bin/sh
cd /var/www/html && export $(cat /opt/elasticbeanstalk/deployment/env) && php artisan queue:work

The script file was owned by the root user, so I don't have to use sudo with cat脚本文件归 root 用户所有,所以我不必将sudocat一起使用

Then I executed the script through systemd service.然后我通过systemd服务执行了脚本。

ExecStart=/usr/bin/nohup /var/www/html/worker_script.sh

Through .ebextenstions通过.ebextenstions

files:
    /var/www/html/worker_script.sh:
        mode: "000755"
        owner: root
        content: |
            #!/bin/sh
            cd /var/www/html && export $(cat /opt/elasticbeanstalk/deployment/env) && php artisan queue:work


    /etc/systemd/system/laravel_worker.service:
        mode: "000755"
        owner: root
        group: root
        content: |
            # Laravel queue worker using systemd
            # ----------------------------------
            #
            # /lib/systemd/system/queue.service
            #
            # run this command to enable service:
            # systemctl enable queue.service

            [Unit]
            Description=Laravel queue worker

            [Service]
            EnvironmentFile=/opt/elasticbeanstalk/deployment/env
            Restart=always
            ExecStart=/usr/bin/nohup /var/www/html/worker_script.sh

            [Install]
            WantedBy=multi-user.target


All configuration files are available at https://github.com/stescacom/elastic_beanstalk_laravel_mongo_config Hope this helps someone else.所有配置文件都可以在https://github.com/stescacom/elastic_beanstalk_laravel_mongo_config 找到希望这对其他人有帮助。

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

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