Skip to content

Commit 994d2c2

Browse files
authored
Merge pull request #184 from ByteInternet/more-robust-dns-check
more robust waitForAvailability
2 parents b45aa52 + a05e355 commit 994d2c2

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

ci/test/magento/deploy_brancher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
$productionStage = $configuration->addStage('test', 'banaan.store');
2121
$productionStage->addBrancherServer('hndeployintegr8')
22+
->setBrancherTimeout(3000)
2223
->setLabels(['gitref='. (\getenv('GITHUB_SHA') ?: 'unknown')]);
2324

2425
return $configuration;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"doctrine/annotations": "^1.6",
1818
"guzzlehttp/guzzle": "^7.5",
1919
"hypernode/api-client": "^0.5",
20-
"hypernode/deploy-configuration": "^3.5",
20+
"hypernode/deploy-configuration": "^3.6",
2121
"php-di/php-di": "^7.0",
2222
"psr/log": "^1.0",
2323
"symfony/console": "^5.4",

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class DeployRunner
3535
public const TASK_BUILD = 'build';
3636
public const TASK_DEPLOY = 'deploy';
3737

38+
public const DEFAULT_BRANCHER_TIMEOUT = 1500;
39+
public const DEFAULT_BRANCHER_REACHABILITY_CHECK_COUNT = 6;
40+
public const DEFAULT_BRANCHER_REACHABILITY_CHECK_INTERVAL = 10;
41+
3842
private TaskFactory $taskFactory;
3943
private InputInterface $input;
4044
private LoggerInterface $log;
@@ -274,6 +278,9 @@ private function maybeConfigureBrancherServer(Server $server, bool $reuseBranche
274278
if ($isBrancher && $parentApp) {
275279
$settings = $serverOptions[Server::OPTION_HN_BRANCHER_SETTINGS] ?? [];
276280
$labels = $serverOptions[Server::OPTION_HN_BRANCHER_LABELS] ?? [];
281+
$timeout = $serverOptions[Server::OPTION_HN_BRANCHER_TIMEOUT] ?? self::DEFAULT_BRANCHER_TIMEOUT;
282+
$reachabilityCheckCount = $serverOptions[Server::OPTION_HN_BRANCHER_REACHABILITY_CHECK_COUNT] ?? self::DEFAULT_BRANCHER_REACHABILITY_CHECK_COUNT;
283+
$reachabilityCheckInterval = $serverOptions[Server::OPTION_HN_BRANCHER_REACHABILITY_CHECK_INTERVAL] ?? self::DEFAULT_BRANCHER_REACHABILITY_CHECK_INTERVAL;
277284

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

300307
try {
301308
$this->log->info('Waiting for brancher Hypernode to become available...');
302-
$this->brancherHypernodeManager->waitForAvailability($brancherApp);
309+
$this->brancherHypernodeManager->waitForAvailability(
310+
$brancherApp,
311+
$timeout,
312+
$reachabilityCheckCount,
313+
$reachabilityCheckInterval
314+
);
303315
$this->log->info('Brancher Hypernode has become available!');
304316
} catch (CreateBrancherHypernodeFailedException | TimeoutException $e) {
305317
if (in_array($brancherApp, $this->brancherHypernodesRegistered)) {

0 commit comments

Comments
 (0)