-
-
Save RickardAhlstedt/0b5478cd747594500332d094b27255f5 to your computer and use it in GitHub Desktop.
Retry any function in php
This file contains hidden or 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 | |
if (!function_exists('retry')) { | |
/** | |
* Retries callable | |
* | |
* Could be specified how many times, default is 1 times. | |
* | |
* @param callable $what | |
* @param int $retry how many time should it be retried, default is 1 | |
* @return mixed | |
* @throws Exception | |
*/ | |
function retry(callable $what, $retry = 1) | |
{ | |
again: | |
try { | |
return $what(); | |
} catch (\RetryableException $e) { | |
if ($retry-- > 0) { | |
goto again; | |
} | |
throw $e; | |
} | |
} | |
/** | |
* Class RetryableException | |
* To be able to specify specific conditions for repeating | |
*/ | |
class RetryableException extends \Exception{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment