| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Exception\AwsException; |
| 5 |
use GuzzleHttp\Promise\Coroutine; |
| 6 |
use GuzzleHttp\Promise\PromiseInterface; |
| 7 |
use GuzzleHttp\Promise\PromisorInterface; |
| 8 |
use GuzzleHttp\Promise\RejectedPromise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Waiter implements PromisorInterface |
| 24 |
{ |
| 25 |
|
| 26 |
private $client; |
| 27 |
|
| 28 |
|
| 29 |
private $name; |
| 30 |
|
| 31 |
|
| 32 |
private $args; |
| 33 |
|
| 34 |
|
| 35 |
private $config; |
| 36 |
|
| 37 |
|
| 38 |
private static $defaults = ['initDelay' => 0, 'before' => null]; |
| 39 |
|
| 40 |
|
| 41 |
private static $required = [ |
| 42 |
'acceptors', |
| 43 |
'delay', |
| 44 |
'maxAttempts', |
| 45 |
'operation', |
| 46 |
]; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct( |
| 65 |
AwsClientInterface $client, |
| 66 |
$name, |
| 67 |
array $args = [], |
| 68 |
array $config = [] |
| 69 |
) { |
| 70 |
$this->client = $client; |
| 71 |
$this->name = $name; |
| 72 |
$this->args = $args; |
| 73 |
|
| 74 |
|
| 75 |
$this->config = $config + self::$defaults; |
| 76 |
foreach (self::$required as $key) { |
| 77 |
if (!isset($this->config[$key])) { |
| 78 |
throw new \InvalidArgumentException( |
| 79 |
'The provided waiter configuration was incomplete.' |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
if ($this->config['before'] && !is_callable($this->config['before'])) { |
| 84 |
throw new \InvalidArgumentException( |
| 85 |
'The provided "before" callback is not callable.' |
| 86 |
); |
| 87 |
} |
| 88 |
MetricsBuilder::appendMetricsCaptureMiddleware( |
| 89 |
$this->client->getHandlerList(), |
| 90 |
MetricsBuilder::WAITER |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function promise(): PromiseInterface |
| 98 |
{ |
| 99 |
return Coroutine::of(function () { |
| 100 |
$name = $this->config['operation']; |
| 101 |
for ($state = 'retry', $attempt = 1; $state === 'retry'; $attempt++) { |
| 102 |
|
| 103 |
$args = $this->getArgsForAttempt($attempt); |
| 104 |
$command = $this->client->getCommand($name, $args); |
| 105 |
try { |
| 106 |
if ($this->config['before']) { |
| 107 |
$this->config['before']($command, $attempt); |
| 108 |
} |
| 109 |
$result = (yield $this->client->executeAsync($command)); |
| 110 |
} catch (AwsException $e) { |
| 111 |
$result = $e; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
$state = $this->determineState($result); |
| 116 |
if ($state === 'success') { |
| 117 |
yield $command; |
| 118 |
} elseif ($state === 'failed') { |
| 119 |
$msg = "The {$this->name} waiter entered a failure state."; |
| 120 |
if ($result instanceof \Exception) { |
| 121 |
$msg .= ' Reason: ' . $result->getMessage(); |
| 122 |
} |
| 123 |
yield new RejectedPromise(new \RuntimeException($msg)); |
| 124 |
} elseif ($state === 'retry' |
| 125 |
&& $attempt >= $this->config['maxAttempts'] |
| 126 |
) { |
| 127 |
$state = 'failed'; |
| 128 |
yield new RejectedPromise(new \RuntimeException( |
| 129 |
"The {$this->name} waiter failed after attempt #{$attempt}." |
| 130 |
)); |
| 131 |
} |
| 132 |
} |
| 133 |
}); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private function getArgsForAttempt($attempt) |
| 144 |
{ |
| 145 |
$args = $this->args; |
| 146 |
|
| 147 |
|
| 148 |
$delay = ($attempt === 1) |
| 149 |
? $this->config['initDelay'] |
| 150 |
: $this->config['delay']; |
| 151 |
if (is_callable($delay)) { |
| 152 |
$delay = $delay($attempt); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
if (!isset($args['@http'])) { |
| 157 |
$args['@http'] = []; |
| 158 |
} |
| 159 |
$args['@http']['delay'] = $delay * 1000; |
| 160 |
|
| 161 |
return $args; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private function determineState($result) |
| 174 |
{ |
| 175 |
foreach ($this->config['acceptors'] as $acceptor) { |
| 176 |
$matcher = 'matches' . ucfirst($acceptor['matcher']); |
| 177 |
if ($this->{$matcher}($result, $acceptor)) { |
| 178 |
return $acceptor['state']; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $result instanceof \Exception ? 'failed' : 'retry'; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private function matchesPath($result, array $acceptor) |
| 192 |
{ |
| 193 |
return $result instanceof ResultInterface |
| 194 |
&& $acceptor['expected'] === $result->search($acceptor['argument']); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private function matchesPathAll($result, array $acceptor) |
| 204 |
{ |
| 205 |
if (!($result instanceof ResultInterface)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
$actuals = $result->search($acceptor['argument']) ?: []; |
| 210 |
|
| 211 |
if (empty($actuals) || !is_array($actuals)) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
foreach ($actuals as $actual) { |
| 216 |
if ($actual != $acceptor['expected']) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private function matchesPathAny($result, array $acceptor) |
| 231 |
{ |
| 232 |
if (!($result instanceof ResultInterface)) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
$actuals = $result->search($acceptor['argument']) ?: []; |
| 237 |
|
| 238 |
if (empty($actuals) || !is_array($actuals)) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
return in_array($acceptor['expected'], $actuals); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
private function matchesStatus($result, array $acceptor) |
| 252 |
{ |
| 253 |
if ($result instanceof ResultInterface) { |
| 254 |
return $acceptor['expected'] == $result['@metadata']['statusCode']; |
| 255 |
} |
| 256 |
|
| 257 |
if ($result instanceof AwsException && $response = $result->getResponse()) { |
| 258 |
return $acceptor['expected'] == $response->getStatusCode(); |
| 259 |
} |
| 260 |
|
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private function matchesError($result, array $acceptor) |
| 271 |
{ |
| 272 |
|
| 273 |
|
| 274 |
if (isset($acceptor['expected']) && is_bool($acceptor['expected'])) { |
| 275 |
return $acceptor['expected'] === ($result instanceof AwsException); |
| 276 |
} |
| 277 |
|
| 278 |
if ($result instanceof AwsException) { |
| 279 |
return $result->isConnectionError() |
| 280 |
|| $result->getAwsErrorCode() == $acceptor['expected']; |
| 281 |
} |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
} |