fix: service deletion job
This commit is contained in:
parent
6a6275d4fa
commit
4e680deb93
42
app/Actions/Service/DeleteService.php
Normal file
42
app/Actions/Service/DeleteService.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ namespace App\Jobs;
|
|||||||
|
|
||||||
use App\Actions\Application\StopApplication;
|
use App\Actions\Application\StopApplication;
|
||||||
use App\Actions\Database\StopDatabase;
|
use App\Actions\Database\StopDatabase;
|
||||||
use App\Actions\Service\StopService;
|
use App\Actions\Service\DeleteService;
|
||||||
use App\Models\Application;
|
use App\Models\Application;
|
||||||
use App\Models\Service;
|
use App\Models\Service;
|
||||||
use App\Models\StandaloneMariadb;
|
use App\Models\StandaloneMariadb;
|
||||||
@ -54,11 +54,13 @@ class DeleteResourceJob implements ShouldQueue, ShouldBeEncrypted
|
|||||||
case 'standalone-mariadb':
|
case 'standalone-mariadb':
|
||||||
StopDatabase::run($this->resource);
|
StopDatabase::run($this->resource);
|
||||||
break;
|
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) {
|
} catch (\Throwable $e) {
|
||||||
send_internal_notification('ContainerStoppingJob failed with: ' . $e->getMessage());
|
send_internal_notification('ContainerStoppingJob failed with: ' . $e->getMessage());
|
||||||
throw $e;
|
throw $e;
|
||||||
|
@ -65,6 +65,6 @@ class Navbar extends Component
|
|||||||
} else {
|
} else {
|
||||||
$this->dispatch('success', 'Service stopped successfully.');
|
$this->dispatch('success', 'Service stopped successfully.');
|
||||||
}
|
}
|
||||||
event(new ServiceStatusChanged());
|
ServiceStatusChanged::dispatch();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,44 +2,17 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Actions\Service\DeleteService;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class Service extends BaseModel
|
class Service extends BaseModel
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory, SoftDeletes;
|
||||||
protected $guarded = [];
|
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()
|
public function type()
|
||||||
{
|
{
|
||||||
return 'service';
|
return 'service';
|
||||||
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Events\TestEvent;
|
||||||
use App\Http\Controllers\ApplicationController;
|
use App\Http\Controllers\ApplicationController;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Controllers\DatabaseController;
|
use App\Http\Controllers\DatabaseController;
|
||||||
@ -48,7 +49,7 @@ Route::get('/api/v1/test/realtime', function () {
|
|||||||
if (auth()->user()?->currentTeam()->id !== 0) {
|
if (auth()->user()?->currentTeam()->id !== 0) {
|
||||||
return redirect('/');
|
return redirect('/');
|
||||||
}
|
}
|
||||||
event(new \App\Events\TestEvent('asd'));
|
TestEvent::dispatch('asd');
|
||||||
return 'Look at your other tab.';
|
return 'Look at your other tab.';
|
||||||
})->middleware('auth');
|
})->middleware('auth');
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user