fix: docker cleanup job

This commit is contained in:
Andras Bacsai
2024-08-21 10:50:05 +02:00
parent 8dc26a18d8
commit 3f9f197282
5 changed files with 24 additions and 28 deletions

View File

@@ -9,17 +9,11 @@ class CleanupDocker
{ {
use AsAction; use AsAction;
public function handle(Server $server, bool $force = true) public function handle(Server $server)
{ {
// cleanup docker images, containers, and builder caches // cleanup docker images, containers, and builder caches
if ($force) { instant_remote_process(['docker image prune -af'], $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 container prune -f --filter "label=coolify.managed=true"'], $server, false); instant_remote_process(['docker builder prune -af'], $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);
}
} }
} }

View File

@@ -26,7 +26,7 @@ class UpdateCoolify
if (! $this->server) { if (! $this->server) {
return; 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'); $response = Http::retry(3, 1000)->get('https://cdn.coollabs.io/coolify/versions.json');
if ($response->successful()) { if ($response->successful()) {
$versions = $response->json(); $versions = $response->json();

View File

@@ -19,32 +19,33 @@ class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueue
public $timeout = 300; public $timeout = 300;
public int|string|null $usageBefore = null; public $tries = 2;
public ?string $usageBefore = null;
public function __construct(public Server $server) {} public function __construct(public Server $server) {}
public function handle(): void 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 { 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(); $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); Log::info('DockerCleanupJob force cleanup on '.$this->server->name);
CleanupDocker::run(server: $this->server, force: true); CleanupDocker::run(server: $this->server);
return; return;
} }
if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) { 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(); $usageAfter = $this->server->getDiskUsage();
if ($usageAfter < $this->usageBefore) { if ($usageAfter < $this->usageBefore) {
$this->server->team?->notify(new DockerCleanup($this->server, 'Saved '.($this->usageBefore - $usageAfter).'% disk space.')); $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); Log::info('No need to clean up '.$this->server->name);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
ray($e->getMessage()); CleanupDocker::run(server: $this->server);
Log::error('DockerCleanupJob failed: '.$e->getMessage());
throw $e; throw $e;
} }
} }

View File

@@ -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); return instant_remote_process(["df /| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this, false);
} }

View File

@@ -170,7 +170,7 @@ function generateSshCommand(Server $server, string $command)
// ray($ssh_command); // ray($ssh_command);
return $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'); $timeout = config('constants.ssh.command_timeout');
if ($command instanceof Collection) { if ($command instanceof Collection) {