2017 © Pedro Peláez
 

library executioner

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

image

tebru/executioner

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

  • Monday, January 12, 2015
  • by natebrunette
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,416 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 14 Versions
  • 4 % Grown

The README.md

Build Status, (*1)

Executioner

This library aims to create an easy way to execute code that may throw an exception and should be reattempted., (*2)

Installation

Run composer require tebru/executioner:dev-master, (*3)

Basic Usage

The library can be used as simply as this, (*4)

<?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., (*5)

Events

Events are used to provides hooks/insight into the operations. The 4 events are:, (*6)

  • beforeAttempt
  • afterAttempt
  • failedAttempt
  • endAttempt

This library uses the Symfony2 event dispatcher. Feel free to use the included subscribers or create your own., (*7)

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

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:, (*9)

  • 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, (*10)

Add a LoggerSubscriber, (*11)

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

Add a WaitSubscriber using the StaticWaitStrategy, (*12)

Executor::addWait($seconds)

Add a WaitSubscriber, (*13)

Executor::addWaitStrategy(WaitStrategy $waitStrategy)

The Versions

12/01 2015

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.4.1

0.4.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.4.0

0.4.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.3.5

0.3.5.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.3.4

0.3.4.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.3.3

0.3.3.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/01 2015

v0.3.2

0.3.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

11/01 2015

v0.3.1

0.3.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

11/01 2015

v0.3.0

0.3.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

12/11 2014

v0.2.0

0.2.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

18/09 2014

v0.1.3

0.1.3.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

17/09 2014

v0.1.2

0.1.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

17/09 2014

v0.1.1

0.1.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

17/09 2014

v0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette