lasthourcloud/app/Jobs/ContainerStatusJob.php

89 lines
3.7 KiB
PHP
Raw Normal View History

2023-03-30 15:52:19 +02:00
<?php
namespace App\Jobs;
use App\Models\Application;
2023-05-30 15:52:17 +02:00
use App\Models\ApplicationPreview;
2023-03-30 15:52:19 +02:00
use App\Models\Server;
use Illuminate\Bus\Queueable;
2023-05-23 09:53:24 +02:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
2023-03-30 15:52:19 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
2023-05-23 09:53:24 +02:00
class ContainerStatusJob implements ShouldQueue, ShouldBeUnique
2023-03-30 15:52:19 +02:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-05-30 15:52:17 +02:00
public string $container_name;
public string|null $pull_request_id;
public Application $application;
public function __construct($application, string $container_name, string|null $pull_request_id = null)
{
$this->application = $application;
$this->container_name = $container_name;
$this->pull_request_id = $pull_request_id;
2023-03-30 15:52:19 +02:00
}
2023-05-23 09:53:24 +02:00
public function uniqueId(): string
{
2023-05-30 15:52:17 +02:00
return $this->container_name;
2023-05-23 09:53:24 +02:00
}
2023-04-14 13:18:55 +02:00
public function handle(): void
{
2023-04-28 10:38:22 +02:00
try {
2023-05-30 15:52:17 +02:00
$status = get_container_status(server: $this->application->destination->server, container_id: $this->container_name, throwError: false);
2023-05-31 14:57:42 +02:00
ray('Container ' . $this->container_name . ' statuus is ' . $status);
2023-05-30 15:52:17 +02:00
if ($this->pull_request_id) {
$preview = ApplicationPreview::findPreviewByApplicationAndPullId($this->application->id, $this->pull_request_id);
$preview->status = $status;
$preview->save();
2023-04-28 10:38:22 +02:00
} else {
2023-05-30 15:52:17 +02:00
$this->application->status = $status;
$this->application->save();
2023-04-28 10:38:22 +02:00
}
} catch (\Exception $e) {
Log::error($e->getMessage());
2023-04-14 13:18:55 +02:00
}
}
2023-05-24 14:26:50 +02:00
protected function check_container_status()
2023-04-14 11:10:21 +02:00
{
2023-05-24 14:26:50 +02:00
if ($this->application->destination->server) {
$this->application->status = get_container_status(server: $this->application->destination->server, container_id: $this->application->uuid);
$this->application->save();
2023-04-14 11:10:21 +02:00
}
}
2023-05-30 15:52:17 +02:00
// protected function check_all_servers()
// {
// $servers = Server::all()->reject(fn (Server $server) => $server->settings->is_build_server);
// $applications = Application::all();
// $not_found_applications = $applications;
// $containers = collect();
// foreach ($servers as $server) {
// $output = instant_remote_process(['docker ps -a -q --format \'{{json .}}\''], $server);
// $containers = $containers->concat(format_docker_command_output_to_json($output));
// }
// foreach ($containers as $container) {
// $found_application = $applications->filter(function ($value, $key) use ($container) {
// return $value->uuid == $container['Names'];
// })->first();
// if ($found_application) {
// $not_found_applications = $not_found_applications->filter(function ($value, $key) use ($found_application) {
// return $value->uuid != $found_application->uuid;
// });
// $found_application->status = $container['State'];
// $found_application->save();
// Log::info('Found application: ' . $found_application->uuid . '. Set status to: ' . $found_application->status);
// }
// }
// foreach ($not_found_applications as $not_found_application) {
// $not_found_application->status = 'exited';
// $not_found_application->save();
// Log::info('Not found application: ' . $not_found_application->uuid . '. Set status to: ' . $not_found_application->status);
// }
// }
2023-03-30 15:52:19 +02:00
}