Horizon Queue Management Tool
- Configuration
- Balanced configuration
- Task trimming
- ##Tags
- Configuration waiting time has expired Long threshold
Laravel Horizon
##IntroductionHorizon provides a beautiful dashboard and can configure your Laravel Redis queue through code, and it allows you to easily monitor your queue system Key metrics such as task throughput, runtime, and failed tasks. All configuration items are stored in a simple configuration file, allowing you to place it in the team's collaborative version control to facilitate team collaboration.Installation
{note} You should make sure
queue
is set in theredis
configuration file Queue driver.
You should use Composer to install Horizon for your Laravel project:
composer require laravel/horizon
After the installation is complete, use horizon:install
to issue the Artisan command:
php artisan horizon:install
You should also create the failed_jobs
table, which Laravel will use to store any failed queue jobs:
php artisan queue:failed-table php artisan migrate
Configuration
After publishing Horizon related files, its main configuration file will be placed in config/horizon.php
. You can configure queue-related options in this file, and each configuration item has detailed usage instructions. Please read this file in detail.
Balanced Configuration
Horizon provides three balancing strategies: simple
, auto
, and false
. The default is simple
, which will distribute the received tasks equally to the queue process:
'balance' => 'simple',
auto
The strategy will adjust the work of each queue according to the current workload Number of process tasks. For example: If the notifications
queue has 1000 pending tasks, but your render
queue is empty, Horizon will allocate more worker processes to the notifications
queue , until all tasks in the notifications
queue are completed. When the configuration item balance
is configured to false
, Horizon will use Laravel's default execution behavior, which will process queue tasks in the order listed in the configuration.
Task Pruning
horizon
The configuration file allows you to configure how long (in minutes) recent and failed tasks should be retained. By default, recent tasks are retained for one hour, and failed tasks are retained for one week:
'trim' => [ 'recent' => 60, 'failed' => 10080, ],##Dashboard Permission VerificationHorizon Dashboard The route is
/horizon. By default, you can only access the dashboard in the
local environment. In your
app/Providers/HorizonServiceProvider.php file, there is a
gate method. This authorization controls access to Horizon in
non-local environments. You are free to modify this facade to restrict access to your Horizon installation as needed:
/** * 注册 Horizon gate 方法 * * gate 决定了谁可以在非本地环境中访问 Horizon。 * * @return void */ protected function gate(){ Gate::define('viewHorizon', function ($user) { return in_array($user->email, [ 'taylor@laravel.com', ]); }); }Run HorizonWhen in
config After configuring your queue execution process in the /horizon.php file, you can start Horizon using the
horizon Artisan command. Only one command statement is needed to start all configured queue processes:
php artisan horizonYou can also use the
horizon:pause and
horizon:continue Artisan commands to pause or Continue executing queue tasks:
php artisan horizon:pausephp artisan horizon:continueYou can use the
horizon:terminate Artisan command to gracefully terminate the Horizon main process. Horizon will exit after processing the task being executed:
php artisan horizon:terminate
Deploying Horizon
If you deploy Horizon to an online server, you need to configure a process monitor to detect the php artisan horizon
command and automatically restart it when it exits unexpectedly. When new code is launched, the process monitor needs to terminate the Horizon process and restart Horizon with the modified code.
Supervisor Configuration
If you use the Supervisor process monitor to manage your horizon
process, then the following configuration file will meet your needs:
[program:horizon] process_name=%(program_name)s 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 don't like to maintain the server yourself, you can consider using Laravel Forge. Forge provides PHP7 and all other environments needed to run a modern, powerful Laravel application with Horizon.
Tags
Horizon allows you to assign "tags" to tasks, including emails, event broadcasts, notifications, and queued event listeners. device. In fact, Horizon will intelligently and automatically label most tasks based on the Eloquent model carried by the task, as in the following task example:
<?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; /** * video 实例 * * @var \App\Video */ public $video; /** * 创建工作实例 * * @param \App\Video $video * @return void */ public function __construct(Video $video) { $this->video = $video; } /** * 执行任务 * * @return void */ public function handle() { // } }
If the queue task is a task carrying id
is 1
's App\Video
instance, then it will automatically be marked with the App\Video:1
tag. Because Horizon will check whether the task attribute has an Eloquent model, if an Eloquent model is found, Horizon will intelligently tag the task with the class name and primary key of the model:
$video = App\Video::find(1); App\Jobs\RenderVideo::dispatch($video);
Custom tag
If you want to manually define tags for queued objects, you can define a tags
method for this class:
class RenderVideo implements ShouldQueue{ /** * 获取分配给这个任务的标签 * * @return array */ public function tags() { return ['render', 'video:'.$this->video->id]; } }
Notification
Note: Before using notifications, you should add the
guzzlehttp/guzzle
Composer package to your project. When using Horizon to configure sending SMS notifications, you should also read the Nexmo Notification Driver Dependencies chapter.
If you need to initiate a notification when the queue waiting time is too long, you can call Horizon::routeMailNotificationsTo
, Horizon: in the application's
AppServiceProvider :routeSlackNotificationsTo
, and Horizon::routeSmsNotificationsTo
Methods:
Horizon::routeMailNotificationsTo('example@example.com'); Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel'); Horizon::routeSmsNotificationsTo('15556667777');
Configuring the threshold for excessive wait time
You can configure this in config/horizon.php
Set the specific number of seconds when the waiting time is too long in the configuration file. waits
Configuration items can configure thresholds for each link/queue:
'waits' => [ 'redis:default' => 60, ],
Metrics
Horizon contains a Metrics instrument Dashboard, which can provide task and queue wait time and throughput information. In order to populate this dashboard, you need to configure the application's scheduler to run Horizon's snapshot
Artisan command every five minutes:
/** * 定义应用程序的任务调度 * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule){ $schedule->command('horizon:snapshot')->everyFiveMinutes(); }