44
55use Hypernode \Api \Exception \HypernodeApiClientException ;
66use Hypernode \Api \Exception \HypernodeApiServerException ;
7+ use Hypernode \Api \Exception \ResponseException ;
78use Hypernode \Api \HypernodeClient ;
89use Hypernode \Api \HypernodeClientFactory ;
910use Hypernode \Api \Resource \Logbook \Flow ;
@@ -28,6 +29,7 @@ public function __construct(LoggerInterface $log)
2829 * @param string $hypernode The parent hypernode to query the Brancher instances from
2930 * @param string[] $labels Labels to match against, may be empty
3031 * @return string[] The found Brancher instance names
32+ * @throws ResponseException
3133 */
3234 public function queryBrancherHypernodes (string $ hypernode , array $ labels = []): array
3335 {
@@ -66,10 +68,21 @@ public function queryBrancherHypernodes(string $hypernode, array $labels = []):
6668 */
6769 public function reuseExistingBrancherHypernode (string $ hypernode , array $ labels = []): ?string
6870 {
69- $ brancherHypernodes = $ this ->queryBrancherHypernodes ($ hypernode , $ labels );
70- if (count ($ brancherHypernodes ) > 0 ) {
71- // Return the last brancher Hypernode, which is the most recently created one
72- return $ brancherHypernodes [count ($ brancherHypernodes ) - 1 ];
71+ try {
72+ $ brancherHypernodes = $ this ->queryBrancherHypernodes ($ hypernode , $ labels );
73+ if (count ($ brancherHypernodes ) > 0 ) {
74+ // Return the last brancher Hypernode, which is the most recently created one
75+ return $ brancherHypernodes [count ($ brancherHypernodes ) - 1 ];
76+ }
77+ } catch (ResponseException $ e ) {
78+ $ this ->log ->error (
79+ sprintf (
80+ 'Got an API exception (code %d) while querying for existing brancher Hypernodes for Hypernode %s with labels (%s). ' ,
81+ $ e ->getCode (),
82+ $ hypernode ,
83+ implode (', ' , $ labels )
84+ )
85+ );
7386 }
7487
7588 return null ;
@@ -108,16 +121,16 @@ public function waitForAvailability(string $brancherHypernode, int $timeout = 15
108121 $ interval = 3 ;
109122 $ allowedErrorWindow = 3 ;
110123
111- while ($ timeElapsed < $ timeout && ! $ resolved ) {
124+ while ($ timeElapsed < $ timeout ) {
112125 $ now = microtime (true );
113126 $ timeElapsed += $ now - $ latest ;
114127 $ latest = $ now ;
115128
116129 try {
117130 $ flows = $ this ->hypernodeClient ->logbook ->getList ($ brancherHypernode );
118- $ relevantFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->name === 'ensure_app ' );
119- $ failedFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->isReverted ());
120- $ completedFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->isComplete ());
131+ $ relevantFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->name === 'ensure_app ' );
132+ $ failedFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->isReverted ());
133+ $ completedFlows = array_filter ($ flows , fn (Flow $ flow ) => $ flow ->isComplete ());
121134
122135 if (count ($ failedFlows ) === count ($ relevantFlows )) {
123136 throw new CreateBrancherHypernodeFailedException ();
@@ -142,19 +155,52 @@ public function waitForAvailability(string $brancherHypernode, int $timeout = 15
142155 $ brancherHypernode
143156 )
144157 );
145- ;
146158 continue ;
147159 }
148160 }
149161
150162 sleep ($ interval );
151163 }
152164
165+ $ this ->log ->info (
166+ sprintf (
167+ 'Brancher Hypernode %s was delivered. Now waiting for node to become reachable... ' ,
168+ $ brancherHypernode
169+ )
170+ );
171+
153172 if (!$ resolved ) {
154173 throw new TimeoutException (
155- sprintf ('Timed out waiting for brancher Hypernode %s to become available ' , $ brancherHypernode )
174+ sprintf ('Timed out waiting for brancher Hypernode %s to be delivered ' , $ brancherHypernode )
156175 );
157176 }
177+
178+ $ resolved = false ;
179+ while ($ timeElapsed < $ timeout ) {
180+ $ now = microtime (true );
181+ $ timeElapsed += $ now - $ latest ;
182+ $ latest = $ now ;
183+
184+ $ connection = @fsockopen (sprintf ("%s.hypernode.io " , $ brancherHypernode ), 22 );
185+ if ($ connection ) {
186+ fclose ($ connection );
187+ $ resolved = true ;
188+ break ;
189+ }
190+ }
191+
192+ if (!$ resolved ) {
193+ throw new TimeoutException (
194+ sprintf ('Timed out waiting for brancher Hypernode %s to become reachable ' , $ brancherHypernode )
195+ );
196+ }
197+
198+ $ this ->log ->info (
199+ sprintf (
200+ 'Brancher Hypernode %s became reachable! ' ,
201+ $ brancherHypernode
202+ )
203+ );
158204 }
159205
160206 /**
0 commit comments