fix: service deletion job

This commit is contained in:
Andras Bacsai 2023-12-08 18:32:08 +01:00
parent 6a6275d4fa
commit 4e680deb93
6 changed files with 83 additions and 37 deletions

View File

@ -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();
}
}

View File

@ -4,7 +4,7 @@
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 @@ public function handle()
case '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;

View File

@ -65,6 +65,6 @@ public function stop(bool $forceCleanup = false)
} else {
$this->dispatch('success', 'Service stopped successfully.');
}
event(new ServiceStatusChanged());
ServiceStatusChanged::dispatch();
}
}

View File

@ -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';

View File

@ -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();
});
}
};

View File

@ -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 @@
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');