mle86/wq-redis

使用phpredis扩展的mle86/wq Redis模块

v1.0.2 2018-11-01 19:33 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:38 UTC


README

此包包含PHP类mle86\WQ\WorkServerAdapter\RedisWorkServer

它通过实现其WorkServerAdapter接口补充了mle86/wq包。

它通过phpredis扩展连接到Redis服务器。

版本和兼容性

这是mle86/wq-redis版本1.0.2

它是为mle86/wq的1.0.0版本开发的,应与所有未来的1.x版本兼容。

安装和依赖

$ sudo apt install php-redis  # to install the phpredis extension
$ composer require mle86/wq-redis

它依赖于PHP 7.1、mle86/wqphpredis扩展。

类参考

(class mle86\WQ\WorkServerAdapter\RedisWorkServer implements WorkServerAdapter)

它连接到Redis服务器。

由于Redis没有延迟条目、保留条目或埋藏条目,此类使用多个自定义解决方案来模拟这些功能。

对于每个使用的$workQueue,此类将创建多个Redis键

  • _wq.$workQueue(准备中的工作 – 列表)
  • _wq_delay.$workQueue(延迟工作 – 有序集)
  • _wq_buried.$workQueue(埋藏工作 – 列表)

延迟机制受到了这个StackOverflow响应的启发。

  • public function __construct (\Redis $serverConnection)
    接受一个已经配置好的Redis实例来工作。不会尝试自己建立连接 - 使用connect()工厂方法代替,或者在使用此构造函数之前使用Redis::connect()
  • public function connect ($host = "localhost", $port = 6379, $timeout = 0.0, $retry_interval = 0)
    工厂方法。这将自行创建一个新的Redis实例。
    有关参数描述,请参阅Redis::connect()

WorkServerAdapter接口中记录的接口方法

  • public function storeJob (string $workQueue, Job $job, int $delay = 0)
  • public function getNextQueueEntry ($workQueue, int $timeout = DEFAULT_TIMEOUT) : ?QueueEntry
  • public function buryEntry (QueueEntry $entry)
  • public function requeueEntry (QueueEntry $entry, int $delay, string $workQueue = null)
  • public function deleteEntry (QueueEntry $entry)

使用示例

<?php
use mle86\WQ\WorkServerAdapter\RedisWorkServer;
use mle86\WQ\WorkProcessor;
use mle86\WQ\Job\Job;

$processor = new WorkProcessor( new RedisWorkServer("localhost") );

while (true) {
    $processor->processNextJob("webhook", function(Job $job) {
        $job->...;
    });
}

此命令将永久执行本地Redis服务器“webhook”队列中的所有工作。但是,如果其中一个工作抛出异常,则将终止 - 您可能想在processNextJob()调用周围添加日志try-catch块,如WQ的“快速入门”示例中所示。