jojo1981 / polling
基于结果检查器条件重试操作的多线程库。
3.0.1
2023-08-29 09:31 UTC
Requires
- php: ^7.4|^8.0
- jojo1981/contracts: ^1.0
- jojo1981/typed-collection: ^5.0
Requires (Dev)
- dg/bypass-finals: ^1.3
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ^9.0
README
作者:Joost Nijhuis <jnijhuis81@gmail.com>
一个简单的多线程库,只要结果检查器没有返回成功结果,就会一直重试轮询操作,最多重试指定次数,并在两次重试之间有指定的延迟。
执行器回调可以执行任何任务,并返回任何结果。结果检查器应该能够检查执行器返回的结果是否成功,并必须返回一个布尔值。可选地,您可以添加异常检查器。这可以省略,并在执行器中处理,但为了关注点的分离,您可以拆分这个逻辑。延迟(微秒)可以给出,默认为10000000微秒(10秒)。延迟将在重试之间。最大轮询次数可以给出,默认为10。最大轮询次数是执行器最多被调用的次数,包括第一次调用。执行器、结果检查器、异常检查器和开始轮询的默认参数可以省略。这可以很方便地保持回调函数的纯净。
安装
库
git clone https://github.com/jojo1981/polling.git
Composer
composer require jojo1981/polling
用法
<?php use Jojo1981\Polling\Checker\PollResultChecker; use Jojo1981\Polling\Executor\PollExecutor; use Jojo1981\Polling\Poller; use Jojo1981\Polling\Result\PollResult; use Jojo1981\Polling\Value\PollCount; use Jojo1981\Polling\Value\UnsignedInteger; use Jojo1981\TypedCollection\Collection; require 'vendor/autoload.php'; // Example polling with 3 tries and then having a success result. $pollExecutor = new PollExecutor( /** * @param string $text1 * @param string $text2 * @param int $number * @param Collection|PollResult[] $previousResults * @param int $currentPollCount * @return string */ static function (string $text1, string $text2, int $number, Collection $previousResults, int $currentPollCount): string { // order of arguments: start polling arguments (when given), executor arguments (when given), $previousResults and $currentPollCount. return $currentPollCount === 3 ? 'This is a success result :)' : 'We are not yet there :('; }, [1] // optionally the default arguments which will be given to the poll executor callback ); $pollResultChecker = new PollResultChecker( /** * @param string $text1 * @param string $text2 * @param bool $number * @param string $result * @param Collection|PollResult[] $previousResults * @param int $currentPollCount * @return bool */ static function (string $text1, string $text2, bool $number, string $result, Collection $previousResults, int $currentPollCount): bool { // order of arguments: start polling arguments (when given), executor arguments (when given), $previousResults and $currentPollCount. return 'This is a success result :)' === $result; }, [1] // optionally the default arguments which will be given to the poll result checker callback ); // poll delay is expressed in microseconds, so poll max 5 times with in between 5 seconds delay. $poller = new Poller($pollExecutor, $pollResultChecker, null, new UnsignedInteger(5000000), new PollCount(5)); $finalPollResult = $poller->startPolling(['text1', 'text2']); // start polling with optionally some default arguments $finalPollResult->getCount(); // Will return the number of poll retries including the first one. Value: 3 in this case. $finalPollResult->isSuccess(); // Will return true when polling is succeeded. Value true in this case. $finalPollResult->isFailed(); // Will return true when polling has been failed. Value false in this case. $finalPollResult->getResult(); /// Will return the last poll result. Value 'This is a success result :)' in this case.