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;
|
|
|
|
|
|
|
|
class ServerStatusJob implements ShouldQueue, ShouldBeEncrypted
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2023-11-16 13:29:01 +00:00
|
|
|
public ?int $disk_usage = null;
|
2023-11-16 10:53:37 +00:00
|
|
|
public function __construct(public Server $server)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
|
|
|
return [(new WithoutOverlapping($this->server->id))->dontRelease()];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uniqueId(): int
|
|
|
|
{
|
|
|
|
return $this->server->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
{
|
|
|
|
ray("checking server status for {$this->server->id}");
|
|
|
|
try {
|
2023-12-20 11:25:14 +00:00
|
|
|
if (!$this->server->isServerReady(4)) {
|
|
|
|
throw new \RuntimeException('Server is not reachable.');
|
|
|
|
};
|
|
|
|
$this->cleanup(notify: false);
|
2023-11-16 13:29:01 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
send_internal_notification('ServerStatusJob failed with: ' . $e->getMessage());
|
|
|
|
ray($e->getMessage());
|
|
|
|
handleError($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|