From 23a94c9378968100564502f4493ec5c3d3bb87e8 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 10 Nov 2023 10:34:28 +0100 Subject: [PATCH] Refactor DockerCleanupJob and Application model --- app/Jobs/DockerCleanupJob.php | 28 +++++++++++++++------------- app/Models/Application.php | 8 ++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.php index 0813c1218..49e872cb7 100644 --- a/app/Jobs/DockerCleanupJob.php +++ b/app/Jobs/DockerCleanupJob.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Models\Server; +use Exception; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; @@ -15,7 +16,7 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $timeout = 1000; + public $timeout = 300; public ?string $dockerRootFilesystem = null; public ?int $usageBefore = null; @@ -33,14 +34,15 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted } public function handle(): void { - $queuedCount = 0; - $this->server->applications()->each(function ($application) use ($queuedCount) { - $count = data_get($application->deployments(), 'count', 0); - $queuedCount += $count; + $isInprogress = false; + $this->server->applications()->each(function ($application) use (&$isInprogress) { + if ($application->isDeploymentInprogress()) { + $isInprogress = true; + return; + } }); - if ($queuedCount > 0) { - ray('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping')->color('orange'); - return; + if ($isInprogress) { + throw new Exception('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping...'); } try { if (!$this->server->isFunctional()) { @@ -49,23 +51,23 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted $this->dockerRootFilesystem = "/"; $this->usageBefore = $this->getFilesystemUsage(); if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) { - ray('Cleaning up ' . $this->server->name)->color('orange'); + ray('Cleaning up ' . $this->server->name); instant_remote_process(['docker image prune -af'], $this->server); instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $this->server); instant_remote_process(['docker builder prune -af'], $this->server); $usageAfter = $this->getFilesystemUsage(); if ($usageAfter < $this->usageBefore) { - ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name)->color('orange'); + ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name); send_internal_notification('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name); } else { - ray('DockerCleanupJob failed to save disk space on ' . $this->server->name)->color('orange'); + ray('DockerCleanupJob failed to save disk space on ' . $this->server->name); } } else { - ray('No need to clean up ' . $this->server->name)->color('orange'); + ray('No need to clean up ' . $this->server->name); } } catch (\Throwable $e) { send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage()); - ray($e->getMessage())->color('orange'); + ray($e->getMessage()); throw $e; } } diff --git a/app/Models/Application.php b/app/Models/Application.php index ca7334b7e..0e72f94ce 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -213,6 +213,14 @@ class Application extends BaseModel return $this->morphTo(); } + public function isDeploymentInprogress() { + $deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->where('status', 'in_progress')->count(); + if ($deployments > 0) { + return true; + } + return false; + } + public function deployments(int $skip = 0, int $take = 10) { $deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc');