lasthourcloud/app/Jobs/ProxyContainerStatusJob.php

66 lines
2.2 KiB
PHP
Raw Normal View History

2023-06-02 13:15:12 +00:00
<?php
namespace App\Jobs;
use App\Enums\ProxyTypes;
use App\Models\Server;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
2023-06-20 18:19:31 +00:00
use Illuminate\Support\Str;
2023-06-02 13:15:12 +00:00
class ProxyContainerStatusJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Server $server;
public $tries = 1;
public $timeout = 120;
2023-06-02 13:15:12 +00:00
public function __construct(Server $server)
{
$this->server = $server;
}
public function middleware(): array
{
return [new WithoutOverlapping($this->server->id)];
}
2023-06-02 13:15:12 +00:00
public function uniqueId(): int
{
return $this->server->id;
}
2023-06-02 13:15:12 +00:00
public function handle(): void
{
try {
2023-08-28 18:45:53 +00:00
$container = getContainerStatus(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: false);
2023-08-24 19:20:24 +00:00
$status = data_get($container, 'State.Status');
2023-08-28 18:45:53 +00:00
if ($status && data_get($this->server, 'proxy.status') !== $status) {
2023-06-20 18:19:31 +00:00
$this->server->proxy->status = $status;
if ($this->server->proxy->status === 'running') {
2023-06-02 13:15:12 +00:00
$traefik = $container['Config']['Labels']['org.opencontainers.image.title'];
$version = $container['Config']['Labels']['org.opencontainers.image.version'];
if (isset($version) && isset($traefik) && $traefik === 'Traefik' && Str::of($version)->startsWith('v2')) {
2023-06-20 18:19:31 +00:00
$this->server->proxy->type = ProxyTypes::TRAEFIK_V2->value;
2023-06-02 13:15:12 +00:00
}
}
$this->server->save();
}
} catch (\Exception $e) {
if ($e->getCode() === 1) {
$this->server->proxy->status = 'exited';
$this->server->save();
}
2023-08-24 14:14:09 +00:00
send_internal_notification('ProxyContainerStatusJob failed with: ' . $e->getMessage());
2023-08-24 19:09:58 +00:00
ray($e->getMessage());
throw $e;
2023-06-02 13:15:12 +00:00
}
}
}