softwarepunt/php-resque
Composer 安装命令:
composer require softwarepunt/php-resque
包简介
Redis backed library for creating background jobs and processing them later. Based on resque for Ruby.
关键字:
README 文档
README
php-resque is a Redis-based library for enqueuing and running background jobs.
This is a lightly maintained fork of chrisboulton/php-resque, with fixes and improvements, compatible with PHP 8.2+.
⚠️ Not recommended for new projects. We are only maintaining this for legacy projects.
Installation
Add the package with composer:
composer require softwarepunt/php-resque
Usage
Configuration
If you are not using default Redis configuration, set the backend manually:
Resque::setBackend('localhost:6379');
Define jobs
Each job should be in its own class, and include a perform method:
namespace MyApp; class Greeter_Job { public function setUp() { // Optional: Set up before (called before perform()) } public function perform() { // Required: Main work method // Perform work; context data is accessible from $this->args echo "Hello, {$this->args['name']}!"; } public function tearDown() { // Optional: Tear down after (called after job finishes) } }
Enqueue jobs
Jobs instances are placed in specific queues with a set of context data (args):
// Enqueue an instance of "My_Job" in the "default" queue Resque::enqueue('default', 'MyApp\Greeter_Job', ['name' => "Hank"]);
Dequeue jobs
Jobs can be removed from queues:
// Remove all jobs of a certain type from a queue Resque::dequeue('default', ['MyApp\Greeter_Job']); // Remove specific job from a queue Resque::dequeue('default', ['MyApp\Greeter_Job' => '087df5819a790ac666c9608e2234b21e']);
Workers
Start a worker with the QUEUE environment variable to begin processing jobs from that queue:
QUEUE=default php vendor/bin/resque
Environment variables
You can set the following environment variables on the worker:
| Name | Description |
|---|---|
QUEUE |
Required. Defines one or more comma-separated queues to process tasks from. Set to * to process from any queue. |
APP_INCLUDE |
Optional. Defines a bootstrap script to run before starting the worker. |
PREFIX |
Optional. Prefix to use in Redis. |
COUNT |
Optional. Amount of worker forks to start. If set to > 1, the process will start the workers and then exit immediately. |
SCHEDULE |
Optional. An expression with a from/until time, e.g. 22:00-06:00 to only run tasks between 10pm and 6am. The worker is paused outside of the schedule. Relative to default timezone (date_default_timezone_set). |
VERBOSE |
Optional. Forces verbose log output. |
VVERBOSE |
Optional. Forces detailed verbose log output. |
Events
You can listen for specific events to track status changes:
Resque_Event::listen('eventName', $callback);
$callback may be anything in PHP that is callable by call_user_func_array.
| Name | Description | Event arguments |
|---|---|---|
beforeFirstFork |
Called once, as a worker initializes. | Resque_Worker $worker |
beforeFork |
Called before php-resque forks to run a job. | Resque_Job $job |
afterFork |
Called after php-resque forks to run a job (but before the job is run). | Resque_Job $job |
beforePerform |
Called once per job run, before setUp and perform. |
Resque_Job $job |
afterPerform |
Called once per job run, after perform and tearDown. |
Resque_Job $job |
onFailure |
Called whenever a job fails. | Exception $ex, Resque_Job $job |
beforeEnqueue |
Called before a job has been queued using enqueue. |
string $class, ?array $args, string $queue, string $id |
afterEnqueue |
Called after a job has been queued using enqueue. |
string $class, ?array $args, string $queue, string $id |
Advanced
Getting self job info
You can get the ID, queue, etc. from within an executing job:
public function perform() { echo $this->queue; // my_queue (Queue name) echo $this->job->payload['id']; // aa4c8b768d7b89a1b90d000cfefdf785 (Job ID) echo $this->job->payload['queue_time']; // 1695993877.3551 (Job enqueued at) }
softwarepunt/php-resque 适用场景与选型建议
softwarepunt/php-resque 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 685 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「background」 「resque」 「job」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 softwarepunt/php-resque 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 softwarepunt/php-resque 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 softwarepunt/php-resque 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a background task processing module.
Redis backed library for creating background jobs and processing them later. Based on resque for Ruby.
Microservice RPC through message queues.
The CodeIgniter Redis package
Redis backend library for creating background jobs and processing them later. PHP port based on resque for Ruby.
贝嘟分布式缓存扩展
统计信息
- 总下载量: 685
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-01