tebru/executioner 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tebru/executioner

Composer 安装命令:

composer require tebru/executioner

包简介

Provides a library for handling PHP exceptions when you would like to retry execution.

README 文档

README

Build Status

Executioner

This library aims to create an easy way to execute code that may throw an exception and should be reattempted.

Installation

Run composer require tebru/executioner:dev-master

Basic Usage

The library can be used as simply as this

<?php

use Tebru\Executioner;

$executor = new Executor();
$result = $executor->execute(2, function () { /* code that may throw an exception */ });

Which will retry twice on all exceptions. Failure to execute without throwing an exception will cause a \Tebru\Executioner\Exception\FailedException to be thrown. Be sure to wrap your execute() call in a try/catch if you do not want that exception to propagate.

Events

Events are used to provides hooks/insight into the operations. The 4 events are:

  • beforeAttempt
  • afterAttempt
  • failedAttempt
  • endAttempt

This library uses the Symfony2 event dispatcher. Feel free to use the included subscribers or create your own.

Use the listener methods on the Executor to target one event, and use subscribers to target multiple events.

Delay between attempts

<?php

use Tebru\Executioner;
use Tebru\Executioner\Subscriber\WaitSubscriber;

$waitStrategy = new \Tebru\Executioner\Strategy\WaitStrategy();

$executor = new Executor();
$executor->addSubscriber(new WaitSubscriber($waitStrategy));
$result = $executor->execute(2, function () { /* code that may throw an exception */ });

There are two wait strategies included:

  • Tebru\Executioner\Strategy\StaticWaitStrategy -- Waits for a set amount of time between each attempt
  • Tebru\Executioner\Strategy\ExponentialBackoffStrategy -- On average, exponentially waits longer between each attempt

Logging

<?php

use Tebru\Executioner;
use Tebru\Executioner\Subscriber\LoggerSubscriber;

$logger = new \Psr\Log\LoggerInterface(); // the only requirement is you need a ps-3 compatible logger

$executor = new Executor();
$executor->addSubscriber(new LoggerSubscriber('name-of-this-logger-to-distinguish-it-in-the-logs', $logger));
$result = $executor->execute(2, function () { /* code that may throw an exception */ });

Retry on non-exceptions

<?php

use Tebru\Executioner;
use Tebru\Executioner\Subscriber\ReturnSubscriber;

$executor = new Executor();
$executor->addSubscriber(new ReturnSubscriber([false]));
$result = $executor->execute(2, function () { return false; });

Shortcuts

To make life easier, there are some helper methods to create some of the included subscribers

Add a LoggerSubscriber

Executor::addLogger($name, LoggerInterface $logger)

Add a WaitSubscriber using the StaticWaitStrategy

Executor::addWait($seconds)

Add a WaitSubscriber

Executor::addWaitStrategy(WaitStrategy $waitStrategy)

统计信息

  • 总下载量: 1.72k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-09-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固