PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Destination / Vendors / S3 / Client / ClientRetry.php
backup / src / JetBackup / Destination / Vendors / S3 / Client Last commit date
Exception 9 months ago .htaccess 9 months ago Client.php 9 months ago ClientManager.php 9 months ago ClientRetry.php 9 months ago ListObjects.php 9 months ago ObjectData.php 9 months ago index.html 9 months ago web.config 9 months 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 }