Refactor DockerCleanupJob and Application model

This commit is contained in:
Andras Bacsai 2023-11-10 10:34:28 +01:00
parent ed34fc9645
commit 23a94c9378
2 changed files with 23 additions and 13 deletions

View File

@ -3,6 +3,7 @@
namespace App\Jobs; namespace App\Jobs;
use App\Models\Server; use App\Models\Server;
use Exception;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -15,7 +16,7 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 1000; public $timeout = 300;
public ?string $dockerRootFilesystem = null; public ?string $dockerRootFilesystem = null;
public ?int $usageBefore = null; public ?int $usageBefore = null;
@ -33,14 +34,15 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
} }
public function handle(): void public function handle(): void
{ {
$queuedCount = 0; $isInprogress = false;
$this->server->applications()->each(function ($application) use ($queuedCount) { $this->server->applications()->each(function ($application) use (&$isInprogress) {
$count = data_get($application->deployments(), 'count', 0); if ($application->isDeploymentInprogress()) {
$queuedCount += $count; $isInprogress = true;
return;
}
}); });
if ($queuedCount > 0) { if ($isInprogress) {
ray('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping')->color('orange'); throw new Exception('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping...');
return;
} }
try { try {
if (!$this->server->isFunctional()) { if (!$this->server->isFunctional()) {
@ -49,23 +51,23 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
$this->dockerRootFilesystem = "/"; $this->dockerRootFilesystem = "/";
$this->usageBefore = $this->getFilesystemUsage(); $this->usageBefore = $this->getFilesystemUsage();
if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) { if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) {
ray('Cleaning up ' . $this->server->name)->color('orange'); ray('Cleaning up ' . $this->server->name);
instant_remote_process(['docker image prune -af'], $this->server); instant_remote_process(['docker image prune -af'], $this->server);
instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $this->server); instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $this->server);
instant_remote_process(['docker builder prune -af'], $this->server); instant_remote_process(['docker builder prune -af'], $this->server);
$usageAfter = $this->getFilesystemUsage(); $usageAfter = $this->getFilesystemUsage();
if ($usageAfter < $this->usageBefore) { if ($usageAfter < $this->usageBefore) {
ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name)->color('orange'); 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); send_internal_notification('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
} else { } else {
ray('DockerCleanupJob failed to save disk space on ' . $this->server->name)->color('orange'); ray('DockerCleanupJob failed to save disk space on ' . $this->server->name);
} }
} else { } else {
ray('No need to clean up ' . $this->server->name)->color('orange'); ray('No need to clean up ' . $this->server->name);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage()); send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage());
ray($e->getMessage())->color('orange'); ray($e->getMessage());
throw $e; throw $e;
} }
} }

View File

@ -213,6 +213,14 @@ class Application extends BaseModel
return $this->morphTo(); return $this->morphTo();
} }
public function isDeploymentInprogress() {
$deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->where('status', 'in_progress')->count();
if ($deployments > 0) {
return true;
}
return false;
}
public function deployments(int $skip = 0, int $take = 10) public function deployments(int $skip = 0, int $take = 10)
{ {
$deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc'); $deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc');