Skip to content

Commit

Permalink
Added container healthcheck. Updated daemon list command
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey18106 committed Jul 19, 2023
1 parent f502e36 commit 9d74779
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Command/Daemon/ListDaemons.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$output->writeln('Registered ExApp daemon configs:');
foreach ($daemonConfigs as $daemon) {
$output->writeln(sprintf('%s. %s [%s]: %s://%s', $daemon->getId(), $daemon->getDisplayName(), $daemon->getAcceptsDeployId(), $daemon->getProtocol(), $daemon->getHost()));
$output->writeln(sprintf('%s. %s - %s [%s]: %s://%s', $daemon->getId(), $daemon->getName(), $daemon->getDisplayName(), $daemon->getAcceptsDeployId(), $daemon->getProtocol(), $daemon->getHost()));
}

return 0;
Expand Down
5 changes: 5 additions & 0 deletions lib/Command/ExApp/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (!isset($startResult['error']) && isset($createResult['Id'])) {
if (!$this->dockerActions->healthcheckContainer($createResult['Id'], $daemonConfig)) {
$output->writeln(sprintf('ExApp %s deployment failed. Error: %s', $appId, 'Container healthcheck failed.'));
return 1;
}

// TODO: Remove resultOutput
$resultOutput = [
'appid' => $appId,
Expand Down
18 changes: 18 additions & 0 deletions lib/DeployActions/DockerActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ public function resolveDeployExAppHost(string $appId, DaemonConfig $daemonConfig
return $host;
}

public function containerStateHealthy(array $containerInfo): bool {
return $containerInfo['State']['Health']['Status'] === 'healthy' && $containerInfo['State']['Status'] === 'running';
}

public function healthcheckContainer(string $containerId, DaemonConfig $daemonConfig): bool {
$attempts = 0;
$totalAttempts = 60; // ~60 seconds for container to initialize
while ($attempts < $totalAttempts) {
$containerInfo = $this->inspectContainer($this->buildDockerUrl($daemonConfig), $containerId);
if ($this->containerStateHealthy($containerInfo)) {
return true;
}
$attempts++;
sleep(1);
}
return false;
}

public function buildDockerUrl(DaemonConfig $daemonConfig): string {
$dockerUrl = 'http://localhost';
if (in_array($daemonConfig->getProtocol(), ['http', 'https'])) {
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/DaemonConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public function unregisterDaemonConfig(DaemonConfig $daemonConfig): ?DaemonConfi
}
}

/**
* @return DaemonConfig[]|null
*/
public function getRegisteredDaemonConfigs(): ?array {
try {
$cacheKey = '/daemon_configs';
Expand Down

0 comments on commit 9d74779

Please sign in to comment.