vdlp / oc-horizon-plugin
Laravel Horizon集成于October CMS
Requires
- php: ^8.0.2
- ext-pcntl: *
- ext-posix: *
- ext-redis: *
- composer/installers: ^1.0 || ^2.0
- laravel/horizon: ^5.24.4
- october/rain: ^3.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.42
README
在October CMS中提供Laravel Horizon 5.0的无缝集成。
队列,拥有X-Ray Vision。通过一个漂亮的仪表板和代码驱动的配置,为您的队列提供强大的支持。
天作之合
Horizon由Laravel框架的核心开发者开发,为Laravel的Redis队列提供强大的队列监控解决方案。Horizon允许您轻松监控队列系统的重要指标,如作业吞吐量、运行时间和作业失败。
开源
Horizon是100%开源的,因此您可以自由地查看源代码以了解其工作原理。如果发现需要改进的地方,只需在GitHub上向我们发送pull request即可。
要求
- October CMS 3.0
- PHP 8.0.2或更高版本
- PHP扩展:
ext-pcntl
、ext-posix
和ext-redis
。 - Supervisor,请参阅Laravel 9.x supervisor配置。
安装
composer require vdlp/oc-horizon-plugin
关闭laravel/horizon
的自动发现(重要)
因为这个插件有自己的HorizonServiceProvider
,它扩展自Laravel\Horizon\HorizonServiceProvider
,我们需要防止由于Laravel的自动包发现而加载Laravel\Horizon\HorizonServiceProvider
。
您应该在项目的composer.json
文件中添加dont-discover
选项(该文件位于项目的根路径)。
"extra": {
"laravel": {
"dont-discover": [
"laravel/horizon"
]
}
}
重要:在添加这些行后,请确保执行
composer update
以应用更改。您还需要删除文件storage/framework/packages.php
。不用担心,当您访问项目时,该文件将被重新生成。
重要:请确保将
composer.json
部署到您的托管站点。这将由框架解析以确定哪些服务提供者应该被忽略。
配置
php artisan horizon:install
- 在
config/horizon.php
中配置Laravel Horizon设置文件,请确保use
包含horizon
(见下面的配置片段)。
/*
|--------------------------------------------------------------------------
| Horizon Redis Connection
|--------------------------------------------------------------------------
|
| This is the name of the Redis connection where Horizon will store the
| meta information required for it to function. It includes the list
| of supervisors, failed jobs, job metrics, and other information.
|
*/
'use' => 'horizon',
- 添加到
config/queue.php
的连接
'redis' => [
'driver' => 'redis',
'connection' => 'horizon', // References: databases.redis.horizon
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => env('QUEUE_RETRY_AFTER', 90),
'block_for' => null,
],
- 将特定于Horizon的Redis数据库配置添加到
config/databases.php
'redis' => [
'cluster' => false,
'client' => 'phpredis',
'default' => [
// ..
],
'horizon' => [
'host' => env('HORIZON_REDIS_HOST', '127.0.0.1'),
'password' => env('HORIZON_REDIS_PASSWORD'),
'port' => env('HORIZON_REDIS_PORT', 6379),
'database' => env('HORIZON_REDIS_DATABASE', '1'),
]
],
- 将队列连接
QUEUE_CONNECTION
(可在config/queue.php
中找到)修改为redis
,如下所示
/*
|--------------------------------------------------------------------------
| Default Queue Driver
|--------------------------------------------------------------------------
|
| The Laravel queue API supports a variety of back-ends via an unified
| API, giving you convenient access to each back-end using the same
| syntax for each one. Here you may set the default queue driver.
|
| Supported: "null", "sync", "database", "beanstalkd",
| "sqs", "iron", "redis"
|
*/
'default' => env('QUEUE_CONNECTION', 'redis'),
.env
至少应包含以下QUEUE_
和HORIZON_
变量
#
# Queue
#
QUEUE_CONNECTION = "redis"
QUEUE_RETRY_AFTER = 90
#
# Horizon
#
HORIZON_PREFIX = "myproject-local:"
HORIZON_REDIS_HOST = "127.0.0.1"
HORIZON_REDIS_PASSWORD = null
HORIZON_REDIS_PORT = 6379
HORIZON_REDIS_DATABASE = "1"
建议将您的队列工作配置config.horizon.environments
也添加到.env
文件中。
服务器配置
- 在服务器上的
supervisord
配置中添加以下内容。完整的supervisord
配置可以在supervisor网站上找到。 - 请参阅配置Supervisor(官方Laravel文档)。
[program:<queue-name>-queue]
process_name=%(program_name)s_%(process_num)02d
directory=/<myproject-directory>
command=/<path-to-php>/php /<myproject-directory>/artisan horizon
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=<user>
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/supervisord/<queue-name>-queue.log
stopwaitsecs=90
- 在服务器上的cronjob配置中添加以下内容。这将确保定期创建Horizon指标。
* * * * * /<path-to-php>/php /<myproject-directory>/artisan schedule:run > /dev/null
创建作业类
请按照Laravel 9.x生成作业类的说明进行操作,以制作作业类。
请注意,在October CMS中不支持使用
php artisan make:job
命令。与通用Laravel项目相比,October CMS使用不同的应用程序结构。
此插件还包含一个示例作业文件:Vdlp\Horizon\Example
。此示例文件不使用SerializesModels
和InteractsWithQueue
特性。
测试
- 登录到后端。
- 使用
.env
文件将应用程序设置为调试模式:APP_DEBUG=true
,或通过更改config/app.php
文件中的debug
键。 - 使用此命令运行Horizon:
php artisan horizon
。 - 现在运行此命令将一些
Vdlp\Horizon\Example
作业推送到队列
php artisan vdlp:horizon:push-example-jobs
- 检查
/backend/vdlp/horizon/dashboard
或/horizon
中的Horizon仪表板。 Vdlp\Horizon\Example
每个作业都应该将一个随机字符串记录到应用程序日志(级别=debug
)。
文档
请访问Laravel网站以获取有关Laravel Horizon的详细文档。
问题
如果您对此插件的使用有任何疑问,请随时通过octobercms@vdlp.nl联系我们。我们很乐意帮助您。您也可以访问支持论坛并在那里提出问题/问题。