philiprehberger/php-background-jobs
Composer 安装命令:
composer require philiprehberger/php-background-jobs
包简介
Lightweight background job queue with file-based driver
关键字:
README 文档
README
Lightweight background job queue with file-based driver.
Requirements
- PHP 8.2+
Installation
composer require philiprehberger/php-background-jobs
Usage
Define a Job
Implement the Job interface:
use PhilipRehberger\BackgroundJobs\Contracts\Job; class SendEmailJob implements Job { public function __construct( private readonly string $to, private readonly string $subject, ) {} public function handle(): void { // Send the email... } }
Create a Queue
Use the built-in FileDriver to store jobs as JSON files:
use PhilipRehberger\BackgroundJobs\Drivers\FileDriver; use PhilipRehberger\BackgroundJobs\Queue; $driver = new FileDriver('/path/to/storage/queue'); $queue = new Queue($driver);
Push Jobs
// Push a job for immediate processing $id = $queue->push(new SendEmailJob('user@example.com', 'Welcome!')); // Push a job with a delay (in seconds) $id = $queue->later(new SendEmailJob('user@example.com', 'Reminder'), 3600);
Process Jobs with the Worker
use PhilipRehberger\BackgroundJobs\Worker; // Process the next available job $processed = Worker::processNext($queue); // Process with a custom max attempts limit $processed = Worker::processNext($queue, maxAttempts: 5);
Queue Management
// Get the number of pending jobs $count = $queue->size(); // Clear all jobs $queue->clear(); // Pop a job manually $payload = $queue->pop(); if ($payload !== null) { $job = $payload->resolveJob(); $job->handle(); }
Custom Queue Driver
Implement the QueueDriver interface to use a different storage backend:
use PhilipRehberger\BackgroundJobs\Contracts\QueueDriver; use PhilipRehberger\BackgroundJobs\JobPayload; class RedisDriver implements QueueDriver { public function push(JobPayload $payload): void { /* ... */ } public function pop(): ?JobPayload { /* ... */ } public function size(): int { /* ... */ } public function clear(): void { /* ... */ } }
Lifecycle Hooks
Register callbacks that fire when a job succeeds or fails:
use PhilipRehberger\BackgroundJobs\BaseJob; class SendEmailJob extends BaseJob { public function __construct( private readonly string $to, ) {} public function handle(): void { // Send the email... } } $job = new SendEmailJob('user@example.com'); $job->onSuccess(function ($job) { echo "Job completed after {$job->getAttempts()} attempt(s)."; }); $job->onFailure(function ($job, \Throwable $e) { echo "Job failed: {$e->getMessage()}"; }); $queue->push($job);
Error Handling
Failed jobs throw a JobFailedException:
use PhilipRehberger\BackgroundJobs\Exceptions\JobFailedException; try { Worker::processNext($queue); } catch (JobFailedException $e) { echo $e->getMessage(); echo $e->payload->id; // Job ID echo $e->payload->jobClass; // Original job class echo $e->payload->attempts; // Number of attempts }
API
| Class | Method | Description |
|---|---|---|
Queue |
push(Job $job): string |
Push a job, returns job ID |
Queue |
later(Job $job, int $delaySeconds): string |
Push a delayed job |
Queue |
pop(): ?JobPayload |
Pop the next available job |
Queue |
size(): int |
Get pending job count |
Queue |
clear(): void |
Remove all jobs |
Queue |
pending(): array |
Get all pending job payloads |
Worker |
processNext(Queue $queue, int $maxAttempts = 3): bool |
Process next job |
BaseJob |
onSuccess(callable $callback): self |
Register a success lifecycle hook |
BaseJob |
onFailure(callable $callback): self |
Register a failure lifecycle hook |
BaseJob |
getAttempts(): int |
Get the number of attempts |
JobPayload |
resolveJob(): Job |
Deserialize the job instance |
JobPayload |
isAvailable(): bool |
Check if job is ready to process |
JobPayload |
withIncrementedAttempts(): self |
Clone with incremented attempts |
JobPayload |
toArray(): array |
Serialize to array |
JobPayload |
fromArray(array $data): self |
Restore from array |
Development
composer install vendor/bin/phpunit vendor/bin/pint --test vendor/bin/phpstan analyse
Support
If you find this project useful:
License
philiprehberger/php-background-jobs 适用场景与选型建议
philiprehberger/php-background-jobs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「background」 「async」 「worker」 「jobs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiprehberger/php-background-jobs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiprehberger/php-background-jobs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiprehberger/php-background-jobs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a background task processing module.
PHP AMQP Binding Library
The bundle for easy using json-rpc api on your project
An async event for hyperf.
A Laravel package to monitor queue jobs.
A minimum library to run background processes asynchronously.
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-13