Last active
September 3, 2015 18:23
-
-
Save coderabbi/d0f8f8609fb6f820d020 to your computer and use it in GitHub Desktop.
Retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RetriesExceededException extends \Exception {} | |
class Retry | |
{ | |
const MAX_RETRIES = 5; | |
const BASE_RETRY_DELAY = 1000000; | |
static public function delayed(callable $action, $retries = 1) | |
{ | |
if ( ! is_int($retries) || $retries > self::MAX_RETRIES) { | |
throw new \InvalidArgumentException(); | |
} | |
return self::attempt($action, $retries, true); | |
} | |
static public function immediately(callable $action, $retries = 1) | |
{ | |
if ( ! is_int($retries) || $retries > self::MAX_RETRIES) { | |
throw new \InvalidArgumentException(); | |
} | |
return self::attempt($action, $retries, false); | |
} | |
static private function attempt(callable $action, $retries, $delay) | |
{ | |
do { | |
try { | |
return $action(); | |
} catch (\Exception $e) { | |
if ($delay && $retries) { | |
//do delay | |
} | |
} | |
} while ($retries--) | |
throw new RetriesExceededException(); | |
} | |
} | |
//Retry::delayed($delay, $action, $retries); | |
//Retry::immediately($action, $retries); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment