dovutuan/laravel-sji
Composer 安装命令:
composer require dovutuan/laravel-sji
包简介
Queue middleware: only one job runs at a time per instance
关键字:
README 文档
README
Laravel Queue middleware: on each instance (host/container), only one job runs at a time (per key).
Lock scope: instance_id + key — so it will not block jobs on other instances.
Installation
composer require dovutuan/laravel-sji
php artisan vendor:publish --tag=config --provider="Dovutuan\\Sji\\ServiceProvider"
.env (Redis recommended):
CACHE_STORE=redis
SJI_LOCK_STORE=redis
SJI_TTL=600
SJI_BLOCK_FOR=0
SJI_INSTANCE_RESOLVER=hostname
# SJI_INSTANCE_ID=app-1 # optional, for stable ID in containers
Usage
use Illuminate\\Contracts\\Queue\\ShouldQueue; use Dovutuan\\Sji\\SjiMiddleware; class HeavyJob implements ShouldQueue { public function __construct(public string $group = 'global') {} public function middleware(): array { return [ // per-instance lock: one job at a time on this instance new SjiMiddleware( key: "job:{$this->group}", ttl: 600, blockFor: 0, store: 'redis', releaseAfter: 10 ), ]; } public function handle(): void { // ... } }
Artisan Command
Generate and write a new SJI_INSTANCE_ID to your .env file:
php artisan sji:make-instance-id
Options:
--forceor-f→ overwrite existing value.
Example:
$ php artisan sji:make-instance-id
SJI_INSTANCE_ID set to: 9a0b8e0e-8d1d-4d7c-b229-1f4fbc9f1d2a
.env will now contain:
SJI_INSTANCE_ID=9a0b8e0e-8d1d-4d7c-b229-1f4fbc9f1d2a
Difference from WithoutOverlapping
WithoutOverlapping: locks cluster-wide (if all workers share the same cache), blocking across all instances.- SjiMiddleware: locks per instance → ensures only one job per instance, but allows jobs to run in parallel across multiple instances.
Notes
- Set
TTL≥ max job runtime + buffer to avoid early lock expiration. - In containerized environments, consider setting
SJI_INSTANCE_IDexplicitly for a stable ID. - Can be combined with
ShouldBeUnique/ShouldBeUniqueUntilProcessingto prevent duplicate enqueues, and idempotent job design for retry safety. - For Horizon, ensure
timeoutandretry_afterare configured properly.
Development
Test
Uses Orchestra Testbench.
composer install vendor/bin/phpunit
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-25