Skip to content

Commit 55b8f9d

Browse files
joedixonnunomaduro
andauthored
[1.x] Adds retry middleware on asset upload (#200)
* Add retry middleware on asset upload * Style Co-authored-by: Nuno Maduro <[email protected]>
1 parent 4625c0b commit 55b8f9d

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/Aws/AwsStorageProvider.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
namespace Laravel\VaporCli\Aws;
44

55
use GuzzleHttp\Client;
6+
use GuzzleHttp\HandlerStack;
7+
use GuzzleHttp\Middleware;
68
use GuzzleHttp\Pool;
79
use GuzzleHttp\Psr7\Request;
810
use Illuminate\Support\LazyCollection;
11+
use Illuminate\Support\Str;
912
use Laravel\VaporCli\ConsoleVaporClient;
1013
use Laravel\VaporCli\Exceptions\RequestFailedException;
1114
use Laravel\VaporCli\Helpers;
15+
use Psr\Http\Message\RequestInterface;
16+
use Psr\Http\Message\ResponseInterface;
1217
use Symfony\Component\Console\Helper\ProgressBar;
1318

1419
class AwsStorageProvider
1520
{
1621
protected $vapor;
1722

23+
const MAX_RETRIES = 3;
24+
1825
/**
1926
* Create a new storage provider instance.
2027
*
@@ -55,8 +62,8 @@ public function store($url, array $headers, $file, $withProgress = false)
5562
: null;
5663

5764
$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,
6067
'progress' => $progressCallback,
6168
]));
6269

@@ -119,7 +126,7 @@ public function executeStoreRequests($requests, $assetPath, $callback)
119126
}
120127
};
121128

122-
(new Pool(new Client(), $generator(), [
129+
(new Pool(new Client(['handler' => $this->retryHandler()]), $generator(), [
123130
'concurrency' => 10,
124131
'rejected' => function ($reason, $index) {
125132
throw new RequestFailedException($reason->getMessage(), $index);
@@ -158,7 +165,7 @@ public function executeCopyRequests($requests, $callback)
158165
}
159166
};
160167

161-
(new Pool(new Client(), $generator(), [
168+
(new Pool(new Client(['handler' => $this->retryHandler()]), $generator(), [
162169
'concurrency' => 10,
163170
'rejected' => function ($reason, $index) {
164171
throw new RequestFailedException($reason->getMessage(), $index);
@@ -167,4 +174,30 @@ public function executeCopyRequests($requests, $callback)
167174
->promise()
168175
->wait();
169176
}
177+
178+
/**
179+
* Get a handler stack containing the retry middleware configuration.
180+
*
181+
* @return \GuzzleHttp\HandlerStack
182+
*/
183+
protected function retryHandler()
184+
{
185+
$stack = HandlerStack::create();
186+
187+
$stack->push(Middleware::retry(function (int $retries, RequestInterface $request, ResponseInterface $response = null) {
188+
if ($retries === 0) {
189+
return true;
190+
}
191+
192+
if ($response && $response->getStatusCode() < 300) {
193+
return false;
194+
}
195+
196+
Helpers::step('<comment>Retrying Request: </comment><options=bold>'.$request->getMethod().'</> '.Str::before($request->getUri(), '?'));
197+
198+
return $retries < self::MAX_RETRIES;
199+
}));
200+
201+
return $stack;
202+
}
170203
}

0 commit comments

Comments
 (0)