lasthourcloud/app/Actions/Shared/ComplexStatusCheck.php

61 lines
2.5 KiB
PHP
Raw Normal View History

2024-02-07 13:55:06 +00:00
<?php
namespace App\Actions\Shared;
use App\Models\Application;
use Lorisleiva\Actions\Concerns\AsAction;
class ComplexStatusCheck
{
use AsAction;
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
public function handle(Application $application)
{
$servers = $application->additional_servers;
$servers->push($application->destination->server);
foreach ($servers as $server) {
$is_main_server = $application->destination->server->id === $server->id;
2024-06-10 20:43:34 +00:00
if (! $server->isFunctional()) {
2024-02-07 13:55:06 +00:00
if ($is_main_server) {
$application->update(['status' => 'exited:unhealthy']);
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
continue;
} else {
$application->additional_servers()->updateExistingPivot($server->id, ['status' => 'exited:unhealthy']);
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
continue;
}
}
$container = instant_remote_process(["docker container inspect $(docker container ls -q --filter 'label=coolify.applicationId={$application->id}' --filter 'label=coolify.pullRequestId=0') --format '{{json .}}'"], $server, false);
$container = format_docker_command_output_to_json($container);
if ($container->count() === 1) {
$container = $container->first();
$containerStatus = data_get($container, 'State.Status');
$containerHealth = data_get($container, 'State.Health.Status', 'unhealthy');
if ($is_main_server) {
$statusFromDb = $application->status;
if ($statusFromDb !== $containerStatus) {
$application->update(['status' => "$containerStatus:$containerHealth"]);
}
} else {
$additional_server = $application->additional_servers()->wherePivot('server_id', $server->id);
$statusFromDb = $additional_server->first()->pivot->status;
if ($statusFromDb !== $containerStatus) {
$additional_server->updateExistingPivot($server->id, ['status' => "$containerStatus:$containerHealth"]);
}
}
} else {
if ($is_main_server) {
$application->update(['status' => 'exited:unhealthy']);
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
continue;
} else {
$application->additional_servers()->updateExistingPivot($server->id, ['status' => 'exited:unhealthy']);
2024-06-10 20:43:34 +00:00
2024-02-07 13:55:06 +00:00
continue;
}
}
}
}
}