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