Files
coolify/app/Jobs/ServerCheckJob.php
T

103 lines
3.8 KiB
PHP
Raw Normal View History

2024-07-25 20:30:22 +02:00
<?php
namespace App\Jobs;
2024-08-05 15:48:15 +02:00
use App\Actions\Docker\GetContainersStatus;
2024-07-25 20:30:22 +02:00
use App\Actions\Proxy\CheckProxy;
use App\Actions\Proxy\StartProxy;
2024-10-30 14:54:27 +01:00
use App\Actions\Server\StartLogDrain;
2024-07-25 20:30:22 +02:00
use App\Models\Server;
use App\Notifications\Container\ContainerRestarted;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
2024-11-05 13:59:13 +01:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
2024-07-25 20:30:22 +02:00
use Illuminate\Queue\SerializesModels;
class ServerCheckJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
2024-07-25 20:30:22 +02:00
public $timeout = 60;
2024-07-25 20:30:22 +02:00
public $containers;
2024-11-05 13:59:13 +01:00
public function middleware(): array
{
return [(new WithoutOverlapping('server-check-'.$this->server->uuid))->expireAfter(60)->dontRelease()];
2024-11-05 13:59:13 +01:00
}
2024-07-25 20:30:22 +02:00
2024-12-16 14:04:22 +01:00
public function __construct(public Server $server) {}
2024-07-25 20:30:22 +02:00
public function handle()
{
try {
2024-10-25 15:13:23 +02:00
if ($this->server->serverStatus() === false) {
return 'Server is not reachable or not ready.';
}
2024-08-05 15:48:15 +02:00
if (! $this->server->isSwarmWorker() && ! $this->server->isBuildServer()) {
['containers' => $this->containers, 'containerReplicates' => $containerReplicates] = $this->server->getContainers();
if (is_null($this->containers)) {
return 'No containers found.';
}
GetContainersStatus::run($this->server, $this->containers, $containerReplicates);
2024-10-22 14:01:36 +02:00
2024-10-25 12:02:41 +02:00
if ($this->server->isSentinelEnabled()) {
CheckAndStartSentinelJob::dispatch($this->server);
}
2024-09-27 15:36:51 +02:00
if ($this->server->isLogDrainEnabled()) {
$this->checkLogDrainContainer();
}
2024-10-25 12:02:41 +02:00
2024-10-14 21:35:38 +02:00
if ($this->server->proxySet() && ! $this->server->proxy->force_stop) {
$this->server->proxyType();
$foundProxyContainer = $this->containers->filter(function ($value, $key) {
if ($this->server->isSwarm()) {
return data_get($value, 'Spec.Name') === 'coolify-proxy_traefik';
2025-01-07 15:31:43 +01:00
} else {
return data_get($value, 'Name') === '/coolify-proxy';
2024-10-14 21:35:38 +02:00
}
})->first();
if (! $foundProxyContainer) {
try {
$shouldStart = CheckProxy::run($this->server);
if ($shouldStart) {
StartProxy::run($this->server, async: false);
2024-10-14 21:35:38 +02:00
$this->server->team?->notify(new ContainerRestarted('coolify-proxy', $this->server));
}
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
2024-10-14 21:35:38 +02:00
}
} else {
$this->server->proxy->status = data_get($foundProxyContainer, 'State.Status');
$this->server->save();
ConnectProxyToNetworksJob::dispatchSync($this->server);
2024-10-14 21:35:38 +02:00
}
}
2024-07-25 20:30:22 +02:00
}
2025-01-07 15:31:43 +01:00
} catch (\Throwable $e) {
2024-07-25 20:30:22 +02:00
return handleError($e);
}
}
private function checkLogDrainContainer()
{
$foundLogDrainContainer = $this->containers->filter(function ($value, $key) {
return data_get($value, 'Name') === '/coolify-log-drain';
})->first();
if ($foundLogDrainContainer) {
$status = data_get($foundLogDrainContainer, 'State.Status');
if ($status !== 'running') {
StartLogDrain::dispatch($this->server);
2024-07-25 20:30:22 +02:00
}
} else {
StartLogDrain::dispatch($this->server);
2024-07-25 20:30:22 +02:00
}
}
}