unionofrad/li3_queue

此插件提供了一种简单处理工作队列的方法。

安装: 151

依赖项: 0

建议者: 0

安全: 0

星标: 12

关注者: 7

分支: 10

开放问题: 0

类型:lithium-library

dev-master 2017-07-03 12:54 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:01:00 UTC


README

Christopher Garvis & Olivier Louvignes 编写

描述

此插件提供了一种简单处理工作队列的方法,目前支持以下类型:

安装

  1. 要启用此库,请在app/config/bootstrap/libraries.php文件末尾添加以下行:

    Libraries::add('li3_queue');
  2. 然后,在app/config/bootstrap/queues.php中配置您的队列。

    use li3_queue\storage\Queue;
    
    Queue::config(array('default' => array(
        'adapter' => 'Beanstalk',
        'host' => '127.0.0.1',
        'port' => 11300
    )));
  3. 将新的配置文件包含到app/config/bootstrap.php中。

    /**
     * Include this file if your application uses one or more queues.
     */
    require __DIR__ . '/bootstrap/queues.php';
  4. 现在您可以在应用程序中使用配置的队列了。

    use li3_queue\storage\Queue;
  5. 关于几个PHP版本的stream_get_line函数存在一些已知的错误,可能导致在\r\n EOL包上不正确地失败返回。不幸的是,此错误影响到了12.04版本中提供的PHP版本(php5.3.10-1)。

设置

  1. 如果autoConfirm为true,则消息将在服务器上自动确认,并且每次您使用Queue::read()Queue::consume()时。这意味着您不需要使用$message->confirm(),并且无法使用$message->requeue()重新入队。

AMQP接口

配置

您的队列配置将位于app/config/bootstrap/queues.php中,并可以包含以下选项中的任何一项:

1. 基本配置
Queue::config(array(
    'default' => array(
        'adapter' => 'AMQP',
        'host' => '127.0.0.1',
        'login' => 'guest',
        'password' => 'guest',
        'port' => 5672,
        'vhost' => '/',
        'exchange' => 'li3.default',
        'queue' => 'li3.default',
        'routingKey' => null,
        'autoConfirm' => false,
        'cacert' => null,
        'cert' => null,
        'key' => null,
        'verify' => true
    )
));
2. 发布/订阅

要将AMQP适配器配置为发布/订阅功能,您可以通过以下方式创建多个队列配置:

Queue::config(array(
    'publish' => array(
        'adapter' => 'AMQP',
        'exchangeType' => AMQP_EX_TYPE_FANOUT,
        'exchange' => 'li3.publish',
        'queue' => false,
    ),
    'subscribe.1' => array(
        'adapter' => 'AMQP',
        'exchangeType' => AMQP_EX_TYPE_FANOUT,
        'exchange' => 'li3.publish',
        'queue' => 'li3.subscribe.1'
    ),
    'subscribe.2' => array(
        'adapter' => 'AMQP',
        'exchangeType' => AMQP_EX_TYPE_FANOUT,
        'exchange' => 'li3.publish',
        'queue' => 'li3.subscribe.2'
    )
));

附加说明

  1. routingKey为null时,默认值将设置为与queue相同的值,只有在高级配置中才需要设置路由键。

Beanstalk接口

配置

您的队列配置将位于app/config/bootstrap/queues.php中,并可以包含以下选项中的任何一项:

Queue::config(array(
    'default' => array(
        'adapter' => 'Beanstalk',
        'host' => '127.0.0.1',
        'port' => 11300,
        'tube' => 'default',
        'autoConfirm' => false
    )
));
  • 请查看源代码以获取更多配置信息。

使用方法

  1. 写入消息

    Queue::write('default', 'message');
  2. 读取消息

    $message = Queue::read('default');
  3. 确认或重新入队消息

    一旦您从队列中读取了消息,您将需要使用以下方法之一来确认其成功:

    $message->confirm();

    或使用以下方法重新入队您的消息:

    $message->requeue();
  4. 消费消息

    Queue::consume('default', function($message) {
        // Do something with message
        if($success) {
            // Confirm message
            $message->confirm();
        }
        // Requeue message
        $message->requeue();
    });

    消费消息是一个阻塞操作,它将检索下一个可用的消息并将其传递给回调。在回调中返回false将退出消费。

错误和贡献

欢迎贡献补丁!发送pull请求。

Github上发布问题

许可证

Copyright (c) 2012, Union of RAD http://union-of-rad.org
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
    * Neither the name of Lithium, Union of Rad, nor the names of its contributors
        may be used to endorse or promote products derived from this software
        without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.