Docker + Laravel queue:work

落爺英雄遲暮 提交于 2019-12-07 10:39:08

问题


I'm trying to run the following command after the container is up and running.

php artisan queue:work -n -q &

The "&" is there because the daemon option was deprecated and later removed from Laravel.

However, this breaks my container startup completely.

CMD ["php", "artisan", "queue:work", "-n", "-q", "&"]

How should I do this in a Docker way?

EDIT:

Using docker-compose I added this line to my docker-compose.yml file

command: bash -c 'php artisan queue:work -n -q;'

The container started but did not serve any requests :S

Using this:

command: bash -c 'php artisan queue:work -n -q &; echo "runs"; tail -f /dev/null'

The container stopped after starting up

Final solution

So in the end I thought that maybe the server in charge of delivering the app should not be the one running the queue.

Therefore I spin up another instance of the same docker image with the sole purpose of running artisan queue:work.


回答1:


You can't run the queue in the background, otherwise the container stops, as the command has effectively finished. Remove the & and it will stay alive.

However, you can do something like tail -f /dev/null as the final command to keep the container running if you want to run the queue in the background and still be able to attach to the container and access the shell.




回答2:


The queue:work command runs in the foreground, so you should run it that way so the container doesn't exit immediately.

Since the application code in Laravel is the same for running a container as a web application, queue, or scheduler I build one image that I can use in these contexts. I use a bash start script with an environment variable to define a container role, and this is what I run for a queue worker container:

#!/bin/bash

# Defaults to an app server
role=${CONTAINER_ROLE:-app}

if [ "$role" = "queue" ]; then
    # Run queue
    php artisan queue:work --verbose --tries=3 --timeout=90
elif [ "$role" = "app" ]; then
    # Run the web application
    /usr/bin/caddy --agree=true --conf=/etc/Caddyfile
elif [ "$role" = "scheduler" ]; then
    while [ true ]
    do
      php artisan schedule:run --verbose --no-interaction &
      sleep 60
    done
else
    echo "Could not match the container role...."
    exit 1
fi

Also note the infinite while loop and sleep combo to keep the scheduler role running and running the schedule:run command in the background in case the scheduler runs overlap (since they need to run every minute regardless of if the last one finished).




回答3:


use supervisor

apt-get install supervisor

cd /etc/supervisor/conf.d && sudo nano laravel-worker.conf

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec php php /path/to/artisan queue:work redis --sleep=3 --tries=5
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/logs/worker.log


来源:https://stackoverflow.com/questions/48884802/docker-laravel-queuework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!