From 82a5b4c55df1ee9dbd0b24096a01e4cdc1907821 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 18 Jul 2024 14:43:21 +0200 Subject: [PATCH] refactor: server status job and docker cleanup job --- app/Console/Kernel.php | 2 ++ app/Jobs/DockerCleanupJob.php | 11 ++---- app/Jobs/ServerStatusJob.php | 65 ----------------------------------- 3 files changed, 5 insertions(+), 73 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index f529f63b9..c6ee39524 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -6,6 +6,7 @@ use App\Jobs\CleanupInstanceStuffsJob; use App\Jobs\ContainerStatusJob; use App\Jobs\DatabaseBackupJob; +use App\Jobs\DockerCleanupJob; use App\Jobs\PullCoolifyImageJob; use App\Jobs\PullHelperImageJob; use App\Jobs\PullSentinelImageJob; @@ -87,6 +88,7 @@ private function check_resources($schedule) } foreach ($servers as $server) { $schedule->job(new ServerStatusJob($server))->everyMinute()->onOneServer(); + $schedule->job(new DockerCleanupJob($server))->everyTenMinutes()->onOneServer(); } } diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.php index c8f1bd049..edc798bc7 100644 --- a/app/Jobs/DockerCleanupJob.php +++ b/app/Jobs/DockerCleanupJob.php @@ -41,30 +41,25 @@ public function handle(): void return; } if ($this->server->is_force_cleanup_enabled) { - CleanupDocker::run($this->server); + CleanupDocker::run(server: $this->server, force: true); return; } + $this->usageBefore = $this->server->getDiskUsage(); - ray('Usage before: '.$this->usageBefore); if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) { - ray('Cleaning up '.$this->server->name); - CleanupDocker::run($this->server); + CleanupDocker::run(server: $this->server, force: false); $usageAfter = $this->server->getDiskUsage(); if ($usageAfter < $this->usageBefore) { $this->server->team?->notify(new DockerCleanup($this->server, 'Saved '.($this->usageBefore - $usageAfter).'% disk space.')); - // 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); Log::info('DockerCleanupJob done: Saved '.($this->usageBefore - $usageAfter).'% disk space on '.$this->server->name); } else { Log::info('DockerCleanupJob failed to save disk space on '.$this->server->name); } } else { - ray('No need to clean up '.$this->server->name); Log::info('No need to clean up '.$this->server->name); } } catch (\Throwable $e) { - // send_internal_notification('DockerCleanupJob failed with: '.$e->getMessage()); ray($e->getMessage()); throw $e; } diff --git a/app/Jobs/ServerStatusJob.php b/app/Jobs/ServerStatusJob.php index 64730c6fa..ac9182eca 100644 --- a/app/Jobs/ServerStatusJob.php +++ b/app/Jobs/ServerStatusJob.php @@ -3,7 +3,6 @@ namespace App\Jobs; use App\Models\Server; -use App\Notifications\Server\HighDiskUsage; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; @@ -44,7 +43,6 @@ public function handle() } try { if ($this->server->isFunctional()) { - $this->cleanup(); $this->remove_unnecessary_coolify_yaml(); if ($this->server->isSentinelEnabled()) { $this->server->checkSentinel(); @@ -56,45 +54,7 @@ public function handle() return handleError($e); } - try { - // $this->check_docker_engine(); - } catch (\Throwable $e) { - // Do nothing - } - } - private function check_docker_engine() - { - $version = instant_remote_process([ - 'docker info', - ], $this->server, false); - if (is_null($version)) { - $os = instant_remote_process([ - 'cat /etc/os-release | grep ^ID=', - ], $this->server, false); - $os = str($os)->after('ID=')->trim(); - if ($os === 'ubuntu') { - try { - instant_remote_process([ - 'systemctl start docker', - ], $this->server); - } catch (\Throwable $e) { - ray($e->getMessage()); - - return handleError($e); - } - } else { - try { - instant_remote_process([ - 'service docker start', - ], $this->server); - } catch (\Throwable $e) { - ray($e->getMessage()); - - return handleError($e); - } - } - } } private function remove_unnecessary_coolify_yaml() @@ -108,29 +68,4 @@ private function remove_unnecessary_coolify_yaml() ], $this->server, false); } } - - public function cleanup(): void - { - if ($this->server->settings->is_force_cleanup_enabled) { - DockerCleanupJob::dispatch($this->server); - - return; - } - $this->disk_usage = $this->server->getDiskUsage(); - if ($this->disk_usage >= $this->server->settings->cleanup_after_percentage) { - DockerCleanupJob::dispatch($this->server); - if ($this->server->high_disk_usage_notification_sent) { - ray('high disk usage notification already sent'); - - return; - } else { - $this->server->high_disk_usage_notification_sent = true; - $this->server->save(); - $this->server->team?->notify(new HighDiskUsage($this->server, $this->disk_usage, $this->server->settings->cleanup_after_percentage)); - } - } else { - $this->server->high_disk_usage_notification_sent = false; - $this->server->save(); - } - } }