diff --git a/app/Actions/Server/CleanupDocker.php b/app/Actions/Server/CleanupDocker.php index 0009e001d..0decaf0bc 100644 --- a/app/Actions/Server/CleanupDocker.php +++ b/app/Actions/Server/CleanupDocker.php @@ -9,17 +9,11 @@ class CleanupDocker { use AsAction; - public function handle(Server $server, bool $force = true) + public function handle(Server $server) { // cleanup docker images, containers, and builder caches - if ($force) { - instant_remote_process(['docker image prune -af'], $server, false); - instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $server, false); - instant_remote_process(['docker builder prune -af'], $server, false); - } else { - instant_remote_process(['docker image prune -f'], $server, false); - instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $server, false); - instant_remote_process(['docker builder prune -f'], $server, false); - } + instant_remote_process(['docker image prune -af'], $server, false); + instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $server, false); + instant_remote_process(['docker builder prune -af'], $server, false); } } diff --git a/app/Actions/Server/UpdateCoolify.php b/app/Actions/Server/UpdateCoolify.php index 8910d6e97..09d6471e8 100644 --- a/app/Actions/Server/UpdateCoolify.php +++ b/app/Actions/Server/UpdateCoolify.php @@ -26,7 +26,7 @@ class UpdateCoolify if (! $this->server) { return; } - CleanupDocker::dispatch($this->server, false)->onQueue('high'); + CleanupDocker::dispatch($this->server)->onQueue('high'); $response = Http::retry(3, 1000)->get('https://cdn.coollabs.io/coolify/versions.json'); if ($response->successful()) { $versions = $response->json(); diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.php index 5010263ae..163b579eb 100644 --- a/app/Jobs/DockerCleanupJob.php +++ b/app/Jobs/DockerCleanupJob.php @@ -19,32 +19,33 @@ class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueue public $timeout = 300; - public int|string|null $usageBefore = null; + public $tries = 2; + + public ?string $usageBefore = null; public function __construct(public Server $server) {} public function handle(): void { + if (! $this->server->isFunctional()) { + return; + } + if ($this->server->settings->is_force_cleanup_enabled) { + Log::info('DockerCleanupJob force cleanup on '.$this->server->name); + CleanupDocker::run(server: $this->server); + + return; + } try { - if (! $this->server->isFunctional()) { - return; - } - if ($this->server->settings->is_force_cleanup_enabled) { - Log::info('DockerCleanupJob force cleanup on '.$this->server->name); - CleanupDocker::run(server: $this->server, force: true); - - return; - } - $this->usageBefore = $this->server->getDiskUsage(); - if (str($this->usageBefore)->isEmpty() || $this->usageBefore === null || $this->usageBefore === 0) { + if (str($this->usageBefore)->trim()->isEmpty()) { Log::info('DockerCleanupJob force cleanup on '.$this->server->name); - CleanupDocker::run(server: $this->server, force: true); + CleanupDocker::run(server: $this->server); return; } if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) { - CleanupDocker::run(server: $this->server, force: false); + CleanupDocker::run(server: $this->server); $usageAfter = $this->server->getDiskUsage(); if ($usageAfter < $this->usageBefore) { $this->server->team?->notify(new DockerCleanup($this->server, 'Saved '.($this->usageBefore - $usageAfter).'% disk space.')); @@ -56,7 +57,8 @@ class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueue Log::info('No need to clean up '.$this->server->name); } } catch (\Throwable $e) { - ray($e->getMessage()); + CleanupDocker::run(server: $this->server); + Log::error('DockerCleanupJob failed: '.$e->getMessage()); throw $e; } } diff --git a/app/Models/Server.php b/app/Models/Server.php index 8a7325beb..3a1c0eabe 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -649,7 +649,7 @@ $schema://$host { } } - public function getDiskUsage() + public function getDiskUsage(): ?string { return instant_remote_process(["df /| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this, false); } diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index 918aa74cc..6ba7caeef 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -170,7 +170,7 @@ function generateSshCommand(Server $server, string $command) // ray($ssh_command); return $ssh_command; } -function instant_remote_process(Collection|array $command, Server $server, bool $throwError = true, bool $no_sudo = false) +function instant_remote_process(Collection|array $command, Server $server, bool $throwError = true, bool $no_sudo = false): ?string { $timeout = config('constants.ssh.command_timeout'); if ($command instanceof Collection) {