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); - } }