3
3
namespace Laravel \VaporCli \Aws ;
4
4
5
5
use GuzzleHttp \Client ;
6
+ use GuzzleHttp \HandlerStack ;
7
+ use GuzzleHttp \Middleware ;
6
8
use GuzzleHttp \Pool ;
7
9
use GuzzleHttp \Psr7 \Request ;
8
10
use Illuminate \Support \LazyCollection ;
11
+ use Illuminate \Support \Str ;
9
12
use Laravel \VaporCli \ConsoleVaporClient ;
10
13
use Laravel \VaporCli \Exceptions \RequestFailedException ;
11
14
use Laravel \VaporCli \Helpers ;
15
+ use Psr \Http \Message \RequestInterface ;
16
+ use Psr \Http \Message \ResponseInterface ;
12
17
use Symfony \Component \Console \Helper \ProgressBar ;
13
18
14
19
class AwsStorageProvider
15
20
{
16
21
protected $ vapor ;
17
22
23
+ const MAX_RETRIES = 3 ;
24
+
18
25
/**
19
26
* Create a new storage provider instance.
20
27
*
@@ -55,8 +62,8 @@ public function store($url, array $headers, $file, $withProgress = false)
55
62
: null ;
56
63
57
64
$ response = (new Client ())->request ('PUT ' , $ url , array_filter ([
58
- 'body ' => $ stream ,
59
- 'headers ' => empty ($ headers ) ? null : $ headers ,
65
+ 'body ' => $ stream ,
66
+ 'headers ' => empty ($ headers ) ? null : $ headers ,
60
67
'progress ' => $ progressCallback ,
61
68
]));
62
69
@@ -119,7 +126,7 @@ public function executeStoreRequests($requests, $assetPath, $callback)
119
126
}
120
127
};
121
128
122
- (new Pool (new Client (), $ generator (), [
129
+ (new Pool (new Client ([ ' handler ' => $ this -> retryHandler ()] ), $ generator (), [
123
130
'concurrency ' => 10 ,
124
131
'rejected ' => function ($ reason , $ index ) {
125
132
throw new RequestFailedException ($ reason ->getMessage (), $ index );
@@ -158,7 +165,7 @@ public function executeCopyRequests($requests, $callback)
158
165
}
159
166
};
160
167
161
- (new Pool (new Client (), $ generator (), [
168
+ (new Pool (new Client ([ ' handler ' => $ this -> retryHandler ()] ), $ generator (), [
162
169
'concurrency ' => 10 ,
163
170
'rejected ' => function ($ reason , $ index ) {
164
171
throw new RequestFailedException ($ reason ->getMessage (), $ index );
@@ -167,4 +174,29 @@ public function executeCopyRequests($requests, $callback)
167
174
->promise ()
168
175
->wait ();
169
176
}
177
+
178
+ /**
179
+ * Get a handler stack containing the retry middleware configuration.
180
+ *
181
+ * @return \GuzzleHttp\HandlerStack
182
+ */
183
+ private function retryHandler ()
184
+ {
185
+ $ stack = HandlerStack::create ();
186
+ $ stack ->push (Middleware::retry (function (int $ retries , RequestInterface $ request , ResponseInterface $ response = null ) {
187
+ if ($ retries === 0 ) {
188
+ return true ;
189
+ }
190
+
191
+ if ($ response && $ response ->getStatusCode () < 300 ) {
192
+ return false ;
193
+ }
194
+
195
+ Helpers::step ('<comment>Retrying Request: </comment><options=bold> ' .$ request ->getMethod ().'</> ' .Str::before ($ request ->getUri (), '? ' ));
196
+
197
+ return $ retries < self ::MAX_RETRIES ;
198
+ }));
199
+
200
+ return $ stack ;
201
+ }
170
202
}
0 commit comments