2023-07-07 13:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2024-01-12 07:45:24 +00:00
|
|
|
use App\Actions\Server\CleanupDocker;
|
2023-07-07 13:50:36 +00:00
|
|
|
use App\Models\Server;
|
2024-03-02 14:18:49 +00:00
|
|
|
use App\Notifications\Server\DockerCleanup;
|
2023-07-07 13:50:36 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2023-09-14 08:12:44 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
2023-07-07 13:50:36 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-11-10 09:55:23 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-07-07 13:50:36 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueue
|
2023-07-07 13:50:36 +00:00
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-08-26 10:23:03 +00:00
|
|
|
public $timeout = 600;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-08-26 10:23:03 +00:00
|
|
|
public $tries = 1;
|
2024-08-21 08:50:05 +00:00
|
|
|
|
|
|
|
|
public ?string $usageBefore = null;
|
2023-09-01 08:11:00 +00:00
|
|
|
|
2024-09-22 18:02:51 +00:00
|
|
|
public function __construct(public Server $server, public bool $manualCleanup = false) {}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-07-07 13:50:36 +00:00
|
|
|
public function handle(): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
2024-08-14 19:53:03 +00:00
|
|
|
if (! $this->server->isFunctional()) {
|
2023-09-29 12:26:42 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-08-14 19:53:03 +00:00
|
|
|
|
2024-09-22 18:02:51 +00:00
|
|
|
if ($this->manualCleanup || $this->server->settings->force_docker_cleanup) {
|
2024-09-27 14:43:07 +00:00
|
|
|
Log::info('DockerCleanupJob '.($this->manualCleanup ? 'manual' : 'force').' cleanup on '.$this->server->name);
|
2024-09-22 18:02:51 +00:00
|
|
|
CleanupDocker::run(server: $this->server);
|
2024-09-27 14:43:07 +00:00
|
|
|
|
2024-07-18 12:38:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2024-07-18 12:43:21 +00:00
|
|
|
|
2023-11-16 13:29:01 +00:00
|
|
|
$this->usageBefore = $this->server->getDiskUsage();
|
2024-08-07 08:54:22 +00:00
|
|
|
if (str($this->usageBefore)->isEmpty() || $this->usageBefore === null || $this->usageBefore === 0) {
|
2024-08-26 10:23:03 +00:00
|
|
|
Log::info('DockerCleanupJob force cleanup on '.$this->server->name);
|
|
|
|
|
CleanupDocker::run(server: $this->server);
|
2024-08-05 11:45:53 +00:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-08-26 10:23:03 +00:00
|
|
|
if ($this->usageBefore >= $this->server->settings->docker_cleanup_threshold) {
|
2024-08-21 08:50:05 +00:00
|
|
|
CleanupDocker::run(server: $this->server);
|
2023-11-16 10:53:37 +00:00
|
|
|
$usageAfter = $this->server->getDiskUsage();
|
2024-06-10 20:43:34 +00:00
|
|
|
if ($usageAfter < $this->usageBefore) {
|
2024-08-26 10:23:03 +00:00
|
|
|
$this->server->team?->notify(new DockerCleanup($this->server, 'Saved '.($this->usageBefore - $usageAfter).'% disk space.'));
|
|
|
|
|
Log::info('DockerCleanupJob done: Saved '.($this->usageBefore - $usageAfter).'% disk space on '.$this->server->name);
|
2023-08-29 08:00:29 +00:00
|
|
|
} else {
|
2024-08-26 10:23:03 +00:00
|
|
|
Log::info('DockerCleanupJob failed to save disk space on '.$this->server->name);
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
} else {
|
2024-08-26 10:23:03 +00:00
|
|
|
Log::info('No need to clean up '.$this->server->name);
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2024-08-21 08:50:05 +00:00
|
|
|
CleanupDocker::run(server: $this->server);
|
|
|
|
|
Log::error('DockerCleanupJob failed: '.$e->getMessage());
|
2023-08-24 19:09:58 +00:00
|
|
|
throw $e;
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-26 10:23:03 +00:00
|
|
|
}
|