How to Set up Laravel Horizon Queueing in Rocky Linux 8

Laravel Horizon is an open source dashboard app that keeps track of Laravel Redis queues. The Horizon dashboard is a single page application built using Vue.js. The application is designed to provide real-time insights into queue workloads, recent jobs, failed jobs, job retries, throughput and runtime metrics, and process counts. The Dashboard provides several statistical data on the execution times, throughput or failure of the processes involved, sending notifications if errors occur.

Laravel Horizon has an excellent code-driven setup and user interface dashboard for your Laravel powered Redis queues. Horizon permits you to effortlessly monitor key metrics of your queue framework like runtime, and work failures.

Also check:

Table of Content

  1. Ensure that the server packages are up to date
  2. Disable Selinux
  3. Installing Redis
  4. Install PHP 7.4

1. Ensure that the server packages are up to date

Let us make sure that our server packages are up to date with this command:

sudo dnf -y update

Install common packages that we will need

sudo dnf install -y git unzip vim

Also configure the timezone

sudo timedatectl set-timezone Africa/Nairobi

2. Disable SELinux

Next, let us disable SELinux. We are going to set SELinux to permisive. Edit this file:

sudo vim /etc/selinux/config

Then update this line:

SELINUX=permisive

To ensure that the changes are applied immediately without reboot, use this command:

setenforce 0

3. Install Redis

Redis is used for queue management. It is necessary for Horizon to work as expected. Install redis using this command:

sudo dnf install -y redis

Redis is not started by default. To be able to use it, it has to be running. Start redis using this command:

sudo systemctl start redis

Confirm that it is working as expected by checking the status:

$ sudo systemctl status redis
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since Thu 2022-01-13 12:26:21 UTC; 18min ago
 Main PID: 55173 (redis-server)
    Tasks: 4 (limit: 23176)
   Memory: 6.5M
   CGroup: /system.slice/redis.service
           └─55173 /usr/bin/redis-server 127.0.0.1:6379

Jan 13 12:26:21 dev-rockysrv.inv.re systemd[1]: Starting Redis persistent key-value database...
Jan 13 12:26:21 dev-rockysrv.inv.re systemd[1]: Started Redis persistent key-value database.

4. Install PHP 7.4

The default php version in Rocky Linux might be outdated. To have control over the php version, let us install the remi release repo using this command:

sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Enable version 7.4 of php

sudo dnf module enable -y php:remi-7.4

You can confirm that the right version is enabled using this command:

sudo dnf module list php

Then install php and dependancies

sudo dnf install -y php \
    php-cli \
    php-common \
    php-fpm \
    php-intl \
    php-gd \
    php-zip \
    php-process \
    php-redis

Check php version

$ php -v
PHP 7.4.27 (cli) (built: Dec 14 2021 17:17:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

For more info on setting up PHP and Nginx, check out this guide How to Install and set up PHP and Nginx (LEMP) on Rocky Linux/Alma Linux 8.

5. Install php Composer

Composer is used for dependency management

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Move the downloaded phar file to bin

sudo mv composer.phar /usr/local/bin/composer

Confirm that composer is working fine using this command:

$ composer --version
Composer version 2.2.4 2022-01-08 12:30:42

6. Creating a laravel project

Laravel horizon works with a laravel project. If you have no project to work with, let us create one for this guide. Use this command:

composer create-project --prefer-dist laravel/laravel myblog

Change to the created project

cd myblog

7. Install Horizon

Let us add horizon to our project. We will use the composer package manager. Use this command:

composer require laravel/horizon

Next let us publish horizon assets

php artisan horizon:install

To ensure that the redis client is being used, install the package predis:

composer require predis/predis

Finally, confirm that the database.php configuration has settings for the redis client connection:

'client' => env('REDIS_CLIENT', 'predis'),

7. Horizon options

Horizon configurations are stored in the file config/horizon.php. These are the the default settings of the queue management:

'defaults' => [
    'supervisor-1' => [
        'connection' => 'redis',
        'queue' => ['default'],
        'balance' => 'auto',
        'maxProcesses' => 1,
        'memory' => 128,
        'tries' => 1,
        'nice' => 0,
    ],
],

'environments' => [
    'production' => [
        'supervisor-1' => [
            'maxProcesses' => 10,
            'balanceMaxShift' => 1,
            'balanceCooldown' => 3,
        ],
    ],

    'local' => [
        'supervisor-1' => [
            'maxProcesses' => 3,
        ],
    ],
],

The “balance” field can be:

  • simple, which will split the process in only two tails;
  • auto, which will allow the division into queues based on the remaining processes and average waiting times;
  • null, which will manage the processes in a single queue.

The

queue field defines the queues. You can define unique queue names comma separated. A minimum and a maximum number of processes to be managed in a queue can also be defined.

Other settings to be highlighted are those defined in the queue.php file:

'redis' => [
 'driver' => 'redis',
 'connection' => 'default',
 'queue' => env('REDIS_QUEUE', 'default'),
 'retry_after' => 90,
 'block_for' => null,
],

From the above config, redis will connect to the default queue and, if there are no other processes in the queue, it will restart every 90 seconds.

8. Testing Horizon installation

Finally, let us test the installation. Clear the horizon cache:

php artisan cache:clear

Then start horizon

php artisan horizon

You should see Horizon started successfully. This will block so to perform other operations open a new terminal window then change to the app directory.

In the other window, start Laravel server

php artisan serve --port=8080

9. Accessing Dashboard

The laravel app will listen on http://server_ip:port. Replace with the IP address of your server and the port where the service has been installed on. To access horizon, append /horizon to the app url, i.e.

http://server_ip:port/horizon.

The Dashboard shows the Horizon outputs, obviously based on the previously decided configurations. When you change Horizon configuration, you will need to stop Horizon, clear the cache and restart it. 

Conclusion

That is it! You should now have Laravel horizon set up and accessible from the set up URL. You can modify configurations in the horizon.php file.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy