Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Refactor DockerCleanupJob and Application model

Andras Bacsai23a94c937896
diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.php
index 0813c12184..49e872cb7a 100644
--- a/app/Jobs/DockerCleanupJob.php
+++ b/app/Jobs/DockerCleanupJob.php
@@ -3,6 +3,7 @@
namespace App\Jobs;
use App\Models\Server;
+use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -15,7 +16,7 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public $timeout = 1000;
+ public $timeout = 300;
public ?string $dockerRootFilesystem = null;
public ?int $usageBefore = null;
@@ -33,14 +34,15 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
}
public function handle(): void
{
- $queuedCount = 0;
- $this->server->applications()->each(function ($application) use ($queuedCount) {
- $count = data_get($application->deployments(), 'count', 0);
- $queuedCount += $count;
+ $isInprogress = false;
+ $this->server->applications()->each(function ($application) use (&$isInprogress) {
+ if ($application->isDeploymentInprogress()) {
+ $isInprogress = true;
+ return;
+ }
});
- if ($queuedCount > 0) {
- ray('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping')->color('orange');
- return;
+ if ($isInprogress) {
+ throw new Exception('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping...');
}
try {
if (!$this->server->isFunctional()) {
@@ -49,23 +51,23 @@ class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
$this->dockerRootFilesystem = "/";
$this->usageBefore = $this->getFilesystemUsage();
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 container prune -f --filter "label=coolify.managed=true"'], $this->server);
instant_remote_process(['docker builder prune -af'], $this->server);
$usageAfter = $this->getFilesystemUsage();
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);
} 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 {
- ray('No need to clean up ' . $this->server->name)->color('orange');
+ ray('No need to clean up ' . $this->server->name);
}
} catch (\Throwable $e) {
send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage());
- ray($e->getMessage())->color('orange');
+ ray($e->getMessage());
throw $e;
}
}
diff --git a/app/Models/Application.php b/app/Models/Application.php
index ca7334b7e9..0e72f94ce8 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -213,6 +213,14 @@ class Application extends BaseModel
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)
{
$deployments = ApplicationDeploymentQueue::where('application_id', $this->id)->orderBy('created_at', 'desc');