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
fix: service deletion job
Andras Bacsai4e680deb93d4
diff --git a/app/Actions/Service/DeleteService.php b/app/Actions/Service/DeleteService.phpnew file mode 100644index 0000000000..e7cc049b12--- /dev/null+++ b/app/Actions/Service/DeleteService.php@@ -0,0 +1,42 @@+<?php++namespace App\Actions\Service;++use Lorisleiva\Actions\Concerns\AsAction;+use App\Models\Service;++class DeleteService+{+ use AsAction;+ public function handle(Service $service)+ {+ StopService::run($service);+ $server = data_get($service, 'server');+ $storagesToDelete = collect([]);++ $service->environment_variables()->delete();+ $commands = [];+ foreach ($service->applications()->get() as $application) {+ $storages = $application->persistentStorages()->get();+ foreach ($storages as $storage) {+ $storagesToDelete->push($storage);+ }+ $application->delete();+ }+ foreach ($service->databases()->get() as $database) {+ $storages = $database->persistentStorages()->get();+ foreach ($storages as $storage) {+ $storagesToDelete->push($storage);+ }+ $database->delete();+ }+ foreach ($storagesToDelete as $storage) {+ $commands[] = "docker volume rm -f $storage->name";+ }+ $commands[] = "docker rm -f $service->uuid";++ instant_remote_process($commands, $server, false);++ $service->forceDelete();+ }+}diff --git a/app/Jobs/DeleteResourceJob.php b/app/Jobs/DeleteResourceJob.phpindex 0393d9f56e..f8df38a2a4 100644--- a/app/Jobs/DeleteResourceJob.php+++ b/app/Jobs/DeleteResourceJob.php@@ -4,7 +4,7 @@ namespace App\Jobs;use App\Actions\Application\StopApplication;use App\Actions\Database\StopDatabase;-use App\Actions\Service\StopService;+use App\Actions\Service\DeleteService;use App\Models\Application;use App\Models\Service;use App\Models\StandaloneMariadb;@@ -54,11 +54,13 @@ class DeleteResourceJob implements ShouldQueue, ShouldBeEncryptedcase 'standalone-mariadb':StopDatabase::run($this->resource);break;- case 'service':- StopService::run($this->resource);- break;}- $this->resource->delete();+ if ($this->resource->type() === 'service') {+ $this->resource->delete();+ DeleteService::dispatch($this->resource);+ } else {+ $this->resource->delete();+ }} catch (\Throwable $e) {send_internal_notification('ContainerStoppingJob failed with: ' . $e->getMessage());throw $e;diff --git a/app/Livewire/Project/Service/Navbar.php b/app/Livewire/Project/Service/Navbar.phpindex d8d746a3e7..71ec4e702b 100644--- a/app/Livewire/Project/Service/Navbar.php+++ b/app/Livewire/Project/Service/Navbar.php@@ -65,6 +65,6 @@ class Navbar extends Component} else {$this->dispatch('success', 'Service stopped successfully.');}- event(new ServiceStatusChanged());+ ServiceStatusChanged::dispatch();}}diff --git a/app/Models/Service.php b/app/Models/Service.phpindex b7fd6b07e2..eb0d966704 100644--- a/app/Models/Service.php+++ b/app/Models/Service.php@@ -2,44 +2,17 @@namespace App\Models;+use App\Actions\Service\DeleteService;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Relations\HasMany;+use Illuminate\Database\Eloquent\SoftDeletes;use Illuminate\Support\Collection;use Illuminate\Support\Str;class Service extends BaseModel{- use HasFactory;+ use HasFactory, SoftDeletes;protected $guarded = [];-- protected static function booted()- {- static::deleting(function ($service) {- $storagesToDelete = collect([]);- foreach ($service->applications()->get() as $application) {- $storages = $application->persistentStorages()->get();- foreach ($storages as $storage) {- $storagesToDelete->push($storage);- }- }- foreach ($service->databases()->get() as $database) {- $storages = $database->persistentStorages()->get();- foreach ($storages as $storage) {- $storagesToDelete->push($storage);- }- }- $service->environment_variables()->delete();- $service->applications()->delete();- $service->databases()->delete();-- $server = data_get($service, 'server');- if ($server && $storagesToDelete->count() > 0) {- $storagesToDelete->each(function ($storage) use ($server) {- instant_remote_process(["docker volume rm -f $storage->name"], $server, false);- });- }- });- }public function type(){return 'service';diff --git a/database/migrations/2023_12_08_162228_add_soft_delete_services.php b/database/migrations/2023_12_08_162228_add_soft_delete_services.phpnew file mode 100644index 0000000000..c45ecf5bf1--- /dev/null+++ b/database/migrations/2023_12_08_162228_add_soft_delete_services.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('services', function (Blueprint $table) {+ $table->softDeletes();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('services', function (Blueprint $table) {+ $table->dropSoftDeletes();+ });+ }+};diff --git a/routes/web.php b/routes/web.phpindex 1d46c4a834..dba77238d5 100644--- a/routes/web.php+++ b/routes/web.php@@ -1,5 +1,6 @@<?php+use App\Events\TestEvent;use App\Http\Controllers\ApplicationController;use App\Http\Controllers\Controller;use App\Http\Controllers\DatabaseController;@@ -48,7 +49,7 @@ Route::get('/api/v1/test/realtime', function () {if (auth()->user()?->currentTeam()->id !== 0) {return redirect('/');}- event(new \App\Events\TestEvent('asd'));+ TestEvent::dispatch('asd');return 'Look at your other tab.';})->middleware('auth');