Laravel-Docs/horizon.md

198 lines
8.5 KiB
Markdown
Raw Permalink Normal View History

2017-08-17 15:50:45 +02:00
# Laravel Horizon
- [Introduction](#introduction)
- [Installation](#installation)
- [Configuration](#configuration)
- [Dashboard Authentication](#dashboard-authentication)
- [Running Horizon](#running-horizon)
- [Deploying Horizon](#deploying-horizon)
- [Tags](#tags)
- [Notifications](#notifications)
- [Metrics](#metrics)
<a name="introduction"></a>
## 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.
<a name="installation"></a>
## Installation
2018-07-18 03:26:14 +02:00
> {note} Due to its usage of async process signals, Horizon requires PHP 7.1+. Secondly, you should ensure that your queue driver is set to `redis` in your `queue` configuration file.
2017-08-17 15:50:45 +02:00
You may use Composer to install Horizon into your Laravel project:
2020-04-08 00:29:42 +02:00
composer require laravel/horizon "^2.0"
2017-08-17 15:50:45 +02:00
After installing Horizon, publish its assets using the `vendor:publish` Artisan command:
php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
<a name="configuration"></a>
### Configuration
2018-07-18 03:26:14 +02:00
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.
2017-08-17 15:50:45 +02:00
#### Balance Options
Horizon allows you to choose from three balancing strategies: `simple`, `auto`, and `false`. The `simple` strategy, which is the 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.
<a name="dashboard-authentication"></a>
### Dashboard Authentication
2018-02-12 15:03:48 +01:00
Horizon exposes a dashboard at `/horizon`. By default, you will only be able to access this dashboard in the `local` environment. To define a more specific access policy for the dashboard, you should use the `Horizon::auth` method. The `auth` method accepts a callback which should return `true` or `false`, indicating whether the user should have access to the Horizon dashboard. Typically, you should call `Horizon::auth` in the `boot` method of your `AppServiceProvider`:
2017-08-17 15:50:45 +02:00
Horizon::auth(function ($request) {
// return true / false;
});
<a name="running-horizon"></a>
## 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 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
<a name="deploying-horizon"></a>
### 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.
#### Supervisor Configuration
If you are using the Supervisor process monitor to manage your `horizon` process, the following configuration file should suffice:
[program:horizon]
process_name=%(program_name)s
2017-08-17 15:50:45 +02:00
command=php /home/forge/app.com/artisan horizon
autostart=true
autorestart=true
user=forge
redirect_stderr=true
stdout_logfile=/home/forge/app.com/horizon.log
> {tip} If you are uncomfortable managing your own servers, consider using [Laravel Forge](https://forge.laravel.com). Forge provisions PHP 7+ servers with everything you need to run modern, robust Laravel applications with Horizon.
<a name="tags"></a>
## Tags
Horizon allows you to assign “tags” to jobs, including mailables, event broadcasts, notifications, and queued event listeners. In fact, Horizon will intelligently and automatically tag most jobs depending on the Eloquent models that are attached to the job. For example, take a look at the following job:
<?php
namespace App\Jobs;
use App\Video;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class RenderVideo implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The video instance.
*
* @var \App\Video
*/
public $video;
/**
* Create a new job instance.
*
* @param \App\Video $video
* @return void
*/
public function __construct(Video $video)
{
$this->video = $video;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
}
}
If this job is queued with an `App\Video` instance that has an `id` of `1`, it will automatically receive the tag `App\Video:1`. This is because Horizon will examine the job's properties for any Eloquent models. If Eloquent models are found, Horizon will intelligently tag the job using the model's class name and primary key:
$video = App\Video::find(1);
App\Jobs\RenderVideo::dispatch($video);
#### Manually Tagging
If you would like to manually define the tags for one of your queueable objects, you may define a `tags` method on the class:
class RenderVideo implements ShouldQueue
{
/**
* Get the tags that should be assigned to the job.
*
* @return array
*/
public function tags()
{
return ['render', 'video:'.$this->video->id];
}
}
<a name="notifications"></a>
## Notifications
2018-06-14 11:19:02 +02:00
> **Note:** Before using notifications, you should add the `guzzlehttp/guzzle` Composer package to your project. When configuring Horizon to send SMS notifications, you should also review the [prerequisites for the Nexmo notification driver](https://laravel.com/docs/5.6/notifications#sms-notifications).
2017-08-17 15:50:45 +02:00
2017-11-27 14:39:36 +01:00
If you would like to be notified when one of your queues has a long wait time, you may use the `Horizon::routeMailNotificationsTo`, `Horizon::routeSlackNotificationsTo`, and `Horizon::routeSmsNotificationsTo` methods. You may call these methods from your application's `AppServiceProvider`:
2017-08-17 15:50:45 +02:00
Horizon::routeMailNotificationsTo('example@example.com');
2017-11-27 14:39:36 +01:00
Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
2017-08-17 15:50:45 +02:00
Horizon::routeSmsNotificationsTo('15556667777');
#### Configuring Notification Wait Time Thresholds
2017-09-11 10:46:20 +02:00
You may configure how many seconds are considered a "long wait" within your `config/horizon.php` configuration file. The `waits` configuration option within this file allows you to control the long wait threshold for each connection / queue combination:
2017-08-17 15:50:45 +02:00
'waits' => [
'redis:default' => 60,
],
<a name="metrics"></a>
## Metrics
Horizon includes a metrics dashboard which provides information on your job and queue wait times and throughput. In order to populate this dashboard, you should configure Horizon's `snapshot` Artisan command to run every five minutes via your application's [scheduler](/docs/{{version}}/scheduling):
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('horizon:snapshot')->everyFiveMinutes();
}