1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SP\Attempt; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Attempting to do something several times with a small interval, immidiately return on success |
9
|
|
|
* |
10
|
|
|
* @author Ivan Kerin <[email protected]> |
11
|
|
|
* @copyright 2015, Clippings Ltd. |
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
13
|
|
|
*/ |
14
|
|
|
class Attempt |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var integer |
18
|
|
|
*/ |
19
|
|
|
private $current = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var integer |
23
|
|
|
*/ |
24
|
|
|
private $step = 50; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* timeout in milliseconds |
28
|
|
|
* |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
private $timeout = 2000; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var callable |
35
|
|
|
*/ |
36
|
|
|
private $callback; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param callable $callback |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(callable $callback) |
42
|
|
|
{ |
43
|
1 |
|
$this->callback = $callback; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return callable |
48
|
|
|
*/ |
49
|
1 |
|
public function getCallback() |
50
|
|
|
{ |
51
|
1 |
|
return $this->callback; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param integer $timeout |
56
|
|
|
*/ |
57
|
1 |
|
public function setTimeout($timeout) |
58
|
|
|
{ |
59
|
1 |
|
$this->timeout = (int) $timeout; |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return integer |
66
|
|
|
*/ |
67
|
1 |
|
public function getTimeout() |
68
|
|
|
{ |
69
|
1 |
|
return $this->timeout; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param integer $step |
74
|
|
|
*/ |
75
|
1 |
|
public function setStep($step) |
76
|
|
|
{ |
77
|
1 |
|
$this->step = (int) $step; |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return integer |
84
|
|
|
*/ |
85
|
1 |
|
public function getStep() |
86
|
|
|
{ |
87
|
1 |
|
return $this->step; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
3 |
|
public function getCurrent() |
94
|
|
|
{ |
95
|
3 |
|
return $this->current; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
1 |
|
public function hasTriesLeft() |
102
|
|
|
{ |
103
|
1 |
|
return $this->current * $this->step < $this->timeout; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return integer |
108
|
|
|
*/ |
109
|
1 |
|
public function getTries() |
110
|
|
|
{ |
111
|
1 |
|
return (int) ceil($this->timeout / $this->step); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
2 |
|
public function execute() |
118
|
|
|
{ |
119
|
2 |
|
$this->current = 0; |
120
|
|
|
|
121
|
2 |
|
$callback = $this->callback; |
122
|
|
|
|
123
|
|
|
do { |
124
|
2 |
|
$result = $callback($this); |
125
|
|
|
|
126
|
2 |
|
$this->current += 1; |
127
|
|
|
|
128
|
2 |
|
if (!$result) { |
129
|
2 |
|
usleep($this->step * 1000); |
130
|
2 |
|
} |
131
|
2 |
|
} while (!$result && $this->hasTriesLeft()); |
132
|
|
|
|
133
|
2 |
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Like normal execute, but throw RuntimeException on time out |
138
|
|
|
* |
139
|
|
|
* @param string $message optional message |
140
|
|
|
* @throws RuntimeException on time out |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
2 |
|
public function executeOrFail($message = 'Timed out attempting to execute callback') |
144
|
|
|
{ |
145
|
2 |
|
$result = $this->execute(); |
146
|
|
|
|
147
|
2 |
|
if (empty($result)) { |
148
|
1 |
|
throw new RuntimeException($message); |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return $result; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|