diff --git a/app/Actions/Database/StopDatabaseProxy.php b/app/Actions/Database/StopDatabaseProxy.php index a753153eb..96a109766 100644 --- a/app/Actions/Database/StopDatabaseProxy.php +++ b/app/Actions/Database/StopDatabaseProxy.php @@ -25,7 +25,6 @@ public function handle(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|St $server = data_get($database, 'destination.server'); $uuid = $database->uuid; if ($database->getMorphClass() === \App\Models\ServiceDatabase::class) { - $uuid = $database->service->uuid; $server = data_get($database, 'service.server'); } instant_remote_process(["docker rm -f {$uuid}-proxy"], $server); diff --git a/app/Actions/Docker/GetContainersStatus.php b/app/Actions/Docker/GetContainersStatus.php index c8e3162c0..6c9a54f77 100644 --- a/app/Actions/Docker/GetContainersStatus.php +++ b/app/Actions/Docker/GetContainersStatus.php @@ -3,6 +3,7 @@ namespace App\Actions\Docker; use App\Actions\Database\StartDatabaseProxy; +use App\Actions\Database\StopDatabaseProxy; use App\Actions\Shared\ComplexStatusCheck; use App\Events\ServiceChecked; use App\Models\ApplicationPreview; @@ -180,21 +181,30 @@ public function handle(Server $server, ?Collection $containers = null, ?Collecti if ($database_id) { $service_db = ServiceDatabase::where('id', $database_id)->first(); if ($service_db) { - $uuid = data_get($service_db, 'service.uuid'); - if ($uuid) { - $isPublic = data_get($service_db, 'is_public'); - if ($isPublic) { - $foundTcpProxy = $this->containers->filter(function ($value, $key) use ($uuid) { - if ($this->server->isSwarm()) { - return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; - } else { - return data_get($value, 'Name') === "/$uuid-proxy"; - } - })->first(); - if (! $foundTcpProxy) { - StartDatabaseProxy::run($service_db); - // $this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$service_db->service->name}", $this->server)); + $proxyUuid = $service_db->uuid; + $isPublic = data_get($service_db, 'is_public'); + if ($isPublic) { + $foundTcpProxy = $this->containers->filter(function ($value, $key) use ($proxyUuid) { + if ($this->server->isSwarm()) { + return data_get($value, 'Spec.Name') === "coolify-proxy_$proxyUuid"; + } else { + return data_get($value, 'Name') === "/$proxyUuid-proxy"; } + })->first(); + if (! $foundTcpProxy) { + StartDatabaseProxy::run($service_db); + } + } else { + // Clean up orphaned proxy when is_public=false + $orphanedProxy = $this->containers->filter(function ($value, $key) use ($proxyUuid) { + if ($this->server->isSwarm()) { + return data_get($value, 'Spec.Name') === "coolify-proxy_$proxyUuid"; + } else { + return data_get($value, 'Name') === "/$proxyUuid-proxy"; + } + })->first(); + if ($orphanedProxy) { + StopDatabaseProxy::run($service_db); } } } @@ -235,7 +245,18 @@ public function handle(Server $server, ?Collection $containers = null, ?Collecti })->first(); if (! $foundTcpProxy) { StartDatabaseProxy::run($database); - // $this->server->team?->notify(new ContainerRestarted("TCP Proxy for database", $this->server)); + } + } else { + // Clean up orphaned proxy when is_public=false + $orphanedProxy = $this->containers->filter(function ($value, $key) use ($uuid) { + if ($this->server->isSwarm()) { + return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; + } else { + return data_get($value, 'Name') === "/$uuid-proxy"; + } + })->first(); + if ($orphanedProxy) { + StopDatabaseProxy::run($database); } } } else { @@ -393,6 +414,11 @@ public function handle(Server $server, ?Collection $containers = null, ?Collecti 'last_restart_type' => null, ]); + // Stop proxy if database was public + if ($database->is_public) { + StopDatabaseProxy::run($database); + } + $name = data_get($database, 'name'); $fqdn = data_get($database, 'fqdn'); diff --git a/app/Jobs/PushServerUpdateJob.php b/app/Jobs/PushServerUpdateJob.php index e5a6e0c99..c02a7e3c5 100644 --- a/app/Jobs/PushServerUpdateJob.php +++ b/app/Jobs/PushServerUpdateJob.php @@ -496,7 +496,14 @@ private function updateDatabaseStatus(string $databaseUuid, string $containerSta if (! $tcpProxyContainerFound) { StartDatabaseProxy::dispatch($database); $this->server->team?->notify(new ContainerRestarted("TCP Proxy for {$database->name}", $this->server)); - } else { + } + } elseif ($this->isRunning($containerStatus) && ! $tcpProxy) { + // Clean up orphaned proxy containers when is_public=false + $orphanedProxy = $this->containers->filter(function ($value, $key) use ($databaseUuid) { + return data_get($value, 'name') === "$databaseUuid-proxy" && data_get($value, 'state') === 'running'; + })->first(); + if ($orphanedProxy) { + StopDatabaseProxy::dispatch($database); } } }