coolify/app/Jobs/DockerCleanupJob.php

69 lines
2.8 KiB
PHP
Raw Normal View History

2023-07-07 15:50:36 +02:00
<?php
namespace App\Jobs;
use App\Actions\Server\CleanupDocker;
2023-07-07 15:50:36 +02:00
use App\Models\Server;
use App\Notifications\Server\DockerCleanup;
2023-07-07 15:50:36 +02:00
use Illuminate\Bus\Queueable;
2023-09-14 10:12:44 +02:00
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
2023-07-07 15:50:36 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-11-10 10:55:23 +01:00
use Illuminate\Support\Facades\Log;
2023-11-22 16:39:16 +01:00
use RuntimeException;
2023-07-07 15:50:36 +02:00
2024-06-10 22:43:34 +02:00
class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueue
2023-07-07 15:50:36 +02:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 300;
2024-06-10 22:43:34 +02:00
2023-08-29 10:00:29 +02:00
public ?int $usageBefore = null;
2024-06-28 12:04:28 +02:00
public function __construct(public Server $server) {}
2024-06-10 22:43:34 +02:00
2023-07-07 15:50:36 +02:00
public function handle(): void
{
try {
$isInprogress = false;
$this->server->applications()->each(function ($application) use (&$isInprogress) {
if ($application->isDeploymentInprogress()) {
$isInprogress = true;
2024-06-10 22:43:34 +02:00
return;
}
});
2024-06-28 12:03:38 +02:00
// if ($isInprogress) {
// throw new RuntimeException('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping...');
// }
2024-06-10 22:43:34 +02:00
if (! $this->server->isFunctional()) {
return;
}
$this->usageBefore = $this->server->getDiskUsage();
2024-06-10 22:43:34 +02:00
ray('Usage before: '.$this->usageBefore);
if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) {
2024-06-10 22:43:34 +02:00
ray('Cleaning up '.$this->server->name);
CleanupDocker::run($this->server);
$usageAfter = $this->server->getDiskUsage();
2024-06-10 22:43:34 +02:00
if ($usageAfter < $this->usageBefore) {
$this->server->team?->notify(new DockerCleanup($this->server, 'Saved '.($this->usageBefore - $usageAfter).'% disk space.'));
// ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
// send_internal_notification('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
2024-06-10 22:43:34 +02:00
Log::info('DockerCleanupJob done: Saved '.($this->usageBefore - $usageAfter).'% disk space on '.$this->server->name);
2023-08-29 10:00:29 +02:00
} else {
2024-06-10 22:43:34 +02:00
Log::info('DockerCleanupJob failed to save disk space on '.$this->server->name);
2023-07-07 15:50:36 +02:00
}
} else {
2024-06-10 22:43:34 +02:00
ray('No need to clean up '.$this->server->name);
Log::info('No need to clean up '.$this->server->name);
2023-07-07 15:50:36 +02:00
}
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2024-06-18 12:47:55 +02:00
// send_internal_notification('DockerCleanupJob failed with: '.$e->getMessage());
ray($e->getMessage());
2023-08-24 21:09:58 +02:00
throw $e;
2023-07-07 15:50:36 +02:00
}
}
2023-07-26 13:33:48 +02:00
}