From 9b26a2a859c00c3947a0f85f2ddfc4cbcc46806d Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 14 Apr 2023 11:10:21 +0200 Subject: [PATCH] Able to check one container only. --- app/Jobs/ContainerStatusJob.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php index b3432d3ec..d78e49bf9 100644 --- a/app/Jobs/ContainerStatusJob.php +++ b/app/Jobs/ContainerStatusJob.php @@ -20,11 +20,11 @@ class ContainerStatusJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public function __construct() - { - // + public function __construct( + public string|null $container_id = null, + ) { } - public function handle(): void + protected function checkAllServers() { try { $servers = Server::all()->reject(fn (Server $server) => $server->settings->is_build_server); @@ -57,4 +57,29 @@ class ContainerStatusJob implements ShouldQueue Log::error($e->getMessage()); } } + protected function checkContainerStatus() + { + try { + $application = Application::where('uuid', $this->container_id)->firstOrFail(); + if (!$application) { + return; + } + if ($application->destination->server) { + $container = runRemoteCommandSync($application->destination->server, ["docker inspect --format '{{json .State}}' {$this->container_id}"]); + $container = formatDockerCmdOutputToJson($container); + $application->status = $container[0]['Status']; + $application->save(); + } + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + } + public function handle(): void + { + if ($this->container_id) { + $this->checkContainerStatus(); + } else { + $this->checkAllServers(); + } + } }