2023-03-30 13:52:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\Server;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2023-05-23 07:53:24 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
2023-03-30 13:52:19 +00: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 07:53:24 +00:00
|
|
|
class ContainerStatusJob implements ShouldQueue, ShouldBeUnique
|
2023-03-30 13:52:19 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2023-05-24 12:26:50 +00:00
|
|
|
private Application $application;
|
2023-04-14 09:10:21 +00:00
|
|
|
public function __construct(
|
2023-05-24 12:26:50 +00:00
|
|
|
public string|null $application_id = null,
|
2023-04-14 09:10:21 +00:00
|
|
|
) {
|
2023-05-24 12:26:50 +00:00
|
|
|
if ($this->application_id) {
|
|
|
|
$this->application = Application::find($this->application_id)->first();
|
|
|
|
}
|
2023-03-30 13:52:19 +00:00
|
|
|
}
|
2023-05-23 07:53:24 +00:00
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
2023-05-24 12:26:50 +00:00
|
|
|
return $this->application_id;
|
2023-05-23 07:53:24 +00:00
|
|
|
}
|
2023-04-14 11:18:55 +00:00
|
|
|
public function handle(): void
|
|
|
|
{
|
2023-04-28 08:38:22 +00:00
|
|
|
try {
|
2023-05-24 12:26:50 +00:00
|
|
|
if ($this->application->uuid) {
|
|
|
|
$this->check_container_status();
|
2023-04-28 08:38:22 +00:00
|
|
|
} else {
|
2023-05-24 12:26:50 +00:00
|
|
|
$this->check_all_servers();
|
2023-04-28 08:38:22 +00:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e->getMessage());
|
2023-04-14 11:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 12:26:50 +00:00
|
|
|
protected function check_all_servers()
|
2023-03-30 13:52:19 +00:00
|
|
|
{
|
2023-04-28 08:38:22 +00:00
|
|
|
$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) {
|
2023-05-03 07:38:53 +00:00
|
|
|
$output = instantRemoteProcess(['docker ps -a -q --format \'{{json .}}\''], $server);
|
2023-05-24 12:26:50 +00:00
|
|
|
$containers = $containers->concat(format_docker_command_output_to_json($output));
|
2023-04-28 08:38:22 +00:00
|
|
|
}
|
|
|
|
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);
|
2023-03-30 13:52:19 +00:00
|
|
|
}
|
2023-04-28 08:38:22 +00:00
|
|
|
}
|
|
|
|
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 13:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 12:26:50 +00:00
|
|
|
protected function check_container_status()
|
2023-04-14 09:10:21 +00:00
|
|
|
{
|
2023-05-24 12:26:50 +00: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 09:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 13:52:19 +00:00
|
|
|
}
|