From cd0da04ea29f5982aad6c174a032e8841eb8c886 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 12 Sep 2023 15:47:30 +0200 Subject: [PATCH] wip: server check instead of app check --- app/Console/Kernel.php | 20 +++++--- app/Jobs/ServerDetailsCheckJob.php | 79 ++++++++++++++++++++++++++++++ bootstrap/helpers/docker.php | 5 +- 3 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 app/Jobs/ServerDetailsCheckJob.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 85fa80aac..ae40fec49 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -11,6 +11,7 @@ use App\Jobs\DatabaseContainerStatusJob; use App\Jobs\DockerCleanupJob; use App\Jobs\InstanceAutoUpdateJob; use App\Jobs\ProxyContainerStatusJob; +use App\Jobs\ServerDetailsCheckJob; use App\Models\Application; use App\Models\InstanceSettings; use App\Models\ScheduledDatabaseBackup; @@ -24,20 +25,25 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule): void { if (isDev()) { - $schedule->command('horizon:snapshot')->everyMinute(); - $schedule->job(new CleanupInstanceStuffsJob)->everyMinute(); + $schedule->job(new ServerDetailsCheckJob(Server::find(0)))->everyTenMinutes()->onOneServer(); + // $schedule->command('horizon:snapshot')->everyMinute(); + // $schedule->job(new CleanupInstanceStuffsJob)->everyMinute(); // $schedule->job(new CheckResaleLicenseJob)->hourly(); - $schedule->job(new DockerCleanupJob)->everyOddHour(); + // $schedule->job(new DockerCleanupJob)->everyOddHour(); + // $this->instance_auto_update($schedule); + // $this->check_scheduled_backups($schedule); + // $this->check_resources($schedule); + // $this->check_proxies($schedule); } else { $schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->job(new CleanupInstanceStuffsJob)->everyTenMinutes()->onOneServer(); $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer(); $schedule->job(new DockerCleanupJob)->everyTenMinutes()->onOneServer(); + $this->instance_auto_update($schedule); + $this->check_scheduled_backups($schedule); + $this->check_resources($schedule); + $this->check_proxies($schedule); } - $this->instance_auto_update($schedule); - $this->check_scheduled_backups($schedule); - $this->check_resources($schedule); - $this->check_proxies($schedule); } private function check_proxies($schedule) { diff --git a/app/Jobs/ServerDetailsCheckJob.php b/app/Jobs/ServerDetailsCheckJob.php new file mode 100644 index 000000000..606931c42 --- /dev/null +++ b/app/Jobs/ServerDetailsCheckJob.php @@ -0,0 +1,79 @@ +server->uuid)]; + } + + public function uniqueId(): string + { + return $this->server->uuid; + } + + public function handle(): void + { + try { + ray()->clearAll(); + $containers = instant_remote_process(["docker container inspect $(docker container ls -q) --format '{{json .}}'"], $this->server); + $containers = format_docker_command_output_to_json($containers); + $applications = $this->server->applications(); + // ray($applications); + // ray(format_docker_command_output_to_json($containers)); + foreach ($applications as $application) { + $uuid = data_get($application, 'uuid'); + $foundContainer = $containers->filter(function ($value, $key) use ($uuid) { + $image = data_get($value, 'Config.Image'); + return Str::startsWith($image, $uuid); + })->first(); + + if ($foundContainer) { + $containerStatus = data_get($foundContainer, 'State.Status'); + $databaseStatus = data_get($application, 'status'); + ray($containerStatus, $databaseStatus); + if ($containerStatus !== $databaseStatus) { + // $application->update(['status' => $containerStatus]); + } + } + } + // foreach ($containers as $container) { + // $labels = format_docker_labels_to_json(data_get($container,'Config.Labels')); + // $foundLabel = $labels->filter(fn ($value, $key) => Str::startsWith($key, 'coolify.applicationId')); + // if ($foundLabel->count() > 0) { + // $appFound = $applications->where('id', $foundLabel['coolify.applicationId'])->first(); + // if ($appFound) { + // $containerStatus = data_get($container, 'State.Status'); + // $databaseStatus = data_get($appFound, 'status'); + // ray($containerStatus, $databaseStatus); + // } + // } + // } + } catch (\Throwable $e) { + // send_internal_notification('ServerDetailsCheckJob failed with: ' . $e->getMessage()); + ray($e->getMessage()); + throw $e; + } + } +} diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php index 080448f71..e3629e71c 100644 --- a/bootstrap/helpers/docker.php +++ b/bootstrap/helpers/docker.php @@ -26,8 +26,11 @@ function format_docker_command_output_to_json($rawOutput): Collection ->map(fn ($outputLine) => json_decode($outputLine, true, flags: JSON_THROW_ON_ERROR)); } -function format_docker_labels_to_json($rawOutput): Collection +function format_docker_labels_to_json(string|Array $rawOutput): Collection { + if (is_array($rawOutput)) { + return collect($rawOutput); + } $outputLines = explode(PHP_EOL, $rawOutput); return collect($outputLines)