1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Resource; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Contains reusable functionality for resources that have long operations which require waiting in |
7
|
|
|
* order to reach a particular state. |
8
|
|
|
* |
9
|
|
|
* @codeCoverageIgnore |
10
|
|
|
* |
11
|
|
|
* @package OpenStack\Common\Resource |
12
|
|
|
*/ |
13
|
|
|
trait HasWaiterTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Provides a blocking operation until the resource has reached a particular state. The method |
17
|
|
|
* will enter a loop, requesting feedback from the remote API until it sends back an appropriate |
18
|
|
|
* status. |
19
|
|
|
* |
20
|
|
|
* @param string $status The state to be reached |
21
|
|
|
* @param int $timeout The maximum timeout. If the total time taken by the waiter has reached |
22
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. |
23
|
|
|
* @param int $sleepPeriod The amount of time to pause between each HTTP request. |
24
|
|
|
*/ |
25
|
|
|
public function waitUntil($status, $timeout = 60, $sleepPeriod = 1) |
26
|
|
|
{ |
27
|
|
|
$startTime = time(); |
28
|
|
|
|
29
|
|
|
while (true) { |
30
|
|
|
$this->retrieve(); |
|
|
|
|
31
|
|
|
|
32
|
|
|
if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) { |
|
|
|
|
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
sleep($sleepPeriod); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Provides a blocking operation until the resource has reached a particular state. The method |
42
|
|
|
* will enter a loop, executing the callback until TRUE is returned. This provides great |
43
|
|
|
* flexibility. |
44
|
|
|
* |
45
|
|
|
* @param callable $fn An anonymous function that will be executed on every iteration. You can |
46
|
|
|
* encapsulate your own logic to determine whether the resource has |
47
|
|
|
* successfully transitioned. When TRUE is returned by the callback, |
48
|
|
|
* the loop will end. |
49
|
|
|
* @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
50
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. If FALSE |
51
|
|
|
* is provided, the timeout will never be considered. |
52
|
|
|
* @param int $sleepPeriod The amount of time to pause between each HTTP request. |
53
|
|
|
*/ |
54
|
|
|
public function waitWithCallback(callable $fn, $timeout = 60, $sleepPeriod = 1) |
55
|
|
|
{ |
56
|
|
|
$startTime = time(); |
57
|
|
|
|
58
|
|
|
while (true) { |
59
|
|
|
$this->retrieve(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$response = call_user_func_array($fn, [$this]); |
62
|
|
|
|
63
|
|
|
if ($response === true || $this->shouldHalt($timeout, $startTime)) { |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
sleep($sleepPeriod); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Internal method used to identify whether a timeout has been exceeded. |
73
|
|
|
* |
74
|
|
|
* @param bool|int $timeout |
75
|
|
|
* @param int $startTime |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
private function shouldHalt($timeout, $startTime) |
80
|
|
|
{ |
81
|
|
|
if ($timeout === false) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return time() - $startTime >= $timeout; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Convenience method providing a blocking operation until the resource transitions to an |
90
|
|
|
* ``ACTIVE`` status. |
91
|
|
|
* |
92
|
|
|
* @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached |
93
|
|
|
* or exceed this timeout, the blocking operation will immediately cease. If FALSE |
94
|
|
|
* is provided, the timeout will never be considered. |
95
|
|
|
*/ |
96
|
|
|
public function waitUntilActive($timeout = 60) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->waitUntil('ACTIVE'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.