Docker + Laravel队列:工作 [英] Docker + Laravel queue:work
问题描述
在容器启动并运行后,我正在尝试运行以下命令.
I'm trying to run the following command after the container is up and running.
php artisan queue:work -n -q &
&"在那里是因为不赞成使用daemon选项,后来又将其从Laravel中删除了.
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", "&"]
我应该如何以Docker方式做到这一点?
How should I do this in a Docker way?
使用docker-compose,我将此行添加到了docker-compose.yml文件中
Using docker-compose I added this line to my docker-compose.yml file
command: bash -c 'php artisan queue:work -n -q;'
容器已启动,但未满足任何请求:S
The container started but did not serve any requests :S
使用此:
command: bash -c 'php artisan queue:work -n -q &; echo "runs"; tail -f /dev/null'
容器在启动后停止了
最终解决方案
所以最后我想也许负责交付应用程序的服务器不应该是运行队列的服务器.
So in the end I thought that maybe the server in charge of delivering the app should not be the one running the queue.
因此,出于运行artisan queue:work的唯一目的,我启动了同一docker映像的另一个实例.
Therefore I spin up another instance of the same docker image with the sole purpose of running artisan queue:work.
推荐答案
queue:work命令在前台运行,因此您应该以这种方式运行它,以使容器不会立即退出.
The queue:work command runs in the foreground, so you should run it that way so the container doesn't exit immediately.
由于Laravel中的应用程序代码与作为Web应用程序,队列或调度程序运行容器的代码相同,因此我构建了一个可以在这些上下文中使用的映像.我使用带有环境变量的bash启动脚本来定义容器角色,这就是我为队列工作器容器运行的内容:
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
还要注意无限的while循环和睡眠组合,以使调度程序角色在后台运行并在运行时运行schedule:run命令,以防调度程序重叠(因为它们需要每分钟运行一次,而不管最后一个是否完成)
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).
这篇关于Docker + Laravel队列:工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!