Docker + Laravel queue:work

后端 未结 4 1200
耶瑟儿~
耶瑟儿~ 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:15

    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
    
    0 讨论(0)
  • 2021-01-02 07:25

    If you need graceful shutdown for queue, you can follow this.

    This is taken from @Paul Redmond's article at Laravel News and extending his docker-entrypoint file so suite my need. After a lot of testing for graceful shutdown I finally was able to do.

    First thing in docker-compose.yml file set stop_signal: SIGTERM for your queue service.

      queue:
        image: laravel-www
        container_name: laravel-queue
        stop_signal: SIGTERM
        depends_on:
          - app
        volumes:
          - .:/var/www/html
        ...
    

    Next in the entrypoint.sh file, the main thing is to run the queue:work using the exec command.

    #!/usr/bin/env bash
    
    set -e
    
    # Run our defined exec if args empty
    if [ -z "$1" ]; then
        role=${CONTAINER_ROLE:-app}
        env=${APP_ENV:-production}
    
        if [ "$env" != "local" ]; then
            echo "Caching configuration..."
            (cd /var/www/html && php artisan cache:clear && php artisan config:clear && php artisan route:clear && php artisan view:clear)
            (cd /var/www/html && php artisan config:cache && php artisan event:cache && php artisan route:cache && php artisan view:cache)
        fi
    
        if [ "$role" = "app" ]; then
    
            echo "Running PHP-FPM..."
            exec php-fpm
    
        elif [ "$role" = "queue" ]; then
    
            echo "Running the queue..."
            exec php /var/www/html/artisan queue:work -vv --no-interaction --tries=3 --sleep=5 --timeout=300 --delay=10
    
        elif [ "$role" = "cron" ]; then
    
            echo "Running the cron..."
            while [ true ]
            do
              exec php /var/www/html/artisan schedule:run -vv --no-interaction
              sleep 60
            done
    
        else
            echo "Could not match the container role \"$role\""
            exit 1
        fi
    
    else
        exec "$@"
    fi
    

    Your are done. Next time you stop queue service, it will stop gracefully and won't wait 10 seconds for SIGKILL. I think it has to do with the PID 1 thing.

    0 讨论(0)
  • 2021-01-02 07:26

    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.

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题