1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gousto\Replay; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Replay |
10
|
|
|
* |
11
|
|
|
* @package Gousto\Replay |
12
|
|
|
*/ |
13
|
|
|
class Replay |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $counter = 1; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $max_attempts_count = 1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $attempts_count = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $delay = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $has_handler = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $errored = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Closure |
48
|
|
|
*/ |
49
|
|
|
protected $user_function; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Closure |
53
|
|
|
*/ |
54
|
|
|
protected $retry_handler; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $exception_targets = [Exception::class]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retry constructor. |
63
|
|
|
* |
64
|
|
|
* @param int $retries |
65
|
|
|
* @param Closure $closure |
66
|
|
|
* @param int $delay |
67
|
|
|
* @param array $exception_targets |
68
|
|
|
*/ |
69
|
|
|
public function __construct($retries = 1, Closure $closure, $delay = 0, $exception_targets = []) |
70
|
|
|
{ |
71
|
|
|
$this->counter = $retries; |
72
|
|
|
$this->max_attempts_count = $retries; |
73
|
|
|
$this->user_function = $closure; |
74
|
|
|
$this->delay = $delay; |
75
|
|
|
$this->exception_targets = $exception_targets ?: $this->exception_targets; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $attempts |
80
|
|
|
* @param Closure $closure |
81
|
|
|
* @param int $delay |
82
|
|
|
* @param array $exception_targets |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
|
|
public static function times($attempts = 1, Closure $closure, $delay = 0, $exception_targets = []) |
87
|
|
|
{ |
88
|
|
|
return new static($attempts, $closure, $delay, $exception_targets); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $attempts |
93
|
|
|
* @param Closure $closure |
94
|
|
|
* @param int $delay |
95
|
|
|
* @param array $exception_targets |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public static function retry($attempts = 1, Closure $closure, $delay = 0, $exception_targets = []) |
100
|
|
|
{ |
101
|
|
|
return static::times($attempts, $closure, $delay, $exception_targets)->play(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param bool $silent |
106
|
|
|
* |
107
|
|
|
* @return mixed|null |
108
|
|
|
* @throws RetryLogicException |
109
|
|
|
*/ |
110
|
|
|
public function play($silent = false) |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
do { $response = $this->next(); } while ($this->counter > 0); |
114
|
|
|
return $response; |
115
|
|
|
} catch(RetryLogicException $e) { |
116
|
|
|
if ($silent === true) { |
117
|
|
|
return $e; |
118
|
|
|
} |
119
|
|
|
throw $e; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return mixed |
125
|
|
|
* @throws Exception|RetryLogicException |
126
|
|
|
*/ |
127
|
|
|
public function next() |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
$this->errored = false; |
131
|
|
|
return $this->invokeUserFunction(); |
132
|
|
|
} catch ( Exception $e ) { |
133
|
|
|
|
134
|
|
|
$this->attempts_count++; |
135
|
|
|
$this->counter--; |
136
|
|
|
$this->errored = true; |
137
|
|
|
|
138
|
|
|
$this->handleError($e); |
139
|
|
|
|
140
|
|
|
// No handler specified, bubble up the exception |
141
|
|
|
if ( ! $this->has_handler) { |
142
|
|
|
throw $e; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Counter is done |
146
|
|
|
if ($this->counter <= 0) { |
147
|
|
|
$this->counter = 0; |
148
|
|
|
$this->retryLogicException($e); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $e; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Exception $e |
157
|
|
|
* |
158
|
|
|
* @throws Exception |
159
|
|
|
*/ |
160
|
|
|
protected function handleError(Exception $e) |
161
|
|
|
{ |
162
|
|
|
$exception_targets = $this->exception_targets; |
163
|
|
|
|
164
|
|
|
// Loop through all the exception handlers |
165
|
|
|
// until we find a match |
166
|
|
|
foreach ($exception_targets as $error_handler) { |
167
|
|
|
if (is_a($e, $error_handler, true)) { |
168
|
|
|
$this->has_handler = true; |
169
|
|
|
|
170
|
|
|
// Any delay provided for every retry |
171
|
|
|
if ($this->delay > 0) { |
172
|
|
|
usleep($this->delay * 1000); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Any custom error handler specified |
176
|
|
|
if ($handler = $this->retry_handler) { |
177
|
|
|
$handler($e, $this); |
178
|
|
|
} |
179
|
|
|
break; |
180
|
|
|
} else { |
181
|
|
|
$this->has_handler = false; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Invoke user function |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
private function invokeUserFunction() |
192
|
|
|
{ |
193
|
|
|
$user_function = $this->user_function; |
194
|
|
|
$response = $user_function(); |
195
|
|
|
$this->counter = 0; |
196
|
|
|
$this->errored = false; |
197
|
|
|
|
198
|
|
|
return $response; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return static |
203
|
|
|
*/ |
204
|
|
|
public function newReplay() |
205
|
|
|
{ |
206
|
|
|
return new static( |
207
|
|
|
$this->max_attempts_count, |
208
|
|
|
$this->user_function, |
209
|
|
|
$this->delay, |
210
|
|
|
$this->exception_targets |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param Closure $retry_handler |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
|
|
public function onRetry(Closure $retry_handler) |
220
|
|
|
{ |
221
|
|
|
$this->retry_handler = $retry_handler; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function maxAttempts() |
230
|
|
|
{ |
231
|
|
|
return $this->max_attempts_count; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
public function isErrored() |
238
|
|
|
{ |
239
|
|
|
return $this->errored; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return int |
244
|
|
|
*/ |
245
|
|
|
public function attempts() |
246
|
|
|
{ |
247
|
|
|
return $this->attempts_count; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return int |
252
|
|
|
*/ |
253
|
|
|
public function getDelay() |
254
|
|
|
{ |
255
|
|
|
return $this->delay; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param int $delay |
260
|
|
|
*/ |
261
|
|
|
public function setDelay($delay) |
262
|
|
|
{ |
263
|
|
|
$this->delay = $delay; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param $e |
268
|
|
|
* |
269
|
|
|
* @throws RetryLogicException |
270
|
|
|
*/ |
271
|
|
|
protected function retryLogicException(Exception $e) |
272
|
|
|
{ |
273
|
|
|
throw new RetryLogicException( |
274
|
|
|
$this, |
275
|
|
|
$e, |
276
|
|
|
"Retry logic failed after $this->max_attempts_count times" |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|