laravel horizon——迹忆客-ag捕鱼王app官网
简介
horizon 为 laravel 提供了基于 redis 的、拥有美观后台的、代码驱动配置的队列系统。horizon 让我们可以轻松监控队列系统的关键指标,例如任务吞吐量、运行时间和失败任务等。
所有的队列进程配置都存放在一个单独的简单配置文件中,这样的话配置文件就可以存放到源码控制以便团队所有成员的协作。
安装
注:需要确保在 queue 配置文件中将队列驱动被设置为 redis。
我们使用 composer 安装 horizon 到 laravel 项目:
$ composer require laravel/horizon
安装完成后,使用 artisan 命令 horizon:install
发布资源:
$ php artisan horizon:install
配置
发布好前端资源后,主配置文件就会出现在 config/horizon.php。在这个配置文件中,你可以配置队列进程选项以及每个包含目的描述的配置项,所以使用 horizon 前务必浏览下这个配置文件。
注:需要确保 horizon 配置文件的 environments 部分包含了每个你计划运行 horizon 的环境的入口。
负载均衡配置
horizon 提供了三种负载均衡策略以供选择:simple、auto 和 false,simple 是默认策略,在进程之间平均分配进入任务:
'balance' => 'simple',
auto 策略基于队列当前负载调整每个队列的工作进程数量。例如,如果 notifications 队列有 1000 个等待执行的任务而 render 队列是空的,那么 horizon 将会为 notifications 队列分配更多的工作进程直到队列为空。如果把 balance 选项设置为 false,就会使用默认的 laravel 行为,也就是按照配置文件中的排列顺序处理队列。
使用 auto 策略时,可以定义 minprocesses 和 maxprocesses 配置项来控制 horizon 进程的最小值和最大值:
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'auto',
'minprocesses' => 1,
'maxprocesses' => 10,
'tries' => 3,
],
],
],
任务裁剪
horizon 配置文件允许我们配置最近和执行失败任务保留的时长(单位:分钟),默认情况下,最近任务保留1个小时,执行失败任务保留1周:
'trim' => [
'recent' => 60,
'failed' => 10080,
],
后台授权
默认情况下,我们只能在 local 环境下访问这个后台。在 app/providers/horizonserviceprovider.php 文件中,有一个 gate 方法,该授权 gate 用于控制非本地环境对 horizon 的访问。你可以按照需要编辑这个 gate 方法来限制用户对 horizon 的访问:
/**
* 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',
]);
});
}
注:记住 laravel 会自动注入认证用户到 gate,如果你的应用通过其它方法来保障 horizon 安全,例如 ip 限制,则你的 horizon 用户不需要「登录」。因此,你需要修改上述 function ($user) 为 function ($user = null) 来强制 laravel 不要求认证。
升级 horizon
升级到最新版本的 horizon 时,需要仔细阅读升级指南。
此外,我们还要重新发布 horizon 资源文件:
$ php artisan horizon:publish
为了使资产保持最新并避免在将来的更新中出现问题,我们可以将命令添加到composer.json文件中的post-update-cmd
脚本中:
{
"scripts": {
"post-update-cmd": [
"@php artisan horizon:publish --ansi"
]
}
}
运行 horizon
如果我们已经在配置文件 config/horizon.php
中配置过工作进程,就可以使用 artisan 命令 horizon
来启动 horizon,该命令会启动所有配置的工作进程:
$ php artisan horizon
我们可以使用 artisan 命令 horizon:pause
和 horizon:continue 来暂停或继续处理队列任务:
php artisan horizon:pause
php artisan horizon:continue
要查看 horizon 的当前状态,可以使用 artisan 命令 horizon:status:
$ php artisan horizon:status
我们还可以使用 artisan 命令 horizon:terminate
来优雅地终止 horizon 主进程 —— horizon 会在所有当前正在执行的任务全部完成后退出:
$ php artisan horizon:terminate
部署 horizon
如果要将 horizon 部署到线上服务器,需要配置一个进程监控来监控 php artisan horizon 命令的运行并在异常退出的情况下重启该进程。部署新代码到服务器的时候,需要终止 horizon 主进程,然后再通过进程监控程序重启以便让最新代码生效。
安装 supervisor
supervisor 是一个适用于 linux 操作系统的优秀进程监控程序,它可以实现 horizon 进程失败后自动重启,要安装 supervisor,以 ubuntu 为例,可以使用如下命令:
$ sudo apt-get install supervisor
注:如果自己配置 supervisor 对你而言很困难,可以考虑使用 laravel forge,它会自动安装并为你配置好 laravel 项目。
supervisor 配置
supervisor 配置文件通常存放在 /etc/supervisor/conf.d 目录下,在这个目录中,你可以创建任意数量的配置文件来告知 supervisor 你的进程需要被如何监控,和 nginx 虚拟主机配置文件类似,通常我们会为每个被监控的进程创建一个独立的配置文件。例如,我们创建一个 horizon.conf 文件来配置 horizon 进程的启动和监控(请按照自己的实际情况修改相应的文件路径):
[program:horizon]
process_name=%(program_name)s
command=php /path/to/project/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/path/to/project/storage/logs/horizon.log
stopwaitsecs=3600
需要确保 stopwaitsecs 的值大于最长耗时任务的运行时间,否则,supervisor 会在该任务执行完毕前杀死该任务。
启动 supervisor
配置文件创建好了之后,你可以使用如下命令更新 supervisor 配置并启动进程:
$ sudo supervisorctl reread
$ sudo supervisorctl update
$ sudo supervisorctl start horizon
更多关于 supervisor 的信息,请参考 。
标签
horizon 允许分配“标签”到任务,包括邮件、事件广播、通知以及队列事件监听器等。实际上,horizon 会基于附加到任务的 eloquent 模型为大部分任务以智能的方式自动打上标签。例如,我们来看看下面这个例子:
video = $video;
}
/**
* execute the job.
*
* @return void
*/
public function handle()
{
//
}
}
如果任务被推送到队列时附带了一个 id 为 1 的 app\video 实例,它将会自动打上 app\video:1 的标签,这是因为 horizon 会检查队列任务属性中的 eloquent 模型,如果 eloquent 模型被找到,horizon 就会使用模型类名和主键为任务智能地打上标签:
$video = app\video::find(1);
app\jobs\rendervideo::dispatch($video);
手动打标签
如果想要手动定义某个队列对象的标签,可以在该类中定义一个 tags 方法:
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];
}
}
通知
注意:配置 horizon 发送 slack 或者短信通知前,请先回顾下相关的通知驱动准备工作。
如果我们想要在某个队列任务等待很长时间后被通知,可以使用 horizon::routemailnotificationsto
、horizon::routeslacknotificationsto
以及 horizon::routesmsnotificationsto
方法。我们可以在 horizonserviceprovider
中调用这些方法:
horizon::routemailnotificationsto('example@example.com');
horizon::routeslacknotificationsto('slack-webhook-url', '#channel');
horizon::routesmsnotificationsto('15556667777');
配置通知等待时间下限
我们可以在配置文件 config/horizon.php
中通过修改 waits 配置项来配置每个连接/队列上的等待时间下限(秒):
'waits' => [
'redis:default' => 60,
'redis:critical,high' => 90,
],
监控
horizon 提供了一个监控后台查看任务和队列的等待时间和吞吐量信息,为了获取实时信息,可以配置 horizon 的 artisan 命令 snapshot
通过应用的调度器每五分钟运行一次:
/**
* define the application's command schedule.
*
* @param \illuminate\console\scheduling\schedule $schedule
* @return void
*/
protected function schedule(schedule $schedule)
{
$schedule->command('horizon:snapshot')->everyfiveminutes();
}