From 806d892031b1da544747567d7c6dd7f81b189e08 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Mon, 24 Mar 2025 11:43:10 +0100 Subject: [PATCH] refactor(application): streamline healthcheck parsing from Dockerfile --- app/Jobs/ApplicationDeploymentJob.php | 5 ++- app/Livewire/Project/Application/General.php | 3 ++ app/Livewire/Project/New/SimpleDockerfile.php | 2 +- app/Models/Application.php | 22 +++++++------ bootstrap/helpers/shared.php | 32 +++++++++++++++++++ 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 530136378..92186953b 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1211,7 +1211,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue if ($this->container_name) { $counter = 1; $this->application_deployment_queue->addLogEntry('Waiting for healthcheck to pass on the new container.'); - if ($this->full_healthcheck_url) { + if ($this->full_healthcheck_url && ! $this->application->custom_healthcheck_found) { $this->application_deployment_queue->addLogEntry("Healthcheck URL (inside the container): {$this->full_healthcheck_url}"); } $this->application_deployment_queue->addLogEntry("Waiting for the start period ({$this->application->health_check_start_period} seconds) before starting healthcheck."); @@ -1718,8 +1718,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue 'save' => 'dockerfile_from_repo', 'ignore_errors' => true, ]); - $dockerfile = collect(str($this->saved_outputs->get('dockerfile_from_repo'))->trim()->explode("\n")); - $this->application->parseHealthcheckFromDockerfile($dockerfile); + $this->application->parseHealthcheckFromDockerfile($this->saved_outputs->get('dockerfile_from_repo')); } $docker_compose = [ 'services' => [ diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php index 08fff38c6..1d58ed33a 100644 --- a/app/Livewire/Project/Application/General.php +++ b/app/Livewire/Project/Application/General.php @@ -369,6 +369,9 @@ class General extends Component if ($this->application->isDirty('redirect')) { $this->setRedirect(); } + if ($this->application->isDirty('dockerfile')) { + $this->application->parseHealthcheckFromDockerfile($this->application->dockerfile); + } $this->checkFqdns(); diff --git a/app/Livewire/Project/New/SimpleDockerfile.php b/app/Livewire/Project/New/SimpleDockerfile.php index c3ed6039a..ebc9878dc 100644 --- a/app/Livewire/Project/New/SimpleDockerfile.php +++ b/app/Livewire/Project/New/SimpleDockerfile.php @@ -74,7 +74,7 @@ CMD ["nginx", "-g", "daemon off;"] 'fqdn' => $fqdn, ]); - $application->parseHealthcheckFromDockerfile(dockerfile: collect(str($this->dockerfile)->trim()->explode("\n")), isInit: true); + $application->parseHealthcheckFromDockerfile(dockerfile: $this->dockerfile, isInit: true); return redirect()->route('project.application.configuration', [ 'application_uuid' => $application->uuid, diff --git a/app/Models/Application.php b/app/Models/Application.php index ad688c93f..57a69423d 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -1507,6 +1507,7 @@ class Application extends BaseModel public function parseHealthcheckFromDockerfile($dockerfile, bool $isInit = false) { + $dockerfile = str($dockerfile)->trim()->explode("\n"); if (str($dockerfile)->contains('HEALTHCHECK') && ($this->isHealthcheckDisabled() || $isInit)) { $healthcheckCommand = null; $lines = $dockerfile->toArray(); @@ -1526,23 +1527,24 @@ class Application extends BaseModel } } if (str($healthcheckCommand)->isNotEmpty()) { - $interval = str($healthcheckCommand)->match('/--interval=(\d+)/'); - $timeout = str($healthcheckCommand)->match('/--timeout=(\d+)/'); - $start_period = str($healthcheckCommand)->match('/--start-period=(\d+)/'); - $start_interval = str($healthcheckCommand)->match('/--start-interval=(\d+)/'); + $interval = str($healthcheckCommand)->match('/--interval=([0-9]+[a-zµ]*)/'); + $timeout = str($healthcheckCommand)->match('/--timeout=([0-9]+[a-zµ]*)/'); + $start_period = str($healthcheckCommand)->match('/--start-period=([0-9]+[a-zµ]*)/'); + $start_interval = str($healthcheckCommand)->match('/--start-interval=([0-9]+[a-zµ]*)/'); $retries = str($healthcheckCommand)->match('/--retries=(\d+)/'); + if ($interval->isNotEmpty()) { - $this->health_check_interval = $interval->toInteger(); + $this->health_check_interval = parseDockerfileInterval($interval); } if ($timeout->isNotEmpty()) { - $this->health_check_timeout = $timeout->toInteger(); + $this->health_check_timeout = parseDockerfileInterval($timeout); } if ($start_period->isNotEmpty()) { - $this->health_check_start_period = $start_period->toInteger(); + $this->health_check_start_period = parseDockerfileInterval($start_period); + } + if ($start_interval->isNotEmpty()) { + $this->health_check_start_interval = parseDockerfileInterval($start_interval); } - // if ($start_interval) { - // $this->health_check_start_interval = $start_interval->value(); - // } if ($retries->isNotEmpty()) { $this->health_check_retries = $retries->toInteger(); } diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index db3085649..60e71cd9c 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -4137,3 +4137,35 @@ function getJobStatus(?string $jobId = null) return $jobFound->first()->status; } + +function parseDockerfileInterval(string $something) +{ + $value = preg_replace('/[^0-9]/', '', $something); + $unit = preg_replace('/[0-9]/', '', $something); + + // Default to seconds if no unit specified + $unit = $unit ?: 's'; + + // Convert to seconds based on unit + $seconds = (int) $value; + switch ($unit) { + case 'ns': + $seconds = (int) ($value / 1000000000); + break; + case 'us': + case 'µs': + $seconds = (int) ($value / 1000000); + break; + case 'ms': + $seconds = (int) ($value / 1000); + break; + case 'm': + $seconds = (int) ($value * 60); + break; + case 'h': + $seconds = (int) ($value * 3600); + break; + } + + return $seconds; +}