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.

fix(resources): schedule stuck cleanup and delete metadata before jobs

Andras Bacsai45f9a3398554
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index e6dc323838..8d4d017c81 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -46,6 +46,10 @@ class Kernel extends ConsoleKernel
->hourly()
->when(fn () => config('constants.ssh.mux_enabled') && ! config('constants.coolify.is_windows_docker_desktop'));
$this->scheduleInstance->command('cleanup:redis --clear-locks')->daily();
+ $this->scheduleInstance->command('cleanup:stucked-resources')
+ ->daily()
+ ->onOneServer()
+ ->withoutOverlapping(60);
$this->scheduleInstance->command('sanctum:prune-expired --hours=1')->hourly()->onOneServer();
$this->scheduleInstance->job(new ApiTokenExpirationWarningJob)->hourly()->onOneServer();
diff --git a/app/Http/Controllers/Api/ApplicationsController.php b/app/Http/Controllers/Api/ApplicationsController.php
index 285cf8e2f1..a0e1714ad6 100644
--- a/app/Http/Controllers/Api/ApplicationsController.php
+++ b/app/Http/Controllers/Api/ApplicationsController.php
@@ -2560,6 +2560,8 @@ class ApplicationsController extends Controller
$this->authorize('delete', $application);
+ $application->delete();
+
DeleteResourceJob::dispatch(
resource: $application,
deleteVolumes: $request->boolean('delete_volumes', true),
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.php
index 7b62b4980a..aeb69ac8b4 100644
--- a/app/Http/Controllers/Api/DatabasesController.php
+++ b/app/Http/Controllers/Api/DatabasesController.php
@@ -2586,6 +2586,8 @@ class DatabasesController extends Controller
$this->authorize('delete', $database);
+ $database->delete();
+
DeleteResourceJob::dispatch(
resource: $database,
deleteVolumes: $request->boolean('delete_volumes', true),
diff --git a/app/Http/Controllers/Api/ServicesController.php b/app/Http/Controllers/Api/ServicesController.php
index d7d4953f9c..9bfcfd8539 100644
--- a/app/Http/Controllers/Api/ServicesController.php
+++ b/app/Http/Controllers/Api/ServicesController.php
@@ -1020,6 +1020,8 @@ class ServicesController extends Controller
$this->authorize('delete', $service);
+ $service->delete();
+
DeleteResourceJob::dispatch(
resource: $service,
deleteVolumes: $request->boolean('delete_volumes', true),
diff --git a/app/Jobs/DeleteResourceJob.php b/app/Jobs/DeleteResourceJob.php
index e6f63ec669..d07f346e56 100644
--- a/app/Jobs/DeleteResourceJob.php
+++ b/app/Jobs/DeleteResourceJob.php
@@ -26,7 +26,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
@@ -128,7 +127,6 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
$this->resource->forceDelete();
});
- Artisan::queue('cleanup:stucked-resources');
}
private function isDatabase(): bool
diff --git a/tests/Feature/Api/LifecycleApisTest.php b/tests/Feature/Api/LifecycleApisTest.php
index 913eac59a5..4973453e93 100644
--- a/tests/Feature/Api/LifecycleApisTest.php
+++ b/tests/Feature/Api/LifecycleApisTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Jobs\DeleteResourceJob;
use App\Jobs\ScheduledTaskJob;
use App\Models\Application;
use App\Models\CloudInitScript;
@@ -276,6 +277,60 @@ describe('POST /api/v1/services/{uuid}/clone', function () {
});
});
+describe('DELETE resource endpoints', function () {
+ test('soft deletes an application before queuing cleanup', function () {
+ Queue::fake();
+
+ $this->withHeaders($this->headers)
+ ->deleteJson("/api/v1/applications/{$this->application->uuid}")
+ ->assertOk();
+
+ expect(Application::find($this->application->id))->toBeNull()
+ ->and(Application::withTrashed()->find($this->application->id)?->trashed())->toBeTrue();
+ Queue::assertPushed(DeleteResourceJob::class);
+ });
+
+ test('soft deletes a service before queuing cleanup', function () {
+ Queue::fake();
+ $service = Service::factory()->create([
+ 'environment_id' => $this->environment->id,
+ 'destination_id' => $this->destination->id,
+ 'destination_type' => $this->destination->getMorphClass(),
+ 'server_id' => $this->server->id,
+ ]);
+
+ $this->withHeaders($this->headers)
+ ->deleteJson("/api/v1/services/{$service->uuid}")
+ ->assertOk();
+
+ expect(Service::find($service->id))->toBeNull()
+ ->and(Service::withTrashed()->find($service->id)?->trashed())->toBeTrue();
+ Queue::assertPushed(DeleteResourceJob::class);
+ });
+
+ test('soft deletes a database before queuing cleanup', function () {
+ Queue::fake();
+ $database = StandalonePostgresql::create([
+ 'name' => 'database-to-delete',
+ 'image' => 'postgres:17-alpine',
+ 'postgres_user' => 'postgres',
+ 'postgres_password' => 'password',
+ 'postgres_db' => 'postgres',
+ 'environment_id' => $this->environment->id,
+ 'destination_id' => $this->destination->id,
+ 'destination_type' => $this->destination->getMorphClass(),
+ ]);
+
+ $this->withHeaders($this->headers)
+ ->deleteJson("/api/v1/databases/{$database->uuid}")
+ ->assertOk();
+
+ expect(StandalonePostgresql::find($database->id))->toBeNull()
+ ->and(StandalonePostgresql::withTrashed()->find($database->id)?->trashed())->toBeTrue();
+ Queue::assertPushed(DeleteResourceJob::class);
+ });
+});
+
describe('POST /api/v1/applications/{uuid}/scheduled-tasks/{task_uuid}/execute', function () {
test('dispatches ScheduledTaskJob for an application task', function () {
Queue::fake();
diff --git a/tests/Feature/DeleteResourceJobAtomicityTest.php b/tests/Feature/DeleteResourceJobAtomicityTest.php
index 758227004d..801db44d14 100644
--- a/tests/Feature/DeleteResourceJobAtomicityTest.php
+++ b/tests/Feature/DeleteResourceJobAtomicityTest.php
@@ -10,6 +10,7 @@ use App\Models\ScheduledVolumeBackup;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
+use Illuminate\Foundation\Console\QueuedCommand;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process;
@@ -48,6 +49,7 @@ it('deletes the Coolify resource when remote cleanup fails', function () {
(new DeleteResourceJob($this->application))->handle();
expect(Application::withTrashed()->find($this->application->id))->toBeNull();
+ Queue::assertNotPushed(QueuedCommand::class);
});
it('rolls back local metadata deletion when deleting the resource fails', function () {
diff --git a/tests/Feature/ScheduleOnOneServerTest.php b/tests/Feature/ScheduleOnOneServerTest.php
index 49d5926892..ef2d80e648 100644
--- a/tests/Feature/ScheduleOnOneServerTest.php
+++ b/tests/Feature/ScheduleOnOneServerTest.php
@@ -58,3 +58,15 @@ it('does not schedule Stripe subscription reconciliation automatically', functio
expect($event)->toBeNull();
});
+
+it('schedules stuck resource cleanup once per day on one server', function () {
+ $schedule = app(Schedule::class);
+
+ $event = collect($schedule->events())->first(
+ fn ($event) => str_contains($event->command, 'cleanup:stucked-resources')
+ );
+
+ expect($event)->not->toBeNull()
+ ->and($event->expression)->toBe('0 0 * * *')
+ ->and($event->onOneServer)->toBeTrue();
+});