1. Go to this page and download the library: Download prismamedia/php-endeavor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
prismamedia / php-endeavor example snippets
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\ConstantStrategy;
$endeavor = new Endeavor(new ConstantStrategy(500));
$endeavor->run(function () {
// Code which can throw any \Throwable
});
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\ConstantStrategy;
$endeavor = new Endeavor(new ConstantStrategy(500), 3);
$endeavor->run(function () {
// Code which can throw any \Throwable
});
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\ExponentialStrategy;
$endeavor = new Endeavor(new ExponentialStrategy(1000), 5, 5000);
$endeavor->run(function () {
// Code which can throw any \Throwable
});
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\ConstantStrategy;
$endeavor = new Endeavor(new ConstantStrategy(100));
$endeavor->run(function () {
throw new \RuntimeException('Failing');
});
// 1st attempt: immediate
// 2nd attempt: 100ms
// 3rd attempt: 100ms
// 4th attempt: 100ms
// 5th attempt: 100ms
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\LinearStrategy;
$endeavor = new Endeavor(new LinearStrategy(100));
$endeavor->run(function () {
throw new \RuntimeException('Failing');
});
// 1st attempt: immediate
// 2nd attempt: 100ms
// 3rd attempt: 200ms
// 4th attempt: 300ms
// 5th attempt: 400ms
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\ExponentialStrategy;
$endeavor = new Endeavor(new ExponentialStrategy(100));
$endeavor->run(function () {
throw new \RuntimeException('Failing');
});
// 1st attempt: immediate
// 2nd attempt: 100ms
// 3rd attempt: 200ms
// 4th attempt: 400ms
// 5th attempt: 800ms
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\MultiplicativeStrategy;
$endeavor = new Endeavor(new MultiplicativeStrategy(100, 3));
$endeavor->run(function () {
throw new \RuntimeException('Failing');
});
// 1st attempt: immediate
// 2nd attempt: 100ms
// 3rd attempt: 300ms
// 4th attempt: 900ms
// 5th attempt: 2700ms
use PrismaMedia\Endeavor\Endeavor;
use PrismaMedia\Endeavor\Strategy\LinearStrategy;
$endeavor = new Endeavor(new LinearStrategy(500));
$endeavor->setErrorHandler(function (Endeavor $endeavor, \Throwable $e, int $attempt) {
// $endeavor is the current instance
// $e is the thrown Exception during this attempt
// $attempt is the current attempt number
$this->logger->error(
'Something went wrong on the attempt #{attempt}: {error}',
[
'attempt' => $attempt,
'error' => $e->getMessage(),
]
);
});
$endeavor->run(function () {
throw new \RuntimeException('Failing');
});
$endeavor->setErrorHandler(function (Endeavor $endeavor, \Throwable $e, int $attempt) {
if ($e instanceof OneSpecificException) {
throw $e
}
});
$endeavor->setErrorHandler(function (Endeavor $endeavor, \Throwable $e, int $attempt) {
if ($e instanceof UnreachableDatabaseException) {
$endeavor->setStrategy(new ConstantStrategy(5000));
}
});
# tests/boostrap.php
use PrismaMedia\Endeavor\Endeavor;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\Dotenv\Dotenv;
s(Dotenv::class, 'bootEnv')) {
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
}
// Register Endeavor in ClockMock to skip the waiting time between retries
ClockMock::register(Endeavor::class);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.