How to keep Laravel Queue system running on server

后端 未结 17 948
小鲜肉
小鲜肉 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

    0 讨论(0)
  • 2020-11-28 02:59

    You can also use Docker containers.

    checkout:

    • Docker + Laravel queue:work
    • https://laravel-news.com/laravel-scheduler-queue-docker
    0 讨论(0)
  • 2020-11-28 03:03

    1)sudo apt install supervisor or

    sudo apt-get install supervisor
    

    2)cd /etc/supervisor/conf.d 3)create new file inside

    sudo vim queue-worker.conf
    

    File Content

    [program:email-queue]
    process_name=%(program_name)s_%(process_num)02d
    command=php /var/www/html/laravelproject/artisan queue:work
    autostart=true
    autorestart=true
    user=root
    numprocs=2
    redirect_stderr=true
    stdout_logfile=/var/www/html/laravelproject/storage/logs/supervisord.log
    

    4)sudo supervisorctl reread

    when run this command get output queue-worker:available

    5)sudo supervisorctl update

    when run this command get output queue-worker:added process group

    other command

    1)sudo supervisorctl reload

    when run this command get output Restarted supervisord

    2)sudo service supervisor restart

    0 讨论(0)
  • 2020-11-28 03:04

    I simply used php artisan queue:work --tries=3 & which keeps the process running in the background. But it sometimes stops. I don't know why this is happening

    Edit

    I solved this issue by using supervisor. Put a supervisor script that runs this php script, and that will run every time the server runs

    0 讨论(0)
  • 2020-11-28 03:09

    Using pm2

    I had JS script running with pm2 (Advanced, production process manager for Node.js) Which was the only one I was running. But now as I got one more process to keep running.

    I created process.yml to run both with a single command. Check the first one would run php artisan queue: listen

    # process.yml at /var/www/ which is root dir of the project
    apps:
      # Run php artisan queue:listen to execute queue job
      - script    : 'artisan'
        name      : 'artisan-queue-listen'
        cwd       : '/var/www/'
        args      : 'queue:listen' # or queue:work
        interpreter : 'php'
    
      # same way add any other script if any.
    

    Now run:

    > sudo pm2 start process.yml
    

    Check more options and feature of pm2

    0 讨论(0)
  • 2020-11-28 03:10

    You can use monit tool. it's very small and useful for any type of process management and monitoring.

    After Downloading binary package from this link, you can extract it to a folder on your system and then copy two files from the package to your system to install it:

    cd /path/to/monit/folder
    cp ./bin/monit /usr/sbin/monit
    cp ./conf/monitrc /etc/monitrc  
    

    Now edit /etc/monitrc base on your needs(reference doc). then create a init control file to enable monit on startup. now start monit like this:

    initctl reload-configuration
    start monit
    
    0 讨论(0)
提交回复
热议问题