From a4f107e191c64e2f56054cfdc5e83afe63409359 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 6 Aug 2024 13:05:34 +0200 Subject: [PATCH] fix: graceful shutdown --- app/Jobs/ApplicationDeploymentJob.php | 40 ++++++++++--------- app/Livewire/Project/Application/Advanced.php | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 2e37745fd..fad5ad185 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -1025,7 +1025,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue $this->write_deployment_configurations(); $this->server = $this->original_server; } - if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name) || $this->pull_request_id !== 0 || str($this->application->custom_docker_run_options)->contains('--ip') || str($this->application->custom_docker_run_options)->contains('--ip6')) { + if (count($this->application->ports_mappings_array) > 0 || (bool) $this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty() || $this->pull_request_id !== 0 || str($this->application->custom_docker_run_options)->contains('--ip') || str($this->application->custom_docker_run_options)->contains('--ip6')) { $this->application_deployment_queue->addLogEntry('----------------------------------------'); if (count($this->application->ports_mappings_array) > 0) { $this->application_deployment_queue->addLogEntry('Application has ports mapped to the host system, rolling update is not supported.'); @@ -1517,7 +1517,6 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue { $this->create_workdir(); $ports = $this->application->main_port(); - ray('generate_compose_file: ', $ports); $onlyPort = null; if (count($ports) > 0) { $onlyPort = $ports[0]; @@ -2029,38 +2028,41 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); } /** - * @param integer $timeout in seconds + * @param int $timeout in seconds */ - private function graceful_shutdown_container(string $containerName, int $timeout = 300) { + private function graceful_shutdown_container(string $containerName, int $timeout = 60) + { try { - return $this->execute_remote_command( - ["docker stop --time=$timeout $containerName > /dev/null 2>&1", 'hidden' => true], - ["docker rm $containerName > /dev/null 2>&1", 'hidden' => true] + $this->execute_remote_command( + ["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true], + ["docker rm $containerName", 'hidden' => true, 'ignore_errors' => true] ); } catch (\Exception $error) { // report error if needed } $this->execute_remote_command( - ["docker rm -f $containerName >/dev/null 2>&1", 'hidden' => true, 'ignore_errors' => true] + ["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true] ); + } private function stop_running_container(bool $force = false) { $this->application_deployment_queue->addLogEntry('Removing old containers.'); if ($this->newVersionIsHealthy || $force) { - $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); - if ($this->pull_request_id === 0) { - $containers = $containers->filter(function ($container) { - return data_get($container, 'Names') !== $this->container_name && data_get($container, 'Names') !== $this->container_name.'-pr-'.$this->pull_request_id; - }); - } - $containers->each(function ($container) { - $this->graceful_shutdown_container(data_get($container, 'Names')); - }); - if ($this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name)) { + if ($this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty()) { $this->graceful_shutdown_container($this->container_name); + } else { + $containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id); + if ($this->pull_request_id === 0) { + $containers = $containers->filter(function ($container) { + return data_get($container, 'Names') !== $this->container_name && data_get($container, 'Names') !== $this->container_name.'-pr-'.$this->pull_request_id; + }); + } + $containers->each(function ($container) { + $this->graceful_shutdown_container(data_get($container, 'Names')); + }); } } else { if ($this->application->dockerfile || $this->application->build_pack === 'dockerfile' || $this->application->build_pack === 'dockerimage') { @@ -2247,7 +2249,7 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); ray($code); if ($code !== 69420) { // 69420 means failed to push the image to the registry, so we don't need to remove the new version as it is the currently running one - if ($this->application->settings->is_consistent_container_name_enabled || isset($this->application->settings->custom_internal_name)) { + if ($this->application->settings->is_consistent_container_name_enabled || str($this->application->settings->custom_internal_name)->isNotEmpty()) { // do not remove already running container } else { $this->application_deployment_queue->addLogEntry('Deployment failed. Removing the new version of your application.', 'stderr'); diff --git a/app/Livewire/Project/Application/Advanced.php b/app/Livewire/Project/Application/Advanced.php index 2bc28026f..cde3322db 100644 --- a/app/Livewire/Project/Application/Advanced.php +++ b/app/Livewire/Project/Application/Advanced.php @@ -91,7 +91,7 @@ class Advanced extends Component public function saveCustomName() { - if (isset($this->application->settings->custom_internal_name)) { + if (str($this->application->settings->custom_internal_name)->isNotEmpty()) { $this->application->settings->custom_internal_name = str($this->application->settings->custom_internal_name)->slug()->value(); } else { $this->application->settings->custom_internal_name = null;