From 3e04a7958ea16700c44081dcaccd9cff0ab443e8 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Wed, 4 Sep 2024 20:41:17 +0200 Subject: [PATCH] Feat/Fix: Proxy stop and restart confirmation --- app/Livewire/Server/Proxy/Deploy.php | 48 ++++++++++++++----- .../livewire/server/proxy/deploy.blade.php | 28 ++++++++--- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/app/Livewire/Server/Proxy/Deploy.php b/app/Livewire/Server/Proxy/Deploy.php index 2279951ee..f7318c8f4 100644 --- a/app/Livewire/Server/Proxy/Deploy.php +++ b/app/Livewire/Server/Proxy/Deploy.php @@ -7,6 +7,8 @@ use App\Actions\Proxy\StartProxy; use App\Events\ProxyStatusChanged; use App\Models\Server; use Livewire\Component; +use Illuminate\Support\Facades\Process; +use Illuminate\Process\InvokedProcess; class Deploy extends Component { @@ -94,21 +96,43 @@ class Deploy extends Component public function stop(bool $forceStop = true) { try { - if ($this->server->isSwarm()) { - instant_remote_process([ - 'docker service rm coolify-proxy_traefik', - ], $this->server); - } else { - instant_remote_process([ - 'docker rm -f coolify-proxy', - ], $this->server); + $containerName = $this->server->isSwarm() ? 'coolify-proxy_traefik' : 'coolify-proxy'; + $timeout = 30; + + $process = $this->stopContainer($containerName, $timeout); + + $startTime = time(); + while ($process->running()) { + if (time() - $startTime >= $timeout) { + $this->forceStopContainer($containerName); + break; + } + usleep(100000); } - $this->server->proxy->status = 'exited'; - $this->server->proxy->force_stop = $forceStop; - $this->server->save(); - $this->dispatch('proxyStatusUpdated'); + + $this->removeContainer($containerName); } catch (\Throwable $e) { return handleError($e, $this); + } finally { + $this->server->proxy->force_stop = $forceStop; + $this->server->proxy->status = 'exited'; + $this->server->save(); + $this->dispatch('proxyStatusUpdated'); } } + + private function stopContainer(string $containerName, int $timeout): InvokedProcess + { + return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName"); + } + + private function forceStopContainer(string $containerName) + { + instant_remote_process(["docker kill $containerName"], $this->server, throwError: false); + } + + private function removeContainer(string $containerName) + { + instant_remote_process(["docker rm -f $containerName"], $this->server, throwError: false); + } } diff --git a/resources/views/livewire/server/proxy/deploy.blade.php b/resources/views/livewire/server/proxy/deploy.blade.php index dba4ce80c..cee78d7a0 100644 --- a/resources/views/livewire/server/proxy/deploy.blade.php +++ b/resources/views/livewire/server/proxy/deploy.blade.php @@ -24,7 +24,17 @@ @endif - + Restart Proxy - This proxy will be stopped and started. It is not reversible.
All resources will be unavailable - during the restart.
Please think again.
- + Stop Proxy - This proxy will be stopped. It is not reversible.
All resources will be unavailable. -
Please think again.
@else