feat(previews): implement soft delete and cleanup for ApplicationPreview, enhancing resource management in DeleteResourceJob
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Actions\Server\CleanupDocker;
|
|||||||
use App\Actions\Service\DeleteService;
|
use App\Actions\Service\DeleteService;
|
||||||
use App\Actions\Service\StopService;
|
use App\Actions\Service\StopService;
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
|
use App\Models\ApplicationPreview;
|
||||||
use App\Models\Service;
|
use App\Models\Service;
|
||||||
use App\Models\StandaloneClickhouse;
|
use App\Models\StandaloneClickhouse;
|
||||||
use App\Models\StandaloneDragonfly;
|
use App\Models\StandaloneDragonfly;
|
||||||
@@ -30,7 +31,7 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
public function __construct(
|
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 $deleteConfigurations = true,
|
||||||
public bool $deleteVolumes = true,
|
public bool $deleteVolumes = true,
|
||||||
public bool $dockerCleanup = true,
|
public bool $dockerCleanup = true,
|
||||||
@@ -42,6 +43,13 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
|
|||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
// Handle ApplicationPreview instances separately
|
||||||
|
if ($this->resource instanceof ApplicationPreview) {
|
||||||
|
$this->deleteApplicationPreview();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch ($this->resource->type()) {
|
switch ($this->resource->type()) {
|
||||||
case 'application':
|
case 'application':
|
||||||
StopApplication::run($this->resource, previewDeployments: true);
|
StopApplication::run($this->resource, previewDeployments: true);
|
||||||
@@ -104,4 +112,55 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
|
|||||||
Artisan::queue('cleanup:stucked-resources');
|
Artisan::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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Project\Application;
|
namespace App\Livewire\Project\Application;
|
||||||
|
|
||||||
use App\Actions\Docker\GetContainersStatus;
|
use App\Actions\Docker\GetContainersStatus;
|
||||||
|
use App\Jobs\DeleteResourceJob;
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use App\Models\ApplicationPreview;
|
use App\Models\ApplicationPreview;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -205,48 +206,28 @@ class Previews extends Component
|
|||||||
public function delete(int $pull_request_id)
|
public function delete(int $pull_request_id)
|
||||||
{
|
{
|
||||||
try {
|
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()) {
|
if (! $preview) {
|
||||||
instant_remote_process(["docker stack rm {$this->application->uuid}-{$pull_request_id}"], $server);
|
$this->dispatch('error', 'Preview not found.');
|
||||||
} else {
|
|
||||||
$containers = getCurrentApplicationContainerStatus($server, $this->application->id, $pull_request_id)->toArray();
|
return;
|
||||||
$this->stopContainers($containers, $server);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ApplicationPreview::where('application_id', $this->application->id)
|
// Soft delete immediately for instant UI feedback
|
||||||
->where('pull_request_id', $pull_request_id)
|
$preview->delete();
|
||||||
->first()
|
|
||||||
->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('update_links');
|
||||||
$this->dispatch('success', 'Preview deleted.');
|
$this->dispatch('success', 'Preview deletion started. It may take a few moments to complete.');
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return handleError($e, $this);
|
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -2,19 +2,25 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Spatie\Url\Url;
|
use Spatie\Url\Url;
|
||||||
use Visus\Cuid2\Cuid2;
|
use Visus\Cuid2\Cuid2;
|
||||||
|
|
||||||
class ApplicationPreview extends BaseModel
|
class ApplicationPreview extends BaseModel
|
||||||
{
|
{
|
||||||
|
use SoftDeletes;
|
||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
protected static function booted()
|
protected static function booted()
|
||||||
{
|
{
|
||||||
static::deleting(function ($preview) {
|
static::forceDeleting(function ($preview) {
|
||||||
if (data_get($preview, 'application.build_pack') === 'dockercompose') {
|
|
||||||
$server = $preview->application->destination->server;
|
$server = $preview->application->destination->server;
|
||||||
$composeFile = $preview->application->parse(pull_request_id: $preview->pull_request_id);
|
$application = $preview->application;
|
||||||
|
|
||||||
|
if (data_get($preview, 'application.build_pack') === 'dockercompose') {
|
||||||
|
// Docker Compose volume and network cleanup
|
||||||
|
$composeFile = $application->parse(pull_request_id: $preview->pull_request_id);
|
||||||
$volumes = data_get($composeFile, 'volumes');
|
$volumes = data_get($composeFile, 'volumes');
|
||||||
$networks = data_get($composeFile, 'networks');
|
$networks = data_get($composeFile, 'networks');
|
||||||
$networkKeys = collect($networks)->keys();
|
$networkKeys = collect($networks)->keys();
|
||||||
@@ -26,7 +32,18 @@ class ApplicationPreview extends BaseModel
|
|||||||
instant_remote_process(["docker network disconnect $key coolify-proxy"], $server, false);
|
instant_remote_process(["docker network disconnect $key coolify-proxy"], $server, false);
|
||||||
instant_remote_process(["docker network rm $key"], $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) {
|
static::saving(function ($preview) {
|
||||||
if ($preview->isDirty('status')) {
|
if ($preview->isDirty('status')) {
|
||||||
@@ -50,6 +67,11 @@ class ApplicationPreview extends BaseModel
|
|||||||
return $this->belongsTo(Application::class);
|
return $this->belongsTo(Application::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function persistentStorages()
|
||||||
|
{
|
||||||
|
return $this->morphMany(\App\Models\LocalPersistentVolume::class, 'resource');
|
||||||
|
}
|
||||||
|
|
||||||
public function generate_preview_fqdn()
|
public function generate_preview_fqdn()
|
||||||
{
|
{
|
||||||
if (is_null($this->fqdn) && $this->application->fqdn) {
|
if (is_null($this->fqdn) && $this->application->fqdn) {
|
||||||
|
@@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user