mrfabulous / ucsf_laravel-queue-monitor
Laravel数据库作业队列的队列监控
Requires
- php: ^7.4|^8.0
- ext-json: *
- ext-mbstring: *
- illuminate/database: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/queue: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
- nesbot/carbon: ^2.0
Requires (Dev)
- doctrine/dbal: ^3.1
- friendsofphp/php-cs-fixer: ^3.0
- laravel/framework: ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
- mockery/mockery: ^1.3.2
- orchestra/testbench: >=3.8
- phpstan/phpstan: ^0.12.99|^1.0
- phpunit/phpunit: ^8.5.23|^9.0
- romanzipp/php-cs-fixer-config: ^3.0
This package is auto-updated.
Last update: 2024-09-27 19:34:18 UTC
README
此包提供了类似于Laravel Horizon的数据库队列监控功能。
功能
- 监控任何队列的作业,类似于Laravel Horizon
- 处理失败作业并存储异常
- 监控作业进度
- 获取作业剩余时间的估计值
- 为作业监控存储额外数据
- 通过UI重试作业
安装
✨ 如果您正在更新到4.0,请参阅升级指南 ✨
composer require romanzipp/laravel-queue-monitor
有关Laravel Nova资源和指标的信息,请参阅romanzipp/Laravel-Queue-Monitor-Nova
配置
将配置和迁移复制到您的项目中
php artisan vendor:publish --provider="romanzipp\QueueMonitor\Providers\QueueMonitorProvider" --tag=config --tag=migrations
迁移队列监控表。表名可以在配置文件中配置或通过发布的迁移配置。
php artisan migrate
使用
要监控一个作业,只需添加romanzipp\QueueMonitor\Traits\IsMonitored
特性。
use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use romanzipp\QueueMonitor\Traits\IsMonitored; // <--- class ExampleJob implements ShouldQueue { use Dispatchable; use InteractsWithQueue; use Queueable; use SerializesModels; use IsMonitored; // <--- }
重要!您需要实现Illuminate\Contracts\Queue\ShouldQueue
接口到您的作业类。否则,Laravel不会调度任何包含状态信息的作业事件以进行监控。
Web界面
您可以通过将ui.enabled
配置值设置为true
来启用Web UI。
发布前端资源
php artisan vendor:publish --provider="romanzipp\QueueMonitor\Providers\QueueMonitorProvider" --tag=assets
有关更多信息,请参阅完整的配置文件
命令
artisan queue-monitor:stale
此命令将旧的监控条目标记为stale
。您应该定期在控制台内核中运行此命令。
参数和选项
--before={date}
所有条目将在此日期之前被标记为stale的起始日期--beforeDays={days}
在此天数之前将条目标记为stale的相对天数--beforeInterval={interval}
在此间隔日期字符串之前将条目标记为stale--dry
仅进行Dry-run
artisan queue-monitor:purge
此命令删除旧的监控模型。
参数和选项
--before={date}
在此日期之前删除所有条目的起始日期--beforeDays={days}
在此天数之前删除条目的相对天数--beforeInterval={interval}
在此间隔日期字符串之前删除条目的--queue={queue}
仅清除特定队列(逗号分隔值)--only-succeeded
仅清除成功的条目--dry
仅进行Dry-run
需要提供before
或beforeDate
参数之一。
高级使用
进度
您可以将一个进度值
(0-100)设置为获取作业进度的估计。
use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class ExampleJob implements ShouldQueue { use IsMonitored; public function handle() { $this->queueProgress(0); // Do something... $this->queueProgress(50); // Do something... $this->queueProgress(100); } }
块进度
一个常见的作业场景是迭代大型集合。
本例中的作业循环遍历大量用户,并在每次数据块迭代时更新其进度值。
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Collection; use romanzipp\QueueMonitor\Traits\IsMonitored; class ChunkJob implements ShouldQueue { use IsMonitored; public function handle() { $usersCount = User::count(); $perChunk = 50; User::query() ->chunk($perChunk, function (Collection $users) use ($perChunk, $usersCount) { $this->queueProgressChunk($usersCount‚ $perChunk); foreach ($users as $user) { // ... } }); } }
进度冷却
为了避免因快速重复的更新查询而使数据库过载,您可以重写progressCooldown
方法并指定等待的秒数,在将每个进度更新写入数据库之前。请注意,对于0、25、50、75和100这些值,冷却时间将始终被忽略。
use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class LazyJob implements ShouldQueue { use IsMonitored; public function progressCooldown(): int { return 10; // Wait 10 seconds between each progress update } }
自定义数据
此包还允许在监控模型中用数组语法设置自定义数据。
use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class CustomDataJob implements ShouldQueue { use IsMonitored; public function handle() { $this->queueData(['foo' => 'Bar']); // WARNING! This is overriding the monitoring data $this->queueData(['bar' => 'Foo']); // To preserve previous data and merge the given payload, set the $merge parameter true $this->queueData(['bar' => 'Foo'], true); } }
为了在UI上显示自定义数据,您需要在config/queue-monitor.php
下添加此行。
'ui' => [ ... 'show_custom_data' => true, ... ]
仅保留失败作业
您可以通过重写keepMonitorOnSuccess()
方法来仅存储执行作业的失败监控条目。如果您只想保留经常执行但值得监控的作业的失败监控,则可以使用此功能。或者,您也可以使用Laravel内置的failed_jobs
表。
use Illuminate\Contracts\Queue\ShouldQueue; use romanzipp\QueueMonitor\Traits\IsMonitored; class FrequentSucceedingJob implements ShouldQueue { use IsMonitored; public static function keepMonitorOnSuccess(): bool { return false; } }
检索处理过的作业
use romanzipp\QueueMonitor\Models\Monitor; $job = Monitor::query()->first(); // Check the current state of a job $job->isFinished(); $job->hasFailed(); $job->hasSucceeded(); // Exact start & finish dates with milliseconds $job->getStartedAtExact(); $job->getFinishedAtExact(); // If the job is still running, get the estimated seconds remaining // Notice: This requires a progress to be set $job->getRemainingSeconds(); $job->getRemainingInterval(); // Carbon\CarbonInterval // Retrieve any data that has been set while execution $job->getData(); // Get the base name of the executed job $job->getBasename();
模型作用域
use romanzipp\QueueMonitor\Models\Monitor; // Filter by Status Monitor::failed(); Monitor::succeeded(); // Filter by Date Monitor::lastHour(); Monitor::today(); // Chain Scopes Monitor::today()->failed();
测试
在本地执行测试的最简单方法是使用Lando。该Lando配置文件将自动启动应用程序和数据库容器。
lando start lando phpunit-mysql lando phpunit-postgres
升级
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。
本包受gilbitron的laravel-queue-monitor的启发,该包不再维护。