keystone/queue

使用任何队列服务创建和处理后台任务

0.1.0-alpha1 2017-02-13 01:44 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:50 UTC


README

Build Status

一个PHP库,可用于使用任何队列服务创建和处理后台任务。

支持的队列服务

特性

  • 通过提供程序/发布者接口与任何队列服务兼容。
  • 中间件,可以钩入处理流程(灵感来自 PSR-15)。
  • 将任务消息路由到在 PSR-11/Symfony 容器中注册为服务的工作者。

中间件

  • 自动关闭超时的 Doctrine DBAL 连接。
  • 自动清除 Doctrine ORM 管理器以释放内存。
  • 限制消费者的最大执行时间。
  • 限制消费者将处理的消息的最大数量。
  • 限制消费者允许使用的最大内存量。
  • 使用指数退避策略重试失败的任务。
  • 处理信号以安全地终止消费者进程。

要求

需要PHP 7.0或更高版本。

入门

使用Composer安装库。

composer require keystone/queue

为任务创建一个消息类。

use Keystone\Queue\Message;

class HardMessage implements Message
{
    public $name;
    public $count;

    public function __construct(string $name, int $count)
    {
        $this->name = $name;
        $this->count = $count;
    }

    public function getKey(): string
    {
        // The message key is used to determine which queue to publish to.
        return 'hard';
    }
}

创建一个能够处理消息的工作者类。

class HardWorker
{
    public function process(HardMessage $message)
    {
        // Do some work to process the message.
    }
}

在您的应用程序中发布消息。

use Keystone\Queue\Publisher;

$publisher = new Publisher(...);
// The message is serialized when publishing and unserialized when consuming
$publisher->publish(new HardMessage('Billy', 12));

在长时间运行的过程中消费消息。

use Keystone\Queue\Consumer;
use Keystone\Queue\Provider;

$provider = new Provider(...);
$consumer = new Consumer($provider, ...);
// The consumer will poll the queue for new messages and process them.
$consumer->consume();

致谢

许可

在MIT许可下发布。有关详细信息,请参阅捆绑的LICENSE文件。