1 | <?php |
||
18 | class ConsulConfig implements ConfigInterface |
||
19 | { |
||
20 | /** |
||
21 | * Default KV prefix on Consul. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | const DEFAULT_KV_PREFIX = 'service/CruftFlake/machines/'; |
||
26 | |||
27 | /** |
||
28 | * CURL requestor. |
||
29 | * |
||
30 | * @var ConsulCurl |
||
31 | */ |
||
32 | private $curl; |
||
33 | |||
34 | /** |
||
35 | * Consul KV prefix. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $kvPrefix = self::DEFAULT_KV_PREFIX; |
||
40 | |||
41 | /** |
||
42 | * Consul session ID. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $sessionId = ""; |
||
47 | |||
48 | /** |
||
49 | * Session TTL. |
||
50 | * |
||
51 | * @var integer |
||
52 | */ |
||
53 | private $sessionTTL; |
||
54 | |||
55 | /** |
||
56 | * Last successfull check. |
||
57 | * |
||
58 | * @var integer|null |
||
59 | */ |
||
60 | private $lastSuccessfullCheck = null; |
||
61 | |||
62 | /** |
||
63 | * Machine ID. |
||
64 | * |
||
65 | * @var integer |
||
66 | */ |
||
67 | private $machineId; |
||
68 | |||
69 | /** |
||
70 | * Class constructor. |
||
71 | * |
||
72 | * @param ConsulCurl $curl |
||
73 | * @param integer $sessionTTL |
||
74 | * @param string $kvPrefix |
||
75 | */ |
||
76 | 12 | public function __construct(ConsulCurl $curl, $sessionTTL = 600, $kvPrefix = self::DEFAULT_KV_PREFIX) |
|
85 | |||
86 | /** |
||
87 | * On object destruction, we have to destroy session. |
||
88 | */ |
||
89 | 12 | public function __destruct() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 7 | public function getMachine() |
|
104 | |||
105 | /** |
||
106 | * Configuration heartbeat. |
||
107 | * |
||
108 | * Heartbeat connects periodically to Consul to renew session and check its validity. |
||
109 | * |
||
110 | * @return bool True, if configuration data had been changed during heartbeat. |
||
111 | * |
||
112 | * @throws RuntimeException Thrown, when we could not create new session and it was needed. |
||
113 | */ |
||
114 | 5 | public function heartbeat() |
|
140 | |||
141 | /** |
||
142 | * Return machine ID from consul queries. |
||
143 | * |
||
144 | * @return integer |
||
145 | * @throws RuntimeException |
||
146 | */ |
||
147 | 7 | private function acquireMachineId() |
|
169 | |||
170 | /** |
||
171 | * Try to fetch machine ID. |
||
172 | * |
||
173 | * @param array $currentValues |
||
174 | * @return integer |
||
175 | * @throws RuntimeException |
||
176 | */ |
||
177 | 6 | private function computePossibleMachineId(array $currentValues) |
|
197 | |||
198 | /** |
||
199 | * Lock master key. |
||
200 | */ |
||
201 | 6 | private function lockKey() |
|
213 | |||
214 | /** |
||
215 | * Release master key. |
||
216 | */ |
||
217 | 4 | private function releaseKey() |
|
221 | |||
222 | /** |
||
223 | * Create new session. |
||
224 | * |
||
225 | * @throws RuntimeException |
||
226 | */ |
||
227 | 12 | private function createSession() |
|
242 | |||
243 | /** |
||
244 | * Destroy session. |
||
245 | */ |
||
246 | 12 | private function destroySession() |
|
252 | } |
||
253 |