1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Resource; |
4
|
|
|
use OpenStack\Common\Error\BadResponseError; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Contains reusable functionality for resources that have long operations which require waiting in |
8
|
|
|
* order to reach a particular state. |
9
|
|
|
* |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
* |
12
|
|
|
* @package OpenStack\Common\Resource |
13
|
|
|
*/ |
14
|
|
|
trait HasWaiterTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Provides a blocking operation until the resource has reached a particular state. The method |
18
|
|
|
* will enter a loop, requesting feedback from the remote API until it sends back an appropriate |
19
|
|
|
* status. |
20
|
|
|
* |
21
|
|
|
* @param string $status The state to be reached |
22
|
|
|
* @param int $timeout The maximum timeout. If the total time taken by the waiter has reached |
23
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. |
24
|
|
|
* @param int $sleepPeriod The amount of time to pause between each HTTP request. |
25
|
|
|
*/ |
26
|
|
|
public function waitUntil($status, $timeout = 60, $sleepPeriod = 1) |
27
|
|
|
{ |
28
|
|
|
$startTime = time(); |
29
|
|
|
|
30
|
|
|
while (true) { |
31
|
|
|
$this->retrieve(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) { |
|
|
|
|
34
|
|
|
break; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
sleep($sleepPeriod); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Provides a blocking operation until the resource has reached a particular state. The method |
43
|
|
|
* will enter a loop, executing the callback until TRUE is returned. This provides great |
44
|
|
|
* flexibility. |
45
|
|
|
* |
46
|
|
|
* @param callable $fn An anonymous function that will be executed on every iteration. You can |
47
|
|
|
* encapsulate your own logic to determine whether the resource has |
48
|
|
|
* successfully transitioned. When TRUE is returned by the callback, |
49
|
|
|
* the loop will end. |
50
|
|
|
* @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
51
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. If FALSE |
52
|
|
|
* is provided, the timeout will never be considered. |
53
|
|
|
* @param int $sleepPeriod The amount of time to pause between each HTTP request. |
54
|
|
|
*/ |
55
|
|
|
public function waitWithCallback(callable $fn, $timeout = 60, $sleepPeriod = 1) |
56
|
|
|
{ |
57
|
|
|
$startTime = time(); |
58
|
|
|
|
59
|
|
|
while (true) { |
60
|
|
|
$this->retrieve(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$response = call_user_func_array($fn, [$this]); |
63
|
|
|
|
64
|
|
|
if ($response === true || $this->shouldHalt($timeout, $startTime)) { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
sleep($sleepPeriod); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Internal method used to identify whether a timeout has been exceeded. |
74
|
|
|
* |
75
|
|
|
* @param bool|int $timeout |
76
|
|
|
* @param int $startTime |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
private function shouldHalt($timeout, $startTime) |
81
|
|
|
{ |
82
|
|
|
if ($timeout === false) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return time() - $startTime >= $timeout; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Convenience method providing a blocking operation until the resource transitions to an |
91
|
|
|
* ``ACTIVE`` status. |
92
|
|
|
* |
93
|
|
|
* @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
94
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. If FALSE |
95
|
|
|
* is provided, the timeout will never be considered. |
96
|
|
|
*/ |
97
|
|
|
public function waitUntilActive($timeout = 60) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->waitUntil('ACTIVE'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function waitUntilDeleted($timeout = 60, $sleepPeriod = 1) |
103
|
|
|
{ |
104
|
|
|
$startTime = time(); |
105
|
|
|
|
106
|
|
|
while (true) { |
107
|
|
|
try { |
108
|
|
|
$this->retrieve(); |
|
|
|
|
109
|
|
|
} catch (BadResponseError $e) { |
110
|
|
|
if ($e->getResponse()->getStatusCode() === 404) { |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
throw $e; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->shouldHalt($timeout, $startTime)) { |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
sleep($sleepPeriod); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.