lasthourcloud/app/Actions/Service/DeleteService.php

55 lines
1.8 KiB
PHP
Raw Normal View History

2023-12-08 17:32:08 +00:00
<?php
namespace App\Actions\Service;
use Lorisleiva\Actions\Concerns\AsAction;
use App\Models\Service;
class DeleteService
{
use AsAction;
public function handle(Service $service)
{
try {
$server = data_get($service, 'server');
if ($server->isFunctional()) {
$storagesToDelete = collect([]);
$service->environment_variables()->delete();
$commands = [];
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);
}
}
foreach ($storagesToDelete as $storage) {
$commands[] = "docker volume rm -f $storage->name";
}
$commands[] = "docker rm -f $service->uuid";
2023-12-08 17:32:08 +00:00
instant_remote_process($commands, $server, false);
2023-12-08 17:32:08 +00:00
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
} finally {
foreach ($service->applications()->get() as $application) {
$application->forceDelete();
2023-12-08 17:32:08 +00:00
}
foreach ($service->databases()->get() as $database) {
$database->forceDelete();
2024-01-29 15:03:45 +00:00
}
2024-02-14 14:14:06 +00:00
foreach ($service->scheduled_tasks as $task) {
$task->delete();
}
2024-02-02 10:50:28 +00:00
$service->tags()->detach();
2024-01-29 15:03:45 +00:00
}
2023-12-08 17:32:08 +00:00
}
}