定制 therealedatta/laravel-queue-health-check 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

therealedatta/laravel-queue-health-check

Composer 安装命令:

composer require therealedatta/laravel-queue-health-check

包简介

Monitor Laravel queue health with heartbeat checks and email alerts

README 文档

README

A reusable Laravel package that monitors queue health via heartbeat checks. It periodically dispatches a job through the queue; if the job executes, it writes a timestamp to a file. A scheduled command checks whether that timestamp is recent and sends a synchronous email alert if the queue is unresponsive — and a recovery email when it comes back.

How It Works

  1. The command queue-health:check runs on a cron schedule (configurable).
  2. Each run, it checks the last heartbeat timestamp written by the job.
  3. If the timestamp is older than check_interval_minutes * 2 (minus 1 second), the queue is considered down.
  4. On first detection of downtime, a synchronous alert email is sent (not queued, since the queue is down).
  5. A QueueHealthException is reported via report(), which notifies any configured error tracking service (Bugsnag, Sentry, etc.) without interrupting the command flow.
  6. A flag file tracks alert state — by default only one alert per incident, but repeated alerts with backoff are supported.
  7. When the queue recovers (heartbeat is recent again and the flag exists), a recovery email is sent and the flag is cleared.
  8. After the check, a new QueueHealthCheckJob is dispatched to write the next heartbeat.

State Files

File Purpose
storage/logs/queue-health.log ISO 8601 timestamp of the last successful job execution
storage/logs/queue-health-alert.flag JSON file tracking alert state (timestamp and count). Exists only while the queue is down.

Installation

composer require therealedatta/laravel-queue-health-check
php artisan vendor:publish --tag=queue-health-config

Configuration

Add to your .env:

QUEUE_HEALTH_ALERT_EMAIL=admin@example.com,devops@example.com
QUEUE_HEALTH_CHECK_INTERVAL=5
Variable Description Default
QUEUE_HEALTH_ALERT_EMAIL Comma-separated list of email recipients null (disabled)
QUEUE_HEALTH_CHECK_INTERVAL Minutes between checks 5
QUEUE_HEALTH_ALERT_REPEAT_INTERVAL Alert repeat interval in minutes (see below) null (one alert per incident)

If QUEUE_HEALTH_ALERT_EMAIL is missing or empty, the package does nothing.

Alert Repeat Interval

Controls how often alerts are re-sent while the queue remains down:

  • Not set / null: only one alert per incident (default)
  • Single value (e.g. 60): re-send every 60 minutes
  • Comma-separated backoff (e.g. 5,15,30,60): the first alert is immediate, then re-alert after 5 min, then 15, then 30, then every 60 minutes indefinitely

A 30-second tolerance is applied to repeat intervals to account for cron scheduling jitter, ensuring alerts fire on time rather than being delayed by one cycle.

# Re-alert every hour
QUEUE_HEALTH_ALERT_REPEAT_INTERVAL=60

# Backoff: immediate → 5min → 15min → 30min → every 60min
QUEUE_HEALTH_ALERT_REPEAT_INTERVAL=5,15,30,60

Manual Queue Test

You can manually verify the queue is working by dispatching a test email:

php artisan queue-health:test user@example.com

If no email is provided, it falls back to the configured QUEUE_HEALTH_ALERT_EMAIL:

php artisan queue-health:test

The command dispatches a job through the queue. When the worker processes it, it sends an email with timing information (dispatch time, processing time, and delay). If the delay exceeds 60 seconds, the email subject and body will flag it as a warning.

If no email is configured at all, the command reports a QueueHealthException to your error tracking service so the misconfiguration doesn't go unnoticed.

Error Tracking Integration

Each time an alert is sent, the package calls report(new QueueHealthException(...)). This means any error tracking service configured in your Laravel app (Bugsnag, Sentry, Flare, etc.) will automatically receive the exception — providing a secondary alert channel that doesn't depend on email delivery.

Requirements

  • PHP >= 8.1
  • Laravel 10, 11, or 12

Make sure php artisan schedule:run is in your crontab:

* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1

Architecture

src/
├── QueueHealthCheckServiceProvider.php   # Registers config, commands, and schedule
├── Commands/
│   ├── QueueHealthCheckCommand.php       # Checks heartbeat, sends alert/recovery emails
│   └── QueueHealthTestCommand.php        # Manual test: dispatches a test email via the queue
├── Exceptions/
│   └── QueueHealthException.php          # Reported to error tracking services
└── Jobs/
    ├── QueueHealthCheckJob.php           # Writes heartbeat timestamp to file
    └── QueueHealthTestJob.php            # Sends test email with timing diagnostics

ServiceProvider

  • Merges and publishes the config file
  • Registers the artisan commands
  • Schedules queue-health:check via $schedule->command()->cron() based on the configured interval

QueueHealthCheckJob

  • Implements ShouldQueue with 3 retries and a flat 5s backoff
  • Writes now()->toIso8601String() to storage/logs/queue-health.log

QueueHealthCheckCommand

  • Exits silently if config is missing
  • On first run (no heartbeat file), dispatches the job without alerting
  • Threshold formula: (check_interval_minutes * 2 * 60) - 1 seconds
  • Alert email subject: [AppName] ALERT: Queue worker unresponsive
  • Recovery email subject: [AppName] RECOVERED: Queue worker is back
  • Both emails are sent synchronously via Mail::raw()
  • Reports QueueHealthException via report() on each alert

Testing

composer install
vendor/bin/phpunit

Tests use Orchestra Testbench and cover:

  1. No config → does nothing, no mail, no job dispatched
  2. No heartbeat file → no alert (first run)
  3. Recent heartbeat → no alert
  4. Old heartbeat, no flag → sends alert + creates flag + reports exception
  5. Old heartbeat, flag exists, no repeat interval → no mail (already alerted)
  6. Old heartbeat, flag exists, repeat interval elapsed → re-sends alert
  7. Old heartbeat, flag exists, repeat interval not elapsed → no mail
  8. Backoff schedule follows configured steps
  9. Backoff schedule repeats last step indefinitely
  10. Recent heartbeat, flag exists → sends recovery email, removes flag
  11. Multiple recipients
  12. Job writes heartbeat file correctly
  13. Test command dispatches job with provided email
  14. Test command falls back to config email
  15. Test command reports exception when no email configured
  16. Test job sends email with timing info
  17. Test job warns when queue processing is delayed

License

MIT

therealedatta/laravel-queue-health-check 适用场景与选型建议

therealedatta/laravel-queue-health-check 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「monitoring」 「laravel」 「health-check」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 therealedatta/laravel-queue-health-check 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 therealedatta/laravel-queue-health-check 我们能提供哪些服务?
定制开发 / 二次开发

基于 therealedatta/laravel-queue-health-check 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-30