From e516aeb534ec96f6302639237b8f33f9203bd3cd Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Wed, 30 Apr 2025 09:59:03 +0200 Subject: [PATCH] refactor(Database): streamline container shutdown process and reduce timeout duration --- app/Actions/Database/StopDatabase.php | 36 ++++----------------------- app/Jobs/ApplicationDeploymentJob.php | 21 +++++----------- 2 files changed, 11 insertions(+), 46 deletions(-) diff --git a/app/Actions/Database/StopDatabase.php b/app/Actions/Database/StopDatabase.php index de4eaa31f..f8f6f8519 100644 --- a/app/Actions/Database/StopDatabase.php +++ b/app/Actions/Database/StopDatabase.php @@ -11,7 +11,6 @@ use App\Models\StandaloneMongodb; use App\Models\StandaloneMysql; use App\Models\StandalonePostgresql; use App\Models\StandaloneRedis; -use Illuminate\Support\Facades\Process; use Lorisleiva\Actions\Concerns\AsAction; class StopDatabase @@ -39,37 +38,12 @@ class StopDatabase return 'Database stopped successfully'; } - private function stopContainer($database, string $containerName, int $timeout = 300): void + private function stopContainer($database, string $containerName, int $timeout = 30): void { $server = $database->destination->server; - - $process = Process::timeout($timeout)->start("docker stop --time=$timeout $containerName"); - - $startTime = time(); - while ($process->running()) { - if (time() - $startTime >= $timeout) { - $this->forceStopContainer($containerName, $server); - break; - } - usleep(100000); - } - - $this->removeContainer($containerName, $server); - } - - private function forceStopContainer(string $containerName, $server): void - { - instant_remote_process(command: ["docker kill $containerName"], server: $server, throwError: false); - } - - private function removeContainer(string $containerName, $server): void - { - instant_remote_process(command: ["docker rm -f $containerName"], server: $server, throwError: false); - } - - private function deleteConnectedNetworks($uuid, $server) - { - instant_remote_process(["docker network disconnect {$uuid} coolify-proxy"], $server, false); - instant_remote_process(["docker network rm {$uuid}"], $server, false); + instant_remote_process(command: [ + "docker stop --time=$timeout $containerName", + "docker rm -f $containerName", + ], server: $server, throwError: false); } } diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index d79e09151..bebec64cf 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -27,7 +27,6 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Process; use Illuminate\Support\Sleep; use Illuminate\Support\Str; use RuntimeException; @@ -2246,24 +2245,16 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf"); $this->application_deployment_queue->addLogEntry('Building docker image completed.'); } - private function graceful_shutdown_container(string $containerName, int $timeout = 300) + private function graceful_shutdown_container(string $containerName, int $timeout = 30) { try { - $this->execute_remote_command( - ["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true], - ); - } catch (\Exception $error) { + $this->execute_remote_command( + ["docker stop --time=$timeout $containerName", 'hidden' => true, 'ignore_errors' => true], + ["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true] + ); + } catch (Exception $error) { $this->application_deployment_queue->addLogEntry("Error stopping container $containerName: ".$error->getMessage(), 'stderr'); } - - $this->remove_container($containerName); - } - - private function remove_container(string $containerName) - { - $this->execute_remote_command( - ["docker rm -f $containerName", 'hidden' => true, 'ignore_errors' => true] - ); } private function stop_running_container(bool $force = false)