Features and Basics of Laravel Horizon

Editorial Team

Features and Basics of Laravel Horizon

You should be thorough about the concept of Laravel’s basic queue services before you delve into Laravel Horizon. If the basics are not clear, the chances are that Laravel Horizon will look like a complicated and complex tool to you. Laravel Horizon has brilliant features and provides an updated and fabulous dashboard for the Laravel-powered Redis queues. This code-driven configuration makes it easy to store the entire queue worker configuration in one file and enables the monitoring of important metrics of the queue system.

Laravel horizon is a great tool to measure execution time, job productivity or throughput, and failures. Laravel Horizon plays the role of a queue director that controls the queues, designs job processing, creates analytics, and performs errands related to the queue using an effective dashboard. To achieve the benefits of the technicality of Laravel, you must hire Laravel developers who will help you with the complete picture of how your application submits and executes jobs.

Let us look at some facts related to Laravel Horizon:

  • Laravel is one of the most desirable frameworks for 2022 because of its easy-to-understand code structure and elegant syntax.
  • The future of Laravel in Web development is great because of its amazing features and its capability to improve operations and web marketing
  • 10% of developers use Laravel as per the Stack Overflow’s Developer Survey from 2021
  • Some of the big companies that use Laravel are Pfizer, BBC, Liberty Mutual Insurance, About You, etc

Features: How Laravel Horizon Works

Here is a comprehensive guide on the working and key features of Laravel Horizon:

Installation

To power your queues with Laravel Horizon, you must use Redis and set it up in your application’s config/queue.php configuration file. You can also use the composer package manager to install Horizon in your project. After the installation is complete, you can install the horizon:install Artisan command to publish your assets.

Composition

After Horizon’s assets are released, the primary configuration file lets you configure queue operations for the application. This configuration file is located at config/horizon.php, and every file is unique, with a description aligned to a specific purpose, so you must review the file carefully before you use it. Internally, Horizon uses a Redis connection which should only be attributed to Horizon and not be assigned to any other Redis connection in the database.php configuration file.

Environment

This is an array in which the application will run and define worker process options for each environment. Besides the default production environment and the local environment, you can make more additions to it. Environments are usually assessed by the value of the APP_ENV environment variable. The worker process configuration of the environment is used when you start working on Laravel Horizon.

This can be clear with these examples:

  • The default local Horizon environment can start 3 worker processes and adjust the processes carried out by workers which have been aligned with each queue.
  • The default production environment can initiate 10 worker processes and adjust the processes carried out by workers which have been aligned with each queue.

Dashboard Horizon

The dashboard is a convenient one-page Vue application that can be inserted directly into your current application using a simple arranger that requires Laravel Horizon. It provides continuous insight into queued jobs, process counts, metrics, throughput, and delayed jobs. Dashboard approval or authentication is restricted by a registered callback using Horizon::auth that gives full permission to access the dashboard. It all starts with running PHP Artisan Horizon. This command filters Horizon.php configuration documents and starts different queue workers depending on these configurations.

Here you can characterize several supervisors for each environment. The supervisor is a process that exists in the system and is dedicated to checking the various queues and making sure they are working as agreed. For each supervisor, you can set the following:

  • By default, the supervisor monitors the default queue, but you can add some queues
  • Minimum and maximum number of workers that can run
  • Time to move bombed jobs
  • Memory limit for each specialist
  • Timeout for each worker
  • The number of seconds to rest if there is no free work
  • Number of attempts to complete the task
  • Adaptation strategy
  • Whether to run workers in maintenance mode

Supervisor

Every environment has a supervisor which is set by default as supervisor-1; Each supervisor controls manages, and balances the worker processes across queues. Additional supervisors can be added by defining a different draining strategy or number of worker processes for application-based specific queues.

Dashboard Authorization

You can operate the dashboard only in on-premises environments at the /horizon URI. The app/Providers/HorizonServiceProvider.php  file has an authentication gate definition that allows you to control access to Horizon in other environments like the non-local ones. You can make changes to the gate to enable or confine access to your Horizon installation if required.

Alternate Authentication Strategies

Laravel pushes the authenticated user into gate closure. You can use change function ($user) closure signature to function ($user = null) so that Laravel does not require authentication. If your application provides Horizon security in another way, then the users may not be required to “log in” due to IP restrictions.

Update Horizon

When upgrading to a new major release of Horizon, it’s important to read the upgrade guide carefully and re-post Horizon`s property. For regular updates and to prevent any hurdles about such updates and upgrades, it is recommended to add the horizon:publish command to the post-update-cmd scripts in your application’s composer.json file.

Running Horizon

Start using Laravel Horizon by enabling the horizon Artisan command. Before you start running horizon, make sure that the application’s config/horizon.php configuration file is updated with the supervisors and workers in the application. Once everything is properly set up as per the instructions given above, the single artisan command can start the functioning of the configured worker processes for the current or default environment which is set.

Clearing Jobs from Queues

To erase all jobs from your application’s default queue, you can use the horizon:clear Artisan command.

Metrics

Metrics Horizon consists of a metrics dashboard that offers statistics concerning your task and queue wait instances and throughput. To populate this dashboard, you need to configure Horizon’s snapshot Artisan command to get activated every 5 mins through your application’s scheduler.

Notifications

If you would like to be notified whilst certainly considered one among your queues has an extended wait time, you can use the Horizon::routeMailNotificationsTo, Horizon::routeSlackNotificationsTo, and Horizon::routeSmsNotificationsTo methods. You can call these methods from your application’s boot method of your application’s App\Providers\HorizonServiceProvider:

Conclusion

Laravel Horizon is a large, extensive, and highly beneficial process. It’s best to be handled by professionals, so we recommend that instead of trying it out on your own, you must hire Laravel developers to work on this little complex but highly advantageous application. Here we have covered just the basics, as the scope is quite wide when it comes to creating jobs and assigning them to queues, which is beyond the scope of a layman.