Docker + Laravel queue:work

后端 未结 4 1231
耶瑟儿~
耶瑟儿~ 2021-01-02 06:57

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

php artisan queue:work -n -q &

The \"&\" is there b

4条回答
  •  一个人的身影
    2021-01-02 07:30

    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).

提交回复
热议问题