How to keep Laravel Queue system running on server

后端 未结 17 1041
小鲜肉
小鲜肉 2020-11-28 02:23

I recently setup a Laravel Queue system. The basics are a cronjob calls a command which adds jobs to a queue and calls a second command which sends an email.

The sy

17条回答
  •  盖世英雄少女心
    2020-11-28 02:57

    Installing Supervisor

    sudo apt-get install supervisor
    

    Configuring Supervisor

    step 1 : goto /etc/supervisor/conf.d directory

    cd /etc/supervisor/conf.d
    

    step 2 : create a worker file laravel-worker.conf that will listen queue

    sudo nano laravel-worker.conf
    

    *Note : Now assuming that your laravel app is inside /var/www/html directory

    project folder is : /var/www/html/LaravelApp
    

    step 3 : paste below code in laravel-worker.conf and save file

    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /var/www/html/LaravelApp/artisan queue:listen redis --queue=default --sleep=3 --tries=3 
    autostart=true
    autorestart=true
    user=root
    numprocs=8
    redirect_stderr=true
    stdout_logfile= /var/www/html/LaravelApp/storage/logs/worker.log
    

    *Note : Here is assumed that you are using redis for queue connection

    in .env file QUEUE_CONNECTION=redis

    command=php /var/www/html/LaravelApp/artisan queue:listen redis
    

    if you are using other connection then , general syntax will be :

    command= php [project_folder_path]/artisan queue:listen [connection_name]
    

    [connection_name] can be any of sync , database , beanstalkd , sqs , redis

    step 4 : create a worker file laravel-schedule.conf that will run artisan schedule:run command at every 1 minute (60 seconds) (*you can change it as per your requirement)

    [program:laravel-schedule]
    process_name=%(program_name)s_%(process_num)02d
    command=/bin/bash -c 'while true; do date && php /var/www/html/LaravelApp/artisan schedule:run; sleep 60; done'
    autostart=true
    autorestart=true
    numprocs=1
    stdout_logfile=/dev/stdout
    stdout_logfile_maxbytes=0
    

    step 5 : Starting Supervisor : run below commands

    sudo supervisorctl reread
    
    sudo supervisorctl update
    
    sudo supervisorctl start all
    

    *Note : Whenever you make changes in any of configuration .conf files , run above commands of Step 5

    Extra usefull information :

    • to stop all supervisorctl process : sudo supervisorctl stop all
    • to restart all supervisorctl process : sudo supervisorctl restart all

    usefull links :

    https://laravel.com/docs/5.8/queues#running-the-queue-worker

    http://supervisord.org/index.html

提交回复
热议问题