softwarepunt / php-resque
基于Redis的后端库,用于创建后台任务并在以后处理它们。基于Ruby的resque。
v1.4.0
2023-08-01 18:57 UTC
Requires
- php: >=8.2
- ext-pcntl: *
- colinmollenhour/credis: dev-master
- psr/log: ~1.0
Requires (Dev)
- phpunit/phpunit: ^10
Suggests
- ext-proctitle: Allows php-resque to rename the title of UNIX processes to show the status of a worker.
- ext-redis: Native PHP extension for Redis connectivity. Credis will automatically utilize when available.
This package is auto-updated.
Last update: 2024-08-29 15:20:37 UTC
README
php-resque是一个基于Redis的库,用于排队和运行后台任务。
这是一个对chrisboulton/php-resque
进行轻微修改和改进的分支,兼容PHP 8.2+。
⚠️ 不推荐用于新项目。我们只维护这个分支以支持旧项目。
安装
使用composer添加包
composer require softwarepunt/php-resque
使用
配置
如果你不使用默认的Redis配置,请手动设置后端
Resque::setBackend('localhost:6379');
定义任务
每个任务应在其自己的类中,并包含一个perform方法
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 an instance of "My_Job" in the "default" queue Resque::enqueue('default', 'MyApp\Greeter_Job', ['name' => "Hank"]);
出队任务
可以从队列中删除任务
// 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']);
工作者
使用QUEUE
环境变量启动工作者以开始处理该队列中的任务
QUEUE=default php vendor/bin/resque
环境变量
你可以在工作者上设置以下环境变量
事件
你可以监听特定事件以跟踪状态变化
Resque_Event::listen('eventName', $callback);
$callback
可以是PHP中任何可以通过call_user_func_array
调用的东西。
高级
获取当前任务信息
你可以在执行中的任务内获取ID、队列等信息
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) }