mcpField
L
laravel:queues-and-horizon

laravel:queues-and-horizon

github.com/jpcaparas开发157169 次安装17 周前更新
代码仓库

关于 laravel:queues-and-horizon

面向 Laravel 开发者的队列与 Horizon 运维技能,帮助验证任务行为,涵盖 Worker 命令、可安全重试的任务模式、路由、监控和测试。

主要功能

  1. 01Worker 与 Horizon 命令
  2. 02命名队列优先级管理
  3. 03可安全重试的任务设计
  4. 04队列路由与任务属性
  5. 05任务派发及集成测试

适用场景

  1. 01按队列和重试设置运行 Worker
  2. 02根据路由策略指定连接与队列
  3. 03验证任务派发及其副作用
  4. 04检查失败任务和队列行为

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jpcaparas/superpowers-laravel --skill laravel:queues-and-horizon

安装后在 Claude Code 中输入/laravel:queues-and-horizon确认已加载。

详细介绍

队列与 Horizon

安全地运行 Worker、验证执行情况,并测试任务行为。

命令

# Start worker
sail artisan queue:work --queue=high,default --tries=3 --backoff=5   # or: php artisan queue:work --queue=high,default --tries=3 --backoff=5

# Horizon (if installed)
sail artisan horizon                                               # or: php artisan horizon

# Failed jobs
sail artisan queue:failed                                          # or: php artisan queue:failed
sail artisan queue:retry all                                       # or: php artisan queue:retry all

模式

  • 使用具名队列来安排优先级;让默认设置保持合理
  • 在任务中添加带有上下文且可采取行动的 Log::warning/::error
  • 幂等性:让任务可以安全重试
  • 尽可能发送指标;在 Horizon 或 APM 中进行监控

Laravel 13+

# Centralized queue routing (e.g., in AppServiceProvider)
Queue::route(ProcessPodcast::class, connection: 'redis', queue: 'podcasts');

# Declarative job controls via attributes
use Illuminate\Queue\Attributes\{Tries, Backoff, Timeout, FailOnTimeout};

#[Tries(3)]
#[Backoff([10, 30, 60])]
#[Timeout(120)]
#[FailOnTimeout]
class ProcessPodcast implements ShouldQueue { /* ... */ }

# Graceful shutdown for long-running jobs
class ProcessPodcast implements ShouldQueue, Interruptible {
    public function onInterrupt(): void { /* release locks, save state */ }
}

# Auto-scaling friendly worker exit
sail artisan queue:work --stop-when-empty-for=60   # or: php artisan queue:work --stop-when-empty-for=60
  • 如果队列路由属于策略,而非调用方的决定,优先使用 Queue::route(),而不是在每次分发时使用 onQueue()
  • 关于优先使用属性的风格及其权衡,请参阅 laravel:php-attributes

测试任务

  • 在单元测试中使用 Bus::fake() 断言任务已分发
  • 使用集成测试验证副作用(数据库/IO)

评论

支持 Markdown