rpq / client
纯PHP实现的Redis优先级队列客户端
Requires
- php: ^7.1
- ext-redis: ^3.1.2 || ^4.0.2
- ramsey/uuid: ^3.7
Requires (Dev)
- phpunit/phpunit: ^7.0.2
This package is auto-updated.
Last update: 2024-09-09 14:00:15 UTC
README
RPQ-Client 是用纯PHP编写的 Redis 优先级队列实现。此存储库包含客户端代码库,可用于从应用程序中安排作业。此外,此代码库由 RPQ 服务器 实现用于与作业交互和处理。
请注意,此代码库正在不断发展。在创建标记版本之前,API 可能会随时更改。
安装
可以通过 Composer 将 RPQ-Client 添加到您的应用程序中。
composer require rpq/client
请注意,RPQ-Client 需要 PHPRedis。
用法
RPQ 客户端附带了一些选项来实例化队列。要开始使用 RPQ 客户端,连接到您的 Redis 实例,然后将该 Redis 实例传递给 RPQ\Client
对象。
// Create a new Redis instance $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); // Create a new client object $client = new RQP\Client($redis); //$rpq = new RPQ\Client($redis, 'namespace'); // Returns the queue object for interaction $queue = $client->getQueue(); // $queue = $client->getQueue('another-queue-name');
添加作业
只需将完全限定类名推送到队列中,即可为立即执行安排作业。
// Push a new task called `Ping` to the priority queue with the default priority $queue->push('Ping');
作业参数可以是复杂数组。只要详情是可序列化的 JSON,就可以传递给 RPQ。作业的默认优先级为 1
。优先级更高的作业将先于优先级较低的作业执行。优先级范围从 PHP_INT_MIN
到 PHP_INT_MAX
。
重试可以是 boolean
或 integer
。如果 retry
设置为 true
,则作业将不断重新安排,直到通过。如果 retry
设置为 false
,则不会尝试重试作业。如果 retry
设置为整数,则将尝试 恰好 n
次重试,然后作业将失败。
将作业推送到堆栈后,push
方法将返回一个 Job
实例,可用于确定作业的状态和其他信息。
// Alternatively, we can specify args, the queue name, a different priority, and whether or not RQP-Server should attempt to retry the job if it fails. $args = [ 'arg1', 'arg2' => [ 'stuff' => 1, 'more' => false, 'foo' => 'bar' ], 'arg3' => true ]; $retry = true; $priority = 3; // Push a new task onto the priority queue, get a UUIDv4 JobID back in response $job = $queue->push('Worker', $args, $retry, $priority);
请注意,不应将复杂对象作为参数传递。如果您需要访问复杂对象,应在作业类中重新实例化它。
未来调度
可以通过指定 at
参数来安排未来作业,该参数表示您希望作业执行的 Unix 时间戳。
$at = \strtotime('+1 hour'); $job = $queue->push('Worker', $args, $retry, $priority, $at);
请注意,
at
参数声明作业将执行的 最早 时间,但不保证作业将在该时间执行。调度程序将在可能的情况下优先考虑未来作业,但其他作业可能会根据优先级优先执行。如果您需要精确的时间,则作业应具有PHP_MAX_INT
的优先级,并且您应确保您的作业队列有足够的工人以防止作业执行延迟。
队列统计信息
以下是如何检索队列的详细信息
$queue->getStats()->get();
stats 命令将返回一个包含队列中元素数量以及给定日期传递、失败、取消和重试作业的详细信息的数组。
要获取不同日期的统计信息,请使用 get()
调用并使用 Y-m-d
格式的日期。