bytetcore/queue-unique-runner
Composer 安装命令:
composer require bytetcore/queue-unique-runner
包简介
Ensure Laravel queue jobs run on only one server instance at a time with database or Redis distributed locking, heartbeat, and crash recovery.
README 文档
README
Ensure your Laravel queue jobs run on only one server instance at a time using distributed locking. Includes automatic crash recovery, lock heartbeat, and supports both Database and Redis drivers.
The Problem
When running multiple queue workers across different servers, sometimes you have jobs that must not run concurrently under any circumstances (e.g., end-of-day financial calculations, syncing large datasets with third-party APIs).
While Laravel's WithoutOverlapping middleware is great, if a server crashes midway through a job, the lock gets permanently stuck until manually cleared.
The Solution
queue-unique-runner provides robust distributed locking with:
- Heartbeat mechanism: Periodically extends the lock while the job is actively running.
- Crash recovery: If a server crashes or the worker is abruptly killed, the heartbeat stops, the lock expires automatically via TTL, and another server can safely retry the job.
- Per-class or Per-instance locking: Lock the entire job class, or lock per unique payload.
Installation
You can install the package via composer:
composer require bytetcore/queue-unique-runner
If using the Database driver (the default), publish and run the migrations:
php artisan vendor:publish --tag="queue-unique-runner-migrations"
php artisan migrate
Optionally, publish the config file:
php artisan vendor:publish --tag="queue-unique-runner-config"
Usage
Simply add the RunsOnUniqueRunner trait to your job:
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Bytetcore\QueueUniqueRunner\Traits\RunsOnUniqueRunner; class ProcessFinancialAudit implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use RunsOnUniqueRunner; // <-- Add this trait public function handle(): void { // This code is guaranteed to only run on one server at a time. // If a server crashes here, the lock automatically expires. } }
Configuration per Job
You can override the default configuration for specific jobs by overriding these methods:
class SyncUserData implements ShouldQueue { use RunsOnUniqueRunner; public int $userId; public function __construct(int $userId) { $this->userId = $userId; } // Lock scope: 'class' (only one SyncUserData job anywhere) // or 'instance' (one SyncUserData job per unique payload) public function queueUniqueRunnerScope(): string { return 'instance'; } // Custom identifier for 'instance' scope public function queueUniqueRunnerIdentifier(): ?string { return 'user:' . $this->userId; } // How long the lock should be held (in seconds) public function queueUniqueRunnerTtl(): int { return 600; // 10 minutes } // How long to wait before retrying if another server holds the lock public function queueUniqueRunnerRetryDelay(): int { return 60; // Wait 60 seconds } }
Drivers
Database (Default)
Creates a queue_unique_runner_locks table. Uses unique constraints to guarantee atomic locks.
It is recommended to periodically run the prune command to clean up expired locks from the database:
# Add this to your Console/Kernel.php schedule $schedule->command('queue-unique-runner:prune')->daily();
Redis
Uses SET NX EX commands and Lua scripts for atomic operations. Extremely fast and automatically handles expired lock cleanup.
Change your .env:
QUEUE_UNIQUE_RUNNER_DRIVER=redis QUEUE_UNIQUE_RUNNER_REDIS_CONNECTION=default
Requirements
- PHP 8.0+
- Laravel 9.0+
- (Optional but recommended)
pcntlextension for Heartbeat functionality
Testing
composer test
bytetcore/queue-unique-runner 适用场景与选型建议
bytetcore/queue-unique-runner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「laravel」 「job」 「mutex」 「heartbeat」 「distributed-lock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bytetcore/queue-unique-runner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bytetcore/queue-unique-runner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bytetcore/queue-unique-runner 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP AMQP Binding Library
Provides a possibility to auto-setup crontabs required for project based on configuration file.
SlimQ Enables You to push jobs to background workers
A Laravel package to monitor queue jobs.
PHP client for Kue priority job queue API
A lightweight task queue on top of rybakit/phive-queue
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2026-04-03