Laravel Horizon
Introduction
Horizon provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues. Horizon allows you to easily monitor key metrics of your queue system such as job throughput, runtime, and job failures.
All of your worker configuration is stored in a single, simple configuration file, allowing your configuration to stay in source control where your entire team can collaborate.
Installation
You should ensure that your queue connection is set to redis
in your queue
configuration file.
You may use Composer to install Horizon into your Laravel project:
composer require laravel/horizon
After installing Horizon, publish its assets using the horizon:install
Artisan command:
php artisan horizon:install
Configuration
After publishing Horizon's assets, its primary configuration file will be located at config/horizon.php
. This configuration file allows you to configure your worker options and each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.
You should ensure that the environments
portion of your horizon
configuration file contains an entry for each environment on which you plan to run Horizon.
Balance Options
Horizon allows you to choose from three balancing strategies: simple
, auto
, and false
. The simple
strategy, which is the configuration file's default, splits incoming jobs evenly between processes:
'balance' => 'simple',
The auto
strategy adjusts the number of worker processes per queue based on the current workload of the queue. For example, if your notifications
queue has 1,000 waiting jobs while your render
queue is empty, Horizon will allocate more workers to your notifications
queue until it is empty. When the balance
option is set to false
, the default Laravel behavior will be used, which processes queues in the order they are listed in your configuration.
When using the auto
strategy, you may define the minProcesses
and maxProcesses
configuration options to control the minimum and maximum number of processes Horizon should scale up and down to:
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'auto',
'minProcesses' => 1,
'maxProcesses' => 10,
'tries' => 3,
],
],
],
Job Trimming
The horizon
configuration file allows you to configure how long recent and failed jobs should be persisted (in minutes). By default, recent jobs are kept for one hour while failed jobs are kept for a week:
'trim' => [
'recent' => 60,
'failed' => 10080,
],
Dashboard Authorization
Horizon exposes a dashboard at /horizon
. By default, you will only be able to access this dashboard in the local
environment. Within your app/Providers/HorizonServiceProvider.php
file, there is a gate
method. This authorization gate controls access to Horizon in non-local environments. You are free to modify this gate as needed to restrict access to your Horizon installation:
/**
* Register the Horizon gate.
*
* This gate determines who can access Horizon in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewHorizon', function ($user) {
return in_array($user->email, [
'taylor@laravel.com',
]);
});
}
Remember that Laravel injects the authenticated user to the Gate automatically. If your app is providing Horizon security via another method, such as IP restrictions, then your Horizon users may not need to "login". Therefore, you will need to change function ($user)
above to function ($user = null)
to force Laravel to not require authentication.
Upgrading Horizon
When upgrading to a new major version of Horizon, it's important that you carefully review the upgrade guide.
In addition, when upgrading to any new Horizon version, you should re-publish Horizon's assets:
php artisan horizon:publish
To keep the assets up-to-date and avoid issues in future updates, you may add the command to the post-update-cmd
scripts in your composer.json
file:
{
"scripts": {
"post-update-cmd": [
"@php artisan horizon:publish --ansi"
]
}
}
Running Horizon
Once you have configured your workers in the config/horizon.php
configuration file, you may start Horizon using the horizon
Artisan command. This single command will start all of your configured workers:
php artisan horizon
You may pause the Horizon process and instruct it to continue processing jobs using the horizon:pause
and horizon:continue
Artisan commands:
php artisan horizon:pause
php artisan horizon:continue
You may check the current status of the Horizon process using the horizon:status
Artisan command:
php artisan horizon:status
You may gracefully terminate the master Horizon process on your machine using the horizon:terminate
Artisan command. Any jobs that Horizon is currently processing will be completed and then Horizon will exit:
php artisan horizon:terminate
Deploying Horizon
If you are deploying Horizon to a live server, you should configure a process monitor to monitor the php artisan horizon
command and restart it if it quits unexpectedly. When deploying fresh code to your server, you will need to instruct the master Horizon process to terminate so it can be restarted by your process monitor and receive your code changes.
Installing Supervisor
Supervisor is a process monitor for the Linux operating system, and will automatically restart your horizon
process if it fails. To install Supervisor on Ubuntu, you may use the following command:
sudo apt-get install supervisor