Refactor server and docker cleanup jobs

This commit is contained in:
Andras Bacsai
2023-11-16 14:29:01 +01:00
parent fb42c43953
commit 2b666ff121
6 changed files with 42 additions and 39 deletions

View File

@@ -16,6 +16,7 @@ class ServerStatusJob implements ShouldQueue, ShouldBeEncrypted
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public ?int $disk_usage = null;
public function __construct(public Server $server)
{
}
@@ -34,23 +35,33 @@ class ServerStatusJob implements ShouldQueue, ShouldBeEncrypted
ray("checking server status for {$this->server->id}");
try {
$this->server->checkServerRediness();
$disk_usage = $this->server->getDiskUsage();
if ($disk_usage >= $this->server->settings->cleanup_after_percentage) {
if ($this->server->high_disk_usage_notification_sent) {
ray('high disk usage notification already sent');
return;
}
$this->server->high_disk_usage_notification_sent = true;
$this->server->save();
$this->server->team->notify(new HighDiskUsage($this->server, $disk_usage, $this->server->settings->cleanup_after_percentage));
} else {
$this->server->high_disk_usage_notification_sent = false;
$this->server->save();
}
$this->cleanup(notify: false);
} 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) {
if ($this->server->high_disk_usage_notification_sent) {
ray('high disk usage notification already sent');
return;
} else {
$this->server->high_disk_usage_notification_sent = true;
$this->server->save();
$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();
}
}
}