Exception
1 year ago
.htaccess
1 year ago
Client.php
1 year ago
ClientManager.php
2 months ago
ClientRetry.php
1 year ago
ListObjects.php
1 year ago
ObjectData.php
1 year ago
index.html
1 year ago
web.config
1 year ago
ClientRetry.php
114 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\Destination\Vendors\S3\Client; |
| 12 | |
| 13 | use Exception; |
| 14 | use JetBackup\Destination\Vendors\S3\Client\Exception\ClientException; |
| 15 | |
| 16 | defined( '__JETBACKUP__' ) or die( 'Restricted access' ); |
| 17 | |
| 18 | class ClientRetry { |
| 19 | |
| 20 | const WAIT_TIME = 333000; |
| 21 | const MAX_WAIT_TIME = 60000000; |
| 22 | |
| 23 | private string $_func; |
| 24 | private array $_args; |
| 25 | private $_retry_callback; |
| 26 | private $_success_callback; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $this->_args = []; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param string $function |
| 37 | * |
| 38 | * @return ClientRetry |
| 39 | */ |
| 40 | public function func(string $function):ClientRetry { |
| 41 | $this->_func = $function; |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param ...$args |
| 47 | * |
| 48 | * @return ClientRetry |
| 49 | */ |
| 50 | public function args(...$args):ClientRetry { |
| 51 | $this->_args = $args; |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param callable $callback |
| 57 | * |
| 58 | * @return ClientRetry |
| 59 | */ |
| 60 | public function retryCallback(callable $callback):ClientRetry { |
| 61 | $this->_retry_callback = $callback; |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param callable $callback |
| 67 | * |
| 68 | * @return ClientRetry |
| 69 | */ |
| 70 | public function successCallback(callable $callback):ClientRetry { |
| 71 | $this->_success_callback = $callback; |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param ClientManager $manager |
| 77 | * |
| 78 | * @return mixed |
| 79 | * @throws ClientException |
| 80 | */ |
| 81 | public function exec(ClientManager $manager) { |
| 82 | $client = $manager->getClient(); |
| 83 | if(!$this->_func) throw new ClientException("You must provide function"); |
| 84 | if(!method_exists($client, $this->_func)) throw new ClientException("Invalid function ($this->_func) provide"); |
| 85 | |
| 86 | $waittime = self::WAIT_TIME; |
| 87 | $tries = 0; |
| 88 | while(true) { |
| 89 | try { |
| 90 | $result = call_user_func_array([$client, $this->_func], $this->_args); |
| 91 | |
| 92 | if(($success_callback = $this->_success_callback)) $success_callback(); |
| 93 | return $result; |
| 94 | } catch(Exception $e) { |
| 95 | if(($e->getCode() < 500 && !in_array($e->getCode(), [0,1,400,403,429])) || $tries >= $manager->getRetries()) throw new ClientException($e->getMessage(), $e->getCode()); |
| 96 | |
| 97 | if(($retry_callback = $this->_retry_callback)) $retry_callback(); |
| 98 | |
| 99 | $log_args = []; |
| 100 | foreach($this->_args as $arg) { |
| 101 | if(is_array($arg)) $arg = 'Array -> ' . json_encode($arg); |
| 102 | $log_args[] = $arg; |
| 103 | } |
| 104 | |
| 105 | $manager->getLogController()->logDebug("Failed $this->_func(" . implode(", ", $log_args). "). Error: {$e->getMessage()} (Code: {$e->getCode()})"); |
| 106 | if($waittime > self::MAX_WAIT_TIME) $waittime = self::MAX_WAIT_TIME; |
| 107 | usleep($waittime); |
| 108 | $waittime *= 2; |
| 109 | $tries++; |
| 110 | $manager->getLogController()->logDebug("Retry $tries/{$manager->getRetries()} $this->_func(" . implode(", ", $log_args). ")"); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |