Skip to content

Commit

Permalink
Improuve Container States
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Yves <[email protected]>
  • Loading branch information
docjyJ committed Oct 21, 2024
1 parent a1bc150 commit 4798489
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 128 deletions.
18 changes: 6 additions & 12 deletions php/src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AIO\Data\ConfigurationManager;
use AIO\Docker\DockerActionManager;
use AIO\ContainerDefinitionFetcher;
use GuzzleHttp\Exception\GuzzleException;

readonly class Container {
public function __construct(
Expand Down Expand Up @@ -112,20 +113,13 @@ public function GetVolumes() : ContainerVolumes {
return $this->volumes;
}

public function GetRunningState() : ContainerState {
return $this->dockerActionManager->GetContainerRunningState($this);
/** @throws GuzzleException */
public function GetContainerState() : ContainerState {
return $this->dockerActionManager->GetContainerState($this);
}

public function GetRestartingState() : ContainerState {
return $this->dockerActionManager->GetContainerRestartingState($this);
}

public function GetUpdateState() : VersionState {
return $this->dockerActionManager->GetContainerUpdateState($this);
}

public function GetStartingState() : ContainerState {
return $this->dockerActionManager->GetContainerStartingState($this);
public function GetUpdateState() : UpdateState {
return $this->dockerActionManager->GetUpdateState($this);
}

/**
Expand Down
38 changes: 31 additions & 7 deletions php/src/Container/ContainerState.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

namespace AIO\Container;

enum ContainerState: string {
case ImageDoesNotExist = 'image_does_not_exist';
case NotRestarting = 'not_restarting';
case Restarting = 'restarting';
case Running = 'running';
case Starting = 'starting';
case Stopped = 'stopped';
enum ContainerState {
case DoesNotExist;
case Restarting;
case Healthy;
case Starting;
case Stopped;
case Unhealthy;

public function isStopped(): bool {
return $this == self::Stopped;
}

public function isStarting(): bool {
return $this == self::Starting;
}

public function isRestarting(): bool {
return $this == self::Restarting;
}

public function isHealthy(): bool {
return $this == self::Healthy;
}

public function isUnhealthy(): bool {
return $this == self::Unhealthy;
}

public function isRunning(): bool {
return $this->isHealthy() || $this->isUnhealthy() || $this->isStarting() || $this->isRestarting();
}
}
12 changes: 12 additions & 0 deletions php/src/Container/UpdateState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace AIO\Container;

enum UpdateState {
case Outdated;
case Latest;

public function isUpdatableAvailable(): bool {
return $this == self::Outdated;
}
}
8 changes: 0 additions & 8 deletions php/src/Container/VersionState.php

This file was deleted.

9 changes: 6 additions & 3 deletions php/src/Controller/DockerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AIO\Container\ContainerState;
use AIO\ContainerDefinitionFetcher;
use AIO\Docker\DockerActionManager;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use AIO\Data\ConfigurationManager;
Expand All @@ -19,6 +20,7 @@ public function __construct(
) {
}

/** @throws GuzzleException */
private function PerformRecursiveContainerStart(string $id, bool $pullImage = true) : void {
$container = $this->containerDefinitionFetcher->GetContainerById($id);

Expand All @@ -28,7 +30,7 @@ private function PerformRecursiveContainerStart(string $id, bool $pullImage = tr

// Don't start if container is already running
// This is expected to happen if a container is defined in depends_on of multiple containers
if ($container->GetRunningState() === ContainerState::Running) {
if ($container->GetContainerState()->isRunning()) {
error_log('Not starting ' . $id . ' because it was already started.');
return;
}
Expand Down Expand Up @@ -240,6 +242,7 @@ public function stopTopContainer() : void {
$this->PerformRecursiveContainerStop($id);
}

/** @throws GuzzleException */
public function StartDomaincheckContainer() : void
{
# Don't start if domain is already set
Expand All @@ -254,10 +257,10 @@ public function StartDomaincheckContainer() : void
$domaincheckContainer = $this->containerDefinitionFetcher->GetContainerById($id);
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById(self::TOP_CONTAINER);
// Don't start if apache is already running
if ($apacheContainer->GetRunningState() === ContainerState::Running) {
if ($apacheContainer->GetContainerState()->isRunning()) {
return;
// Don't start if domaincheck is already running
} elseif ($domaincheckContainer->GetRunningState() === ContainerState::Running) {
} elseif ($domaincheckContainer->GetContainerState()->isRunning()) {
$domaincheckWasStarted = apcu_fetch($cacheKey);
// Start domaincheck again when 10 minutes are over by not returning here
if($domaincheckWasStarted !== false && is_string($domaincheckWasStarted)) {
Expand Down
Loading

0 comments on commit 4798489

Please sign in to comment.