Let's see how we can easily setup our Laravel application queue management with Laravel Horizon, Redis & Supervisord.

Laravel Queues

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application.

Pre-requisites

Installing Redis Server

Using PPA for latest version

sudo add-apt-repository -y ppa:chris-lea/redis-server

sudo apt-get update

sudo apt-get install -y redis-server

Test if redis-server is running fine.

redis-cli

Type PING & you’ll receive PONG in response.

Type Exit to exit the CLI.

Configure Redis

we’ll make slight change to start our redis-server on system reboot,

Using your favorite text editor, open up the following file, in my case its nano.

sudo nano /etc/redis/redis.conf

Find supervised section and update its value from no to systemd & safe & close the file.

Run the following command to enable the service.

sudo systemctl enable redis-server.service

To check the status

sudo systemctl status redis-server.service

To Restart the service,

sudo systemctl restart redis-server.service

Installing Supervisor

Follow this guide to install supervisor on your machine & head back here for the next step.

Create Supervisor Config File for Laravel Horizon

Now let’s create a .conf file for our laravel application likeso,

sudo nano /etc/supervisor/conf.d/my-laravel-app.conf

& paste in the following piece of code,

[program:laravel_horizon]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/my-app-name/artisan horizon
autostart=true
autorestart=true
redirect_stderr=true
user=www-data
stdout_logfile=/var/www/my-app-name/storage/horizon.log
stdout_logfile_maxbytes=10MB
logfile_backups=10

you can replace program, and your project path accordingly.

Make sure your project storage folder is writable by www-data (apache) user (same as your web server user).

Save & Close the file.

Now run these commands:

sudo supervisorctl reread

sudo supervisorctl update

Check if Horizon Process is running

sudo supervisorctl

You will see process name with status RUNNING, if not; it means you have misconfigured something.

Access your horizon dashboard at http://app.test/horizon, you can see status as active on dashboard.

Make sure to run these commands after each deployment or changes to horizon config file.

php artisan horizon:purge

php artisan horizon:terminate

php artisan horizon:publish

php artisan queue:restart

Update Laravel Configuration

On our laravel application, we can simply update .env file & set value to QUEUE_CONNECTION likeso,

QUEUE_CONNECTION=redis

Simple Debug

If you face any issue in your queues, simply restart supervisord.

sudo service supervisor restart

or you can restart a specific program as well, likeso,

sudo supervisorctl restart laravel_horizon

More Resources

You may also Like