2024-07-25 18:30:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2024-08-05 13:48:15 +00:00
|
|
|
use App\Actions\Docker\GetContainersStatus;
|
2024-07-25 18:30:22 +00:00
|
|
|
use App\Actions\Proxy\CheckProxy;
|
|
|
|
|
use App\Actions\Proxy\StartProxy;
|
2024-10-30 13:54:27 +00:00
|
|
|
use App\Actions\Server\StartLogDrain;
|
2024-07-25 18:30:22 +00: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 12:59:13 +00:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2024-07-25 18:30:22 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
|
|
class ServerCheckJob implements ShouldBeEncrypted, ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
2024-09-23 21:48:12 +00:00
|
|
|
public $tries = 1;
|
2024-07-25 18:30:22 +00:00
|
|
|
|
2024-09-09 07:08:43 +00:00
|
|
|
public $timeout = 60;
|
|
|
|
|
|
2024-07-25 18:30:22 +00:00
|
|
|
public $containers;
|
2024-11-05 12:59:13 +00:00
|
|
|
|
|
|
|
|
public function middleware(): array
|
|
|
|
|
{
|
2025-05-29 15:31:43 +00:00
|
|
|
return [(new WithoutOverlapping('server-check-'.$this->server->uuid))->dontRelease()];
|
2024-11-05 12:59:13 +00:00
|
|
|
}
|
2024-07-25 18:30:22 +00:00
|
|
|
|
2024-12-16 13:04:22 +00:00
|
|
|
public function __construct(public Server $server) {}
|
2024-07-25 18:30:22 +00:00
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
try {
|
2024-10-25 13:13:23 +00:00
|
|
|
if ($this->server->serverStatus() === false) {
|
|
|
|
|
return 'Server is not reachable or not ready.';
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 13:48:15 +00: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 12:01:36 +00:00
|
|
|
|
2024-10-25 10:02:41 +00:00
|
|
|
if ($this->server->isSentinelEnabled()) {
|
|
|
|
|
CheckAndStartSentinelJob::dispatch($this->server);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 13:36:51 +00:00
|
|
|
if ($this->server->isLogDrainEnabled()) {
|
|
|
|
|
$this->checkLogDrainContainer();
|
|
|
|
|
}
|
2024-10-25 10:02:41 +00:00
|
|
|
|
2024-10-14 19:35:38 +00: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 14:31:43 +00:00
|
|
|
} else {
|
|
|
|
|
return data_get($value, 'Name') === '/coolify-proxy';
|
2024-10-14 19:35:38 +00:00
|
|
|
}
|
|
|
|
|
})->first();
|
|
|
|
|
if (! $foundProxyContainer) {
|
|
|
|
|
try {
|
|
|
|
|
$shouldStart = CheckProxy::run($this->server);
|
|
|
|
|
if ($shouldStart) {
|
2025-06-16 11:13:01 +00:00
|
|
|
StartProxy::run($this->server, async: false);
|
2024-10-14 19:35:38 +00:00
|
|
|
$this->server->team?->notify(new ContainerRestarted('coolify-proxy', $this->server));
|
|
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-10-14 19:35:38 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->server->proxy->status = data_get($foundProxyContainer, 'State.Status');
|
|
|
|
|
$this->server->save();
|
|
|
|
|
$connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
|
|
|
|
|
instant_remote_process($connectProxyToDockerNetworks, $this->server, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-25 18:30:22 +00:00
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-07-25 18:30:22 +00: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') {
|
2024-11-22 10:16:01 +00:00
|
|
|
StartLogDrain::dispatch($this->server);
|
2024-07-25 18:30:22 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-11-22 10:16:01 +00:00
|
|
|
StartLogDrain::dispatch($this->server);
|
2024-07-25 18:30:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|