gos/yolo

高级重试实现

v0.2.2 2015-09-29 16:20 UTC

This package is auto-updated.

Last update: 2024-09-18 06:22:54 UTC


README

高级重试实现。

安装

composer require gos/yolo

示例

执行操作

$pusher->push($notification); #send notification over network

使用 YOLO

use Gos\Component\Yolo\Yolo;

$maxRetry = 10; // Default is 5
$timeout = 10; // Default is 5 (in second), set 0 for not timeout
$allowedException = ['RuntimeException']; // empty by default, consider exception as a "success"
$yoloPush = new Yolo(array($pusher, 'push'), array($notification), $maxRetry, $timeout);
$yoloPush->run();

有时我们需要更多技巧来在 web 服务上重试。

use Gos\Component\Yolo\Yolo;

$yoloPush = new Yolo(array($pusher, 'push'), array($notification));
$yoloPush->tryUntil(function(){
    $result = false;
    if ($fp = @fsockopen('my-web-service.dev', 1337, $errCode, $errStr, 1)) {
        $result = true;
        fclose($fp);
    }

    return $result;
});

如果你的操作成本很高,请在服务可用时执行,而不是进行无效的重试。

你还可以做

use Gos\Component\Yolo\Yolo;

$yoloPush = new Yolo(array($pusher, 'push'), array($notification));
$yoloPush->tryUntil($pusher);

通过在你的对象上实现 Gos\Component\Yolo\YoloInterface。添加 isAvailable 并在它正常时返回 true。

你还可以将日志记录器附加到 yolo(我们实现了 Psr\Log\LoggerAwareInterface)。技巧

use Gos\Component\Yolo\Yolo;

$yolo = new Yolo(function(){});
$yolo->setLogger($mySwagPsrLogger);

内置回调

ping 回

use Gos\Component\Yolo\Yolo;
use Gos\Component\Yolo\Callback\PingBack;

$pingger = new PingBack('127.0.0.1', 80);

$yoloPush = new Yolo(function(){});
$yoloPush->tryUntil($pingger);