From 6071412986ae028d86f3d9f1430f7428e215e722 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 17 Oct 2023 19:00:23 +0200 Subject: [PATCH] fix: proxy start process --- app/Actions/Proxy/CheckProxy.php | 35 ++++++++++++++--------- app/Actions/Proxy/StartProxy.php | 2 -- app/Console/Kernel.php | 1 + app/Http/Livewire/Server/Proxy/Deploy.php | 2 +- app/Http/Livewire/Server/Proxy/Status.php | 2 +- app/Jobs/ContainerStatusJob.php | 15 ++++++++-- config/sentry.php | 2 +- config/version.php | 2 +- versions.json | 2 +- 9 files changed, 40 insertions(+), 23 deletions(-) diff --git a/app/Actions/Proxy/CheckProxy.php b/app/Actions/Proxy/CheckProxy.php index 84dc4e955..279fac20e 100644 --- a/app/Actions/Proxy/CheckProxy.php +++ b/app/Actions/Proxy/CheckProxy.php @@ -2,41 +2,50 @@ namespace App\Actions\Proxy; -use App\Enums\ProxyTypes; use App\Models\Server; -use Illuminate\Support\Str; use Lorisleiva\Actions\Concerns\AsAction; -use Spatie\Activitylog\Models\Activity; class CheckProxy { use AsAction; - public function handle(Server $server) + public function handle(Server $server, $fromUI = false) { if (!$server->isProxyShouldRun()) { - throw new \Exception("Proxy should not run"); + if ($fromUI) { + throw new \Exception("Proxy should not run. You selected the Custom Proxy."); + } else { + return false; + } } $status = getContainerStatus($server, 'coolify-proxy'); if ($status === 'running') { $server->proxy->set('status', 'running'); $server->save(); - return 'OK'; + return false; } $ip = $server->ip; if ($server->id === 0) { $ip = 'host.docker.internal'; } - $connection = @fsockopen($ip, '80'); - $connection = @fsockopen($ip, '443'); - $port80 = is_resource($connection) && fclose($connection); - $port443 = is_resource($connection) && fclose($connection); - ray($ip); + $connection80 = @fsockopen($ip, '80'); + $connection443 = @fsockopen($ip, '443'); + $port80 = is_resource($connection80) && fclose($connection80); + $port443 = is_resource($connection443) && fclose($connection443); if ($port80) { - throw new \Exception("Port 80 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); + if ($fromUI) { + throw new \Exception("Port 80 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); + } else { + return false; + } } if ($port443) { - throw new \Exception("Port 443 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord>"); + if ($fromUI) { + throw new \Exception("Port 443 is in use.
You must stop the process using this port.
Docs: https://coolify.io/docs
Discord: https://coollabs.io/discord"); + } else { + return false; + } } + return true; } } diff --git a/app/Actions/Proxy/StartProxy.php b/app/Actions/Proxy/StartProxy.php index 14b7fefb5..6a6738177 100644 --- a/app/Actions/Proxy/StartProxy.php +++ b/app/Actions/Proxy/StartProxy.php @@ -13,8 +13,6 @@ class StartProxy public function handle(Server $server, bool $async = true): string|Activity { try { - CheckProxy::run($server); - $proxyType = $server->proxyType(); $commands = collect([]); $proxy_path = get_proxy_path(); diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 52bf06ed9..730cf4113 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel // $this->check_scheduled_backups($schedule); $this->check_resources($schedule); $this->cleanup_servers($schedule); + $this->check_scheduled_backups($schedule); } else { $schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer(); diff --git a/app/Http/Livewire/Server/Proxy/Deploy.php b/app/Http/Livewire/Server/Proxy/Deploy.php index 39995e077..7e828b092 100644 --- a/app/Http/Livewire/Server/Proxy/Deploy.php +++ b/app/Http/Livewire/Server/Proxy/Deploy.php @@ -39,7 +39,7 @@ class Deploy extends Component public function checkProxy() { try { - CheckProxy::run($this->server); + CheckProxy::run($this->server, true); $this->emit('startProxyPolling'); $this->emit('proxyChecked'); } catch (\Throwable $e) { diff --git a/app/Http/Livewire/Server/Proxy/Status.php b/app/Http/Livewire/Server/Proxy/Status.php index 90deb1455..8df8f10cd 100644 --- a/app/Http/Livewire/Server/Proxy/Status.php +++ b/app/Http/Livewire/Server/Proxy/Status.php @@ -34,7 +34,7 @@ class Status extends Component } $this->numberOfPolls++; } - CheckProxy::run($this->server); + CheckProxy::run($this->server, true); $this->emit('proxyStatusUpdated'); if ($this->server->proxy->status === 'running') { $this->polling = false; diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index 4f379ec65..1c8cf1919 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Actions\Proxy\CheckProxy; use App\Actions\Proxy\StartProxy; use App\Models\ApplicationPreview; use App\Models\Server; @@ -117,10 +118,18 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted return data_get($value, 'Name') === '/coolify-proxy'; })->first(); if (!$foundProxyContainer) { - if ($this->server->isProxyShouldRun()) { - StartProxy::run($this->server, false); - $this->server->team->notify(new ContainerRestarted('coolify-proxy', $this->server)); + try { + $shouldStart = CheckProxy::run($this->server); + if ($shouldStart) { + StartProxy::run($this->server, false); + $this->server->team->notify(new ContainerRestarted('coolify-proxy', $this->server)); + } else { + ray('Proxy could not be started.'); + } + } catch (\Throwable $e) { + ray($e); } + } else { $this->server->proxy->status = data_get($foundProxyContainer, 'State.Status'); $this->server->save(); diff --git a/config/sentry.php b/config/sentry.php index f6f0a29fd..4a096757f 100644 --- a/config/sentry.php +++ b/config/sentry.php @@ -7,7 +7,7 @@ return [ // The release version of your application // Example with dynamic git hash: trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')) - 'release' => '4.0.0-beta.91', + 'release' => '4.0.0-beta.92', // When left empty or `null` the Laravel environment will be used 'environment' => config('app.env'), diff --git a/config/version.php b/config/version.php index 286da2a79..d889b7a6c 100644 --- a/config/version.php +++ b/config/version.php @@ -1,3 +1,3 @@