first working version with simple WaitForContainerRunning implementation

This commit is contained in:
Sergei Shitikov
2024-08-29 18:01:04 +02:00
parent 27caae56c0
commit b7a274b1d9
8 changed files with 276 additions and 201 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Testcontainers\Wait;
use Docker\API\Model\ContainersIdJsonGetResponse200;
use Docker\Docker;
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
use Testcontainers\Exception\ContainerNotReadyException;
/**
* Simply makes container inspect and checks if container is running.
* Uses $timout and $pollInterval in milliseconds to set the parameters for waiting.
*/
class WaitForContainerRunning implements WaitInterface
{
protected Docker $dockerClient;
public function __construct(protected int $timeout = 10000, protected int $pollInterval = 500)
{
$this->dockerClient = ContainerRuntimeClient::getDockerClient();
}
public function wait(string $id): void
{
$startTime = microtime(true) * 1000;
while (true) {
$elapsedTime = (microtime(true) * 1000) - $startTime;
if ($elapsedTime > $this->timeout) {
throw new ContainerNotReadyException($id);
}
/** @var ContainersIdJsonGetResponse200 | null $containerInspect */
$containerInspect = $this->dockerClient->containerInspect($id);
$containerStatus = $containerInspect?->getState()?->getStatus();
if ($containerStatus === 'running') {
return;
}
usleep($this->pollInterval * 1000);
}
}
}
+31 -7
View File
@@ -5,24 +5,48 @@ declare(strict_types=1);
namespace Testcontainers\Wait;
use Docker\Docker;
use Docker\DockerClientFactory;
use Http\Client\Socket\Exception\TimeoutException;
use Testcontainers\ContainerRuntime\ContainerRuntimeClient;
use Testcontainers\Exception\ContainerNotReadyException;
class WaitForHealthCheck implements WaitInterface
{
protected Docker $dockerClient;
protected int $timeout;
protected int $pollInterval;
public function __construct()
public function __construct(int $timeout = 5000, int $pollInterval = 1000)
{
$this->dockerClient = Docker::create();
$this->dockerClient = ContainerRuntimeClient::getDockerClient();
$this->timeout = $timeout;
$this->pollInterval = $pollInterval;
}
public function wait(string $id): void
{
$containerInspect = $this->dockerClient->containerInspect($id);
$containerInspect->getBody()->getContents();
dd($containerInspect->getStatusCode());
$startTime = microtime(true) * 1000;
if ($status !== 'healthy') {
throw new ContainerNotReadyException($id);
while (true) {
$elapsedTime = (microtime(true) * 1000) - $startTime;
if ($elapsedTime > $this->timeout) {
throw new TimeoutException(sprintf("Health check not healthy after %d ms", $this->timeout));
}
$containerInspect = $this->dockerClient->containerInspect($id, [], Docker::FETCH_RESPONSE);
//$containerStatus = $containerInspect?->getArrayCopy() ?? null;
var_dump($containerInspect->getBody()->getContents());
$containerStatus='';
if ($containerStatus === 'healthy') {
return;
}
if ($containerStatus === 'unhealthy') {
throw new ContainerNotReadyException(sprintf("Health check failed: %s", $containerStatus));
}
usleep($this->pollInterval * 1000); // Sleep for the polling interval
}
}
}