lasthourcloud/app/Jobs/ProxyContainerStatusJob.php

88 lines
3.0 KiB
PHP
Raw Normal View History

2023-06-02 13:15:12 +00:00
<?php
namespace App\Jobs;
2023-09-11 20:29:34 +00:00
use App\Actions\Proxy\StartProxy;
use App\Enums\ProxyStatus;
2023-06-02 13:15:12 +00:00
use App\Enums\ProxyTypes;
use App\Models\Server;
use Illuminate\Bus\Queueable;
2023-09-14 08:12:44 +00:00
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
2023-06-02 13:15:12 +00:00
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-02 13:15:12 +00:00
2023-09-14 08:12:44 +00:00
class ProxyContainerStatusJob implements ShouldQueue, ShouldBeUnique, ShouldBeEncrypted
2023-06-02 13:15:12 +00:00
{
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
{
2023-09-11 20:29:34 +00:00
return [new WithoutOverlapping($this->server->uuid)];
}
2023-09-11 20:29:34 +00:00
public function uniqueId(): string
2023-06-02 13:15:12 +00:00
{
2023-09-11 20:29:34 +00:00
ray($this->server->uuid);
return $this->server->uuid;
2023-06-02 13:15:12 +00:00
}
2023-06-02 13:15:12 +00:00
public function handle(): void
{
try {
2023-09-11 20:29:34 +00:00
$proxyType = data_get($this->server, 'proxy.type');
if ($proxyType === ProxyTypes::NONE->value) {
return;
}
if (is_null($proxyType)) {
if ($this->server->isProxyShouldRun()) {
$this->server->proxy->type = ProxyTypes::TRAEFIK_V2->value;
$this->server->proxy->status = ProxyStatus::EXITED->value;
$this->server->save();
resolve(StartProxy::class)($this->server);
return;
}
}
2023-08-28 18:45:53 +00:00
$container = getContainerStatus(server: $this->server, all_data: true, container_id: 'coolify-proxy', throwError: false);
2023-09-11 20:29:34 +00:00
$containerStatus = data_get($container, 'State.Status');
$databaseContainerStatus = data_get($this->server, 'proxy.status', 'exited');
if ($proxyType !== ProxyTypes::NONE->value) {
if ($containerStatus === 'running') {
$this->server->proxy->status = $containerStatus;
$this->server->save();
return;
}
if ((is_null($containerStatus) ||$containerStatus !== 'running' || $databaseContainerStatus !== 'running' || ($containerStatus && $databaseContainerStatus !== $containerStatus)) && $this->server->isProxyShouldRun()) {
$this->server->proxy->status = $containerStatus;
$this->server->save();
resolve(StartProxy::class)($this->server);
return;
2023-06-02 13:15:12 +00:00
}
}
2023-09-11 15:36:30 +00:00
} catch (\Throwable $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
}
}
}