How do I run a Laravel database queue on Elastic Beanstalk?

982 Views Asked by At

I am new to AWS EB but was able to set up a PHP 8.0 running on 64bit Amazon Linux 2/3.3.9 Laravel API.

I have some jobs in a database queue that I'd like to run using php artisan queue:listen. I followed this Stack Exchange question yet when I push my code, the script doesn't run as the jobs aren't executed.

I am able to manually SSH into the EC2 instance and set the env variables manually in the /opt/elasticbeanstalk/deployment/env using this question. Then when I run php artisan queue:listen, it works!

How can I set up my scripts so they run when the EC2/EB instance starts up and the jobs on the queue are run?

PS: My env config is working and database config works as well

Here is my current script setup:

./.ebextensions/01-setup.config

container_commands:
    01-no_dev:
        command: "composer.phar install --optimize-autoloader --no-dev"
    02-config_clear:
        command: "php artisan config:clear"
    03-view_clear:
        command: "php artisan view:clear"
    04-route_cache:
        command: "php artisan route:cache"
    05-view_cache:
        command: "php artisan view:cache"
    06-migrate:
        command: "php artisan migrate --force"
        leader_only: true
    07-queue_service_restart:
        command: "systemctl restart laravel_worker"
files:
    /opt/elasticbeanstalk/tasks/taillogs.d/laravel-logs.conf:
        content: /var/app/current/storage/logs/laravel.log
        group: root
        mode: "000755"
        owner: root
    /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]
            User=nginx
            Group=nginx
            Restart=always
            ExecStart=/usr/bin/nohup /usr/bin/php /var/www/html/artisan queue:work //is this line correct???

            [Install]
            WantedBy=multi-user.target

\.platform\nginx\conf.d\elasticbeanstalk\laravel.conf

location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}

./.ebextensions/cron-linux.config

files:
    "/etc/cron.d/schedule_run":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root . /opt/elasticbeanstalk/deployment/env && /usr/bin/php /var/app/current/artisan schedule:run 1>> /dev/null 2>&1

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/*.bak"

1

There are 1 best solutions below

0
kris gjika On

If your script/application depends on variables that are not in .env but are loaded as os environment variables, your cron won't have access to them by default.

Unless you export those with: env >> /etc/environment before booting cron.