1 | <?php |
||
18 | class ConsulConfig implements ConfigInterface |
||
19 | { |
||
20 | const DEFAULT_KV_PREFIX = 'service/CruftFlake/machines/'; |
||
21 | |||
22 | /** |
||
23 | * CURL requestor. |
||
24 | * |
||
25 | * @var ConsulCurl |
||
26 | */ |
||
27 | private $curl; |
||
28 | |||
29 | private $kvPrefix = self::DEFAULT_KV_PREFIX; |
||
30 | |||
31 | /** |
||
32 | * Consul session ID. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sessionId = ""; |
||
37 | |||
38 | private $sessionTTL; |
||
39 | |||
40 | /** |
||
41 | * Last successfull check. |
||
42 | * |
||
43 | * @var integer|null |
||
44 | */ |
||
45 | private $lastSuccessfullCheck = null; |
||
46 | |||
47 | /** |
||
48 | * Machine ID. |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | private $machineId; |
||
53 | |||
54 | /** |
||
55 | * Class constructor. |
||
56 | * |
||
57 | * @param ConsulCurl $curl |
||
58 | * @param integer $sessionTTL |
||
59 | * @param string $kvPrefix |
||
60 | */ |
||
61 | 7 | public function __construct(ConsulCurl $curl, $sessionTTL = 600, $kvPrefix = self::DEFAULT_KV_PREFIX) |
|
70 | |||
71 | /** |
||
72 | * On object destruction, we have to destroy session. |
||
73 | */ |
||
74 | 7 | public function __destruct() |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 3 | public function getMachine() |
|
89 | |||
90 | /** |
||
91 | * Configuration heartbeat. |
||
92 | * |
||
93 | * Heartbeat connects periodically to Consul to renew session and check its validity. |
||
94 | * |
||
95 | * @return bool True, if configuration data had been changed during heartbeat. |
||
96 | * |
||
97 | * @throws RuntimeException Thrown, when we could not create new session and it was needed. |
||
98 | */ |
||
99 | 4 | public function heartbeat() |
|
125 | |||
126 | /** |
||
127 | * Return machine ID from consul queries. |
||
128 | * |
||
129 | * @return integer |
||
130 | * @throws RuntimeException |
||
131 | */ |
||
132 | 3 | private function acquireMachineId() |
|
154 | |||
155 | /** |
||
156 | * Try to fetch machine ID. |
||
157 | * |
||
158 | * @param array $currentValues |
||
159 | * @return integer |
||
160 | * @throws RuntimeException |
||
161 | */ |
||
162 | 3 | private function computePossibleMachineId(array $currentValues) |
|
163 | { |
||
164 | 3 | $usedIds = array(); |
|
165 | 3 | foreach ($currentValues as $currentValue) { |
|
166 | 2 | if ($currentValue['Key'] == $this->kvPrefix) { |
|
167 | 1 | continue; |
|
168 | 2 | } elseif ($currentValue['Key'] == $this->sessionId) { |
|
169 | return (int)base64_decode($currentValue['Value']); |
||
170 | } |
||
171 | else { |
||
172 | 2 | $usedIds[] = (int)base64_decode($currentValue['Value']); |
|
173 | } |
||
174 | 3 | } |
|
175 | 3 | for ($k = 0; $k < 1024; $k++) { |
|
176 | 3 | if (!in_array($k, $usedIds)) { |
|
177 | 2 | return $k; |
|
178 | } |
||
179 | 2 | } |
|
180 | 1 | throw new RuntimeException("Cannot acquire machine ID - all machine IDs are used up"); |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Lock master key. |
||
185 | */ |
||
186 | 3 | private function lockKey() |
|
196 | |||
197 | /** |
||
198 | * Release master key. |
||
199 | */ |
||
200 | 2 | private function releaseKey() |
|
204 | |||
205 | /** |
||
206 | * Create new session. |
||
207 | * |
||
208 | * @throws RuntimeException |
||
209 | */ |
||
210 | 7 | private function createSession() |
|
225 | |||
226 | /** |
||
227 | * Destroy session. |
||
228 | */ |
||
229 | 7 | private function destroySession() |
|
235 | } |
||
236 |