coolify/app/Jobs/ServerStatusJob.php

136 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
use App\Models\Server;
use App\Notifications\Server\HighDiskUsage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
2024-06-10 22:43:34 +02:00
class ServerStatusJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int|string|null $disk_usage = null;
2024-06-10 22:43:34 +02:00
2024-05-03 13:31:35 +02:00
public $tries = 3;
2024-06-10 22:43:34 +02:00
public function backoff(): int
{
return isDev() ? 1 : 3;
}
2024-06-10 22:43:34 +02:00
2024-06-25 14:30:37 +02:00
public function __construct(public Server $server) {}
2024-06-10 22:43:34 +02:00
public function middleware(): array
{
return [(new WithoutOverlapping($this->server->uuid))];
}
public function uniqueId(): int
{
return $this->server->uuid;
}
2023-12-20 12:33:58 +01:00
public function handle()
{
2024-06-18 16:43:18 +02:00
if (! $this->server->isServerReady($this->tries)) {
throw new \RuntimeException('Server is not ready.');
2024-06-10 22:43:34 +02:00
}
try {
if ($this->server->isFunctional()) {
$this->cleanup(notify: false);
$this->remove_unnecessary_coolify_yaml();
2024-06-20 13:17:06 +02:00
if ($this->server->isSentinelEnabled()) {
2024-05-10 09:21:19 +02:00
$this->server->checkSentinel();
}
}
} catch (\Throwable $e) {
// send_internal_notification('ServerStatusJob failed with: '.$e->getMessage());
ray($e->getMessage());
2024-06-10 22:43:34 +02:00
2023-12-20 12:33:58 +01:00
return handleError($e);
}
2024-05-27 14:16:10 +02:00
try {
// $this->check_docker_engine();
} catch (\Throwable $e) {
// Do nothing
}
}
2024-06-10 22:43:34 +02:00
private function check_docker_engine()
{
$version = instant_remote_process([
2024-06-10 22:43:34 +02:00
'docker info',
], $this->server, false);
if (is_null($version)) {
$os = instant_remote_process([
2024-06-10 22:43:34 +02:00
'cat /etc/os-release | grep ^ID=',
], $this->server, false);
$os = str($os)->after('ID=')->trim();
if ($os === 'ubuntu') {
try {
instant_remote_process([
2024-06-10 22:43:34 +02:00
'systemctl start docker',
], $this->server);
} catch (\Throwable $e) {
ray($e->getMessage());
2024-06-10 22:43:34 +02:00
return handleError($e);
}
} else {
try {
instant_remote_process([
2024-06-10 22:43:34 +02:00
'service docker start',
], $this->server);
} catch (\Throwable $e) {
ray($e->getMessage());
2024-06-10 22:43:34 +02:00
return handleError($e);
}
}
}
}
2024-06-10 22:43:34 +02:00
private function remove_unnecessary_coolify_yaml()
{
// This will remote the coolify.yaml file from the server as it is not needed on cloud servers
if (isCloud() && $this->server->id !== 0) {
2024-06-18 16:43:18 +02:00
$file = $this->server->proxyPath().'/dynamic/coolify.yaml';
2024-06-10 22:43:34 +02:00
return instant_remote_process([
"rm -f $file",
], $this->server, false);
}
}
2024-06-10 22:43:34 +02:00
public function cleanup(bool $notify = false): void
{
$this->disk_usage = $this->server->getDiskUsage();
if ($this->disk_usage >= $this->server->settings->cleanup_after_percentage) {
if ($notify) {
if ($this->server->high_disk_usage_notification_sent) {
ray('high disk usage notification already sent');
2024-06-10 22:43:34 +02:00
return;
} else {
$this->server->high_disk_usage_notification_sent = true;
$this->server->save();
2023-12-13 12:01:27 +01:00
$this->server->team?->notify(new HighDiskUsage($this->server, $this->disk_usage, $this->server->settings->cleanup_after_percentage));
}
} else {
DockerCleanupJob::dispatchSync($this->server);
$this->cleanup(notify: true);
}
} else {
$this->server->high_disk_usage_notification_sent = false;
$this->server->save();
}
}
}