journey/queue

用PHP编写的简单、分层动作-测试-回调队列

v0.2.2 2015-04-06 14:30 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:49:17 UTC


README

Queue 是一个用PHP编写的简单、分层动作-测试-回调队列。PHP线程将等待必要的时间以完成整个队列(使用 Journey\Daemon)。

安装

使用composer进行安装和加载。

使用

队列由 QueueItem 对象的注册表组成。每个 QueueItem 有3个元素

  • 动作
    • __construct(Callable)
    • setAction(Callable)
  • 检查
    • unitl(Callable)
    • succeedWhen(Callable)
  • 回调
    • then(Callable)
    • after(Callable)

队列首先执行动作。然后队列将调用检查以确定动作是否完成,直到接收到真值。最后,在检查通过后,队列将调用回调。

<?php

include "vendor/autoload.php";

$queue = new Journey\Queue\Queue;

$queue->add(function () {
    // The initial action
})->succeedWhen(function () {
    // A check to see if the action succeeded
})->then(function (Queue $queue) {
    // Do some action
});

$queue->run();

要创建层次结构,只需在成功回调中添加另一个 QueueItem(如上例中的 then())。 $queue->add(new QueueItem...)

注意:检查方法还支持第二个参数 (可选) $interval = 0,它定义了每个检查在再次被调用之前应该等待多长时间(非阻塞)。一个有用的例子是,如果你正在调用一个限制请求的API以获取状态,并且你只想每30秒检查一次,而不是每秒检查30次。