Refactor GetContainersStatus.php for improved readability and maintainability

This commit is contained in:
Andras Bacsai 2024-05-08 15:19:12 +02:00
parent bc5d3bea14
commit 829e17ef2b

View File

@ -13,6 +13,7 @@
use App\Notifications\Container\ContainerRestarted; use App\Notifications\Container\ContainerRestarted;
use App\Notifications\Container\ContainerStopped; use App\Notifications\Container\ContainerStopped;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
class GetContainersStatus class GetContainersStatus
@ -119,7 +120,6 @@ private function sentinel()
} else { } else {
$uuid = data_get($labels, 'com.docker.compose.service'); $uuid = data_get($labels, 'com.docker.compose.service');
$type = data_get($labels, 'coolify.type'); $type = data_get($labels, 'coolify.type');
if ($uuid) { if ($uuid) {
if ($type === 'service') { if ($type === 'service') {
$database_id = data_get($labels, 'coolify.service.subId'); $database_id = data_get($labels, 'coolify.service.subId');
@ -131,9 +131,10 @@ private function sentinel()
if ($isPublic) { if ($isPublic) {
$foundTcpProxy = $containers->filter(function ($value, $key) use ($uuid) { $foundTcpProxy = $containers->filter(function ($value, $key) use ($uuid) {
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
// TODO: fix this with sentinel
return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid";
} else { } else {
return data_get($value, 'Name') === "/$uuid-proxy"; return data_get($value, 'name') === "$uuid-proxy";
} }
})->first(); })->first();
if (!$foundTcpProxy) { if (!$foundTcpProxy) {
@ -155,9 +156,10 @@ private function sentinel()
if ($isPublic) { if ($isPublic) {
$foundTcpProxy = $containers->filter(function ($value, $key) use ($uuid) { $foundTcpProxy = $containers->filter(function ($value, $key) use ($uuid) {
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
// TODO: fix this with sentinel
return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid";
} else { } else {
return data_get($value, 'Name') === "/$uuid-proxy"; return data_get($value, 'name') === "$uuid-proxy";
} }
})->first(); })->first();
if (!$foundTcpProxy) { if (!$foundTcpProxy) {
@ -170,7 +172,7 @@ private function sentinel()
} }
} }
} }
if (data_get($container, 'Name') === '/coolify-db') { if (data_get($container, 'name') === 'coolify-db') {
$foundDatabases[] = 0; $foundDatabases[] = 0;
} }
} }
@ -316,9 +318,10 @@ private function sentinel()
$this->server->proxyType(); $this->server->proxyType();
$foundProxyContainer = $containers->filter(function ($value, $key) { $foundProxyContainer = $containers->filter(function ($value, $key) {
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
// TODO: fix this with sentinel
return data_get($value, 'Spec.Name') === 'coolify-proxy_traefik'; return data_get($value, 'Spec.Name') === 'coolify-proxy_traefik';
} else { } else {
return data_get($value, 'Name') === '/coolify-proxy'; return data_get($value, 'name') === 'coolify-proxy';
} }
})->first(); })->first();
if (!$foundProxyContainer) { if (!$foundProxyContainer) {
@ -332,7 +335,7 @@ private function sentinel()
ray($e); ray($e);
} }
} else { } else {
$this->server->proxy->status = data_get($foundProxyContainer, 'State.Status'); $this->server->proxy->status = data_get($foundProxyContainer, 'state');
$this->server->save(); $this->server->save();
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server); $connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
instant_remote_process($connectProxyToDockerNetworks, $this->server, false); instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
@ -448,7 +451,7 @@ private function old_way()
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid";
} else { } else {
return data_get($value, 'Name') === "/$uuid-proxy"; return data_get($value, 'Name') === "$uuid-proxy";
} }
})->first(); })->first();
if (!$foundTcpProxy) { if (!$foundTcpProxy) {
@ -472,7 +475,7 @@ private function old_way()
if ($this->server->isSwarm()) { if ($this->server->isSwarm()) {
return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid"; return data_get($value, 'Spec.Name') === "coolify-proxy_$uuid";
} else { } else {
return data_get($value, 'Name') === "/$uuid-proxy"; return data_get($value, 'Name') === "$uuid-proxy";
} }
})->first(); })->first();
if (!$foundTcpProxy) { if (!$foundTcpProxy) {