From 91e3d33c0b3303e169be0997e2c57a3b82ef54ec Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 16 Nov 2023 20:48:25 +0100 Subject: [PATCH] Add cleanup of stucked helper containers on servers --- app/Console/Commands/Init.php | 12 ++++++++ app/Jobs/CleanupHelperContainersJob.php | 40 +++++++++++++++++++++++++ bootstrap/helpers/docker.php | 2 -- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 app/Jobs/CleanupHelperContainersJob.php diff --git a/app/Console/Commands/Init.php b/app/Console/Commands/Init.php index e0081d0d5..1bb4aa055 100644 --- a/app/Console/Commands/Init.php +++ b/app/Console/Commands/Init.php @@ -3,9 +3,11 @@ namespace App\Console\Commands; use App\Enums\ApplicationDeploymentStatus; +use App\Jobs\CleanupHelperContainersJob; use App\Models\Application; use App\Models\ApplicationDeploymentQueue; use App\Models\InstanceSettings; +use App\Models\Server; use App\Models\Service; use App\Models\ServiceApplication; use App\Models\ServiceDatabase; @@ -32,6 +34,16 @@ public function handle() $this->cleanup_ssh(); } $this->cleanup_in_progress_application_deployments(); + $this->cleanup_stucked_helper_containers(); + } + private function cleanup_stucked_helper_containers() { + $servers = Server::all(); + foreach ($servers as $server) { + if ($server->isFunctional()) { + CleanupHelperContainersJob::dispatch($server); + } + } + } private function alive() { diff --git a/app/Jobs/CleanupHelperContainersJob.php b/app/Jobs/CleanupHelperContainersJob.php new file mode 100644 index 000000000..5c26ca930 --- /dev/null +++ b/app/Jobs/CleanupHelperContainersJob.php @@ -0,0 +1,40 @@ +server->name); + $containers = instant_remote_process(['docker container ps --filter "ancestor=ghcr.io/coollabsio/coolify-helper:next" --filter "ancestor=ghcr.io/coollabsio/coolify-helper:latest" --format \'{{json .}}\''], $this->server, false); + $containers = format_docker_command_output_to_json($containers); + if ($containers->count() > 0) { + foreach ($containers as $container) { + $containerId = data_get($container,'ID'); + ray('Removing container ' . $containerId); + instant_remote_process(['docker container rm -f ' . $containerId], $this->server, false); + } + } + } catch (\Throwable $e) { + send_internal_notification('CleanupHelperContainersJob failed with error: ' . $e->getMessage()); + ray($e->getMessage()); + } + } +} diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 42ea0f9bc..36f8733b3 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -10,7 +10,6 @@ function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null): Collection { - ray($id, $pullRequestId); $containers = collect([]); $containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server); $containers = format_docker_command_output_to_json($containers); @@ -26,7 +25,6 @@ function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pul return null; }); $containers = $containers->filter(); - ray($containers); return $containers; }