Skip to content

Commit 54edc1f

Browse files
committed
more robust waitForAvailability
If the DNS change has not propagated yet to all nameservers, then it may be that the first reachability success goes through but a second one falis. in that case we are better off first trying for 'consecutive success' before we proceed with the deploy can be used in deploy.php like: ``` $testingStage->addBrancherServer("$APP_NAME") ->setLabels(['autodestroy=false', 'stage=acceptance', 'ci_ref=' . (\getenv('GITHUB_HEAD_REF') ?: 'none')]) ->setBrancherReachabilityCheckCount(6) // Amount of consecutive checks required ->setBrancherReachabilityCheckInterval(10); // Amount of seconds between checks ```
1 parent b45aa52 commit 54edc1f

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/Brancher/BrancherHypernodeManager.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,20 @@ public function createForHypernode(string $hypernode, array $data = []): string
107107
*
108108
* @param string $brancherHypernode Name of the brancher Hypernode
109109
* @param int $timeout Maximum time to wait for availability
110+
* @param int $reachabilityCheckCount Number of consecutive successful checks required
111+
* @param int $reachabilityCheckInterval Seconds between reachability checks
110112
* @return void
111113
* @throws CreateBrancherHypernodeFailedException
112114
* @throws HypernodeApiClientException
113115
* @throws HypernodeApiServerException
114116
* @throws TimeoutException
115117
*/
116-
public function waitForAvailability(string $brancherHypernode, int $timeout = 1500): void
117-
{
118+
public function waitForAvailability(
119+
string $brancherHypernode,
120+
int $timeout = 1500,
121+
int $reachabilityCheckCount = 6,
122+
int $reachabilityCheckInterval = 10
123+
): void {
118124
$latest = microtime(true);
119125
$timeElapsed = 0;
120126
$resolved = false;
@@ -175,7 +181,7 @@ public function waitForAvailability(string $brancherHypernode, int $timeout = 15
175181
);
176182
}
177183

178-
$resolved = false;
184+
$consecutiveSuccesses = 0;
179185
while ($timeElapsed < $timeout) {
180186
$now = microtime(true);
181187
$timeElapsed += $now - $latest;
@@ -184,12 +190,37 @@ public function waitForAvailability(string $brancherHypernode, int $timeout = 15
184190
$connection = @fsockopen(sprintf("%s.hypernode.io", $brancherHypernode), 22);
185191
if ($connection) {
186192
fclose($connection);
187-
$resolved = true;
188-
break;
193+
$consecutiveSuccesses++;
194+
$this->log->info(
195+
sprintf(
196+
'Brancher Hypernode %s reachability check %d/%d succeeded.',
197+
$brancherHypernode,
198+
$consecutiveSuccesses,
199+
$reachabilityCheckCount
200+
)
201+
);
202+
203+
if ($consecutiveSuccesses >= $reachabilityCheckCount) {
204+
break;
205+
}
206+
sleep($reachabilityCheckInterval);
207+
} else {
208+
if ($consecutiveSuccesses > 0) {
209+
$this->log->info(
210+
sprintf(
211+
'Brancher Hypernode %s reachability check failed, resetting counter (was at %d/%d).',
212+
$brancherHypernode,
213+
$consecutiveSuccesses,
214+
$reachabilityCheckCount
215+
)
216+
);
217+
}
218+
$consecutiveSuccesses = 0;
219+
sleep($reachabilityCheckInterval);
189220
}
190221
}
191222

192-
if (!$resolved) {
223+
if ($consecutiveSuccesses < $reachabilityCheckCount) {
193224
throw new TimeoutException(
194225
sprintf('Timed out waiting for brancher Hypernode %s to become reachable', $brancherHypernode)
195226
);

src/DeployRunner.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ private function maybeConfigureBrancherServer(Server $server, bool $reuseBranche
274274
if ($isBrancher && $parentApp) {
275275
$settings = $serverOptions[Server::OPTION_HN_BRANCHER_SETTINGS] ?? [];
276276
$labels = $serverOptions[Server::OPTION_HN_BRANCHER_LABELS] ?? [];
277+
$reachabilityCheckCount = $serverOptions[Server::OPTION_HN_BRANCHER_REACHABILITY_CHECK_COUNT] ?? 6;
278+
$reachabilityCheckInterval = $serverOptions[Server::OPTION_HN_BRANCHER_REACHABILITY_CHECK_INTERVAL] ?? 10;
277279

278280
$this->log->info(sprintf('Creating an brancher Hypernode based on %s.', $parentApp));
279281
if ($settings) {
@@ -299,7 +301,12 @@ private function maybeConfigureBrancherServer(Server $server, bool $reuseBranche
299301

300302
try {
301303
$this->log->info('Waiting for brancher Hypernode to become available...');
302-
$this->brancherHypernodeManager->waitForAvailability($brancherApp);
304+
$this->brancherHypernodeManager->waitForAvailability(
305+
$brancherApp,
306+
1500,
307+
$reachabilityCheckCount,
308+
$reachabilityCheckInterval
309+
);
303310
$this->log->info('Brancher Hypernode has become available!');
304311
} catch (CreateBrancherHypernodeFailedException | TimeoutException $e) {
305312
if (in_array($brancherApp, $this->brancherHypernodesRegistered)) {

0 commit comments

Comments
 (0)