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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat(previews): implement soft delete and cleanup for ApplicationPreview, enhancing resource management in DeleteResourceJob
Andras Bacsai13f10028db3c
diff --git a/app/Jobs/DeleteResourceJob.php b/app/Jobs/DeleteResourceJob.phpindex 408bb2a7a6..2750110f20 100644--- a/app/Jobs/DeleteResourceJob.php+++ b/app/Jobs/DeleteResourceJob.php@@ -8,6 +8,7 @@ use App\Actions\Server\CleanupDocker;use App\Actions\Service\DeleteService;use App\Actions\Service\StopService;use App\Models\Application;+use App\Models\ApplicationPreview;use App\Models\Service;use App\Models\StandaloneClickhouse;use App\Models\StandaloneDragonfly;@@ -30,7 +31,7 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueueuse Dispatchable, InteractsWithQueue, Queueable, SerializesModels;public function __construct(- public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $resource,+ public Application|ApplicationPreview|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $resource,public bool $deleteConfigurations = true,public bool $deleteVolumes = true,public bool $dockerCleanup = true,@@ -42,6 +43,13 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueuepublic function handle(){try {+ // Handle ApplicationPreview instances separately+ if ($this->resource instanceof ApplicationPreview) {+ $this->deleteApplicationPreview();++ return;+ }+switch ($this->resource->type()) {case 'application':StopApplication::run($this->resource, previewDeployments: true);@@ -104,4 +112,55 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueueArtisan::queue('cleanup:stucked-resources');}}++ private function deleteApplicationPreview()+ {+ $application = $this->resource->application;+ $server = $application->destination->server;+ $pull_request_id = $this->resource->pull_request_id;++ // Ensure the preview is soft deleted (may already be done in Livewire component)+ if (! $this->resource->trashed()) {+ $this->resource->delete();+ }++ try {+ if ($server->isSwarm()) {+ instant_remote_process(["docker stack rm {$application->uuid}-{$pull_request_id}"], $server);+ } else {+ $containers = getCurrentApplicationContainerStatus($server, $application->id, $pull_request_id)->toArray();+ $this->stopPreviewContainers($containers, $server);+ }+ } catch (\Throwable $e) {+ // Log the error but don't fail the job+ ray('Error stopping preview containers: '.$e->getMessage());+ }++ // Finally, force delete to trigger resource cleanup+ $this->resource->forceDelete();+ }++ private function stopPreviewContainers(array $containers, $server, int $timeout = 30)+ {+ if (empty($containers)) {+ return;+ }++ $containerNames = [];+ foreach ($containers as $container) {+ $containerNames[] = str_replace('/', '', $container['Names']);+ }++ $containerList = implode(' ', array_map('escapeshellarg', $containerNames));+ $commands = [+ "docker stop --time=$timeout $containerList",+ "docker rm -f $containerList",+ ];++ instant_remote_process(+ command: $commands,+ server: $server,+ throwError: false+ );+ }}diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.phpindex 64e920cd73..62b1f1929e 100644--- a/app/Livewire/Project/Application/Previews.php+++ b/app/Livewire/Project/Application/Previews.php@@ -3,6 +3,7 @@namespace App\Livewire\Project\Application;use App\Actions\Docker\GetContainersStatus;+use App\Jobs\DeleteResourceJob;use App\Models\Application;use App\Models\ApplicationPreview;use Illuminate\Support\Collection;@@ -205,48 +206,28 @@ class Previews extends Componentpublic function delete(int $pull_request_id){try {- $server = $this->application->destination->server;+ $preview = ApplicationPreview::where('application_id', $this->application->id)+ ->where('pull_request_id', $pull_request_id)+ ->first();- if ($this->application->destination->server->isSwarm()) {- instant_remote_process(["docker stack rm {$this->application->uuid}-{$pull_request_id}"], $server);- } else {- $containers = getCurrentApplicationContainerStatus($server, $this->application->id, $pull_request_id)->toArray();- $this->stopContainers($containers, $server);+ if (! $preview) {+ $this->dispatch('error', 'Preview not found.');++ return;}- ApplicationPreview::where('application_id', $this->application->id)- ->where('pull_request_id', $pull_request_id)- ->first()- ->delete();+ // Soft delete immediately for instant UI feedback+ $preview->delete();- $this->application->refresh();+ // Dispatch the job for async cleanup (container stopping + force delete)+ DeleteResourceJob::dispatch($preview);++ // Refresh the application and its previews relationship to reflect the soft delete+ $this->application->load('previews');$this->dispatch('update_links');- $this->dispatch('success', 'Preview deleted.');+ $this->dispatch('success', 'Preview deletion started. It may take a few moments to complete.');} catch (\Throwable $e) {return handleError($e, $this);}}-- private function stopContainers(array $containers, $server, int $timeout = 30)- {- if (empty($containers)) {- return;- }- $containerNames = [];- foreach ($containers as $container) {- $containerNames[] = str_replace('/', '', $container['Names']);- }-- $containerList = implode(' ', array_map('escapeshellarg', $containerNames));- $commands = [- "docker stop --time=$timeout $containerList",- "docker rm -f $containerList",- ];-- instant_remote_process(- command: $commands,- server: $server,- throwError: false- );- }}diff --git a/app/Models/ApplicationPreview.php b/app/Models/ApplicationPreview.phpindex a92eb320b5..f45f9da40c 100644--- a/app/Models/ApplicationPreview.php+++ b/app/Models/ApplicationPreview.php@@ -2,19 +2,25 @@namespace App\Models;+use Illuminate\Database\Eloquent\SoftDeletes;use Spatie\Url\Url;use Visus\Cuid2\Cuid2;class ApplicationPreview extends BaseModel{+ use SoftDeletes;+protected $guarded = [];protected static function booted(){- static::deleting(function ($preview) {+ static::forceDeleting(function ($preview) {+ $server = $preview->application->destination->server;+ $application = $preview->application;+if (data_get($preview, 'application.build_pack') === 'dockercompose') {- $server = $preview->application->destination->server;- $composeFile = $preview->application->parse(pull_request_id: $preview->pull_request_id);+ // Docker Compose volume and network cleanup+ $composeFile = $application->parse(pull_request_id: $preview->pull_request_id);$volumes = data_get($composeFile, 'volumes');$networks = data_get($composeFile, 'networks');$networkKeys = collect($networks)->keys();@@ -26,7 +32,18 @@ class ApplicationPreview extends BaseModelinstant_remote_process(["docker network disconnect $key coolify-proxy"], $server, false);instant_remote_process(["docker network rm $key"], $server, false);});+ } else {+ // Regular application volume cleanup+ $persistentStorages = $preview->persistentStorages()->get() ?? collect();+ if ($persistentStorages->count() > 0) {+ foreach ($persistentStorages as $storage) {+ instant_remote_process(["docker volume rm -f $storage->name"], $server, false);+ }+ }}++ // Clean up persistent storage records+ $preview->persistentStorages()->delete();});static::saving(function ($preview) {if ($preview->isDirty('status')) {@@ -50,6 +67,11 @@ class ApplicationPreview extends BaseModelreturn $this->belongsTo(Application::class);}+ public function persistentStorages()+ {+ return $this->morphMany(\App\Models\LocalPersistentVolume::class, 'resource');+ }+public function generate_preview_fqdn(){if (is_null($this->fqdn) && $this->application->fqdn) {diff --git a/database/migrations/2025_07_14_191016_add_deleted_at_to_application_previews_table.php b/database/migrations/2025_07_14_191016_add_deleted_at_to_application_previews_table.phpnew file mode 100644index 0000000000..25aa0f5f02--- /dev/null+++ b/database/migrations/2025_07_14_191016_add_deleted_at_to_application_previews_table.php@@ -0,0 +1,28 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::table('application_previews', function (Blueprint $table) {+ $table->softDeletes();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('application_previews', function (Blueprint $table) {+ $table->dropSoftDeletes();+ });+ }+};