From 331693546bfb7197ac773062d12c88e78e6f9474 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Wed, 2 Apr 2025 19:38:31 +0200 Subject: [PATCH] fix(proxy): proxy restart does not work on domain - When you restart the proxy on an instance domain, the proxy stops and is removed, but never restarted. So you loose access over the domain and have to go in over IP and Port. This is because we are doing the restart directly in the UI instead of in the background via a job, and the proxy is serving the UI domain. --- app/Actions/Proxy/StopProxy.php | 56 ++++++++++++++++++++++++++++ app/Jobs/RestartProxyJob.php | 46 +++++++++++++++++++++++ app/Livewire/Server/Proxy/Deploy.php | 43 +++------------------ 3 files changed, 107 insertions(+), 38 deletions(-) create mode 100644 app/Actions/Proxy/StopProxy.php create mode 100644 app/Jobs/RestartProxyJob.php diff --git a/app/Actions/Proxy/StopProxy.php b/app/Actions/Proxy/StopProxy.php new file mode 100644 index 000000000..a5dcc6cf4 --- /dev/null +++ b/app/Actions/Proxy/StopProxy.php @@ -0,0 +1,56 @@ +isSwarm() ? 'coolify-proxy_traefik' : 'coolify-proxy'; + $timeout = 30; + + $process = $this->stopContainer($containerName, $timeout); + + $startTime = Carbon::now()->getTimestamp(); + while ($process->running()) { + if (Carbon::now()->getTimestamp() - $startTime >= $timeout) { + $this->forceStopContainer($containerName, $server); + break; + } + usleep(100000); + } + + $this->removeContainer($containerName, $server); + } catch (\Throwable $e) { + return handleError($e); + } finally { + $server->proxy->force_stop = $forceStop; + $server->proxy->status = 'exited'; + $server->save(); + } + } + + private function stopContainer(string $containerName, int $timeout): InvokedProcess + { + return Process::timeout($timeout)->start("docker stop --time=$timeout $containerName"); + } + + private function forceStopContainer(string $containerName, Server $server) + { + instant_remote_process(["docker kill $containerName"], $server, throwError: false); + } + + private function removeContainer(string $containerName, Server $server) + { + instant_remote_process(["docker rm -f $containerName"], $server, throwError: false); + } +} diff --git a/app/Jobs/RestartProxyJob.php b/app/Jobs/RestartProxyJob.php new file mode 100644 index 000000000..7fc716f70 --- /dev/null +++ b/app/Jobs/RestartProxyJob.php @@ -0,0 +1,46 @@ +server->uuid))->dontRelease()]; + } + + public function __construct(public Server $server) {} + + public function handle() + { + try { + StopProxy::run($this->server); + + $this->server->proxy->force_stop = false; + $this->server->save(); + StartProxy::run($this->server, force: true); + + CheckProxy::run($this->server, true); + } catch (\Throwable $e) { + return handleError($e); + } + } +} diff --git a/app/Livewire/Server/Proxy/Deploy.php b/app/Livewire/Server/Proxy/Deploy.php index 4a7e4124e..48eede4e5 100644 --- a/app/Livewire/Server/Proxy/Deploy.php +++ b/app/Livewire/Server/Proxy/Deploy.php @@ -4,11 +4,10 @@ namespace App\Livewire\Server\Proxy; use App\Actions\Proxy\CheckProxy; use App\Actions\Proxy\StartProxy; +use App\Actions\Proxy\StopProxy; use App\Events\ProxyStatusChanged; +use App\Jobs\RestartProxyJob; use App\Models\Server; -use Carbon\Carbon; -use Illuminate\Process\InvokedProcess; -use Illuminate\Support\Facades\Process; use Livewire\Component; class Deploy extends Component @@ -65,6 +64,7 @@ class Deploy extends Component public function restart() { try { + RestartProxyJob::dispatch($this->server); $this->dispatch('checkProxy'); } catch (\Throwable $e) { return handleError($e, $this); @@ -97,43 +97,10 @@ class Deploy extends Component public function stop(bool $forceStop = true) { try { - $containerName = $this->server->isSwarm() ? 'coolify-proxy_traefik' : 'coolify-proxy'; - $timeout = 30; - - $process = $this->stopContainer($containerName, $timeout); - - $startTime = Carbon::now()->getTimestamp(); - while ($process->running()) { - if (Carbon::now()->getTimestamp() - $startTime >= $timeout) { - $this->forceStopContainer($containerName); - break; - } - usleep(100000); - } - - $this->removeContainer($containerName); + StopProxy::run($this->server, $forceStop); + $this->dispatch('proxyStatusUpdated'); } 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); - } }