Use BuildModificationService on EditServer (#1042)

* Use `BuildModificationService` on `EditServer` & make it throw if we can't reach wings

* Use Node name on `EditServer` & `EditNode`
This commit is contained in:
MartinOscar 2025-03-03 19:49:42 +01:00 committed by GitHub
parent d79d461e7c
commit 839be53231
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 3 deletions

View File

@ -599,7 +599,7 @@ class EditNode extends EditRecord
$this->errored = true;
Notification::make()
->title(trans('admin/node.error_connecting'))
->title(trans('admin/node.error_connecting', ['node' => $record->name]))
->body(trans('admin/node.error_connecting_description'))
->color('warning')
->icon('tabler-database')

View File

@ -19,6 +19,7 @@ use App\Models\ServerVariable;
use App\Models\User;
use App\Services\Databases\DatabaseManagementService;
use App\Services\Eggs\EggChangerService;
use App\Services\Servers\BuildModificationService;
use App\Services\Servers\RandomWordService;
use App\Services\Servers\ReinstallServerService;
use App\Services\Servers\ServerDeletionService;
@ -50,6 +51,7 @@ use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use LogicException;
use Webbingbrasil\FilamentCopyActions\Forms\Actions\CopyAction;
@ -58,6 +60,15 @@ class EditServer extends EditRecord
{
protected static string $resource = ServerResource::class;
private bool $errored = false;
private BuildModificationService $buildModificationService;
public function boot(BuildModificationService $buildModificationService): void
{
$this->buildModificationService = $buildModificationService;
}
public function form(Form $form): Form
{
return $form
@ -947,6 +958,40 @@ class EditServer extends EditRecord
return $data;
}
protected function handleRecordUpdate(Model $record, array $data): Model
{
if (!$record instanceof Server) {
return $record;
}
try {
$this->record = $this->buildModificationService->handle($record, $data, true);
return $this->record;
} catch (Exception $exception) {
$this->errored = true;
Notification::make()
->title(trans('admin/server.notifications.error_connecting', ['node' => $record->node->name]))
->body(trans('admin/server.notifications.error_connecting_description'))
->color('warning')
->icon('tabler-database')
->warning()
->send();
return parent::handleRecordUpdate($record, $data);
}
}
protected function getSavedNotification(): ?Notification
{
if ($this->errored) {
return null;
}
return parent::getSavedNotification();
}
public function getRelationManagers(): array
{
return [

View File

@ -8,6 +8,7 @@ use App\Models\Allocation;
use Illuminate\Database\ConnectionInterface;
use App\Exceptions\DisplayException;
use App\Repositories\Daemon\DaemonServerRepository;
use Exception;
use Illuminate\Http\Client\ConnectionException;
class BuildModificationService
@ -27,7 +28,7 @@ class BuildModificationService
* @throws \Throwable
* @throws \App\Exceptions\DisplayException
*/
public function handle(Server $server, array $data): Server
public function handle(Server $server, array $data, ?bool $shouldThrow = false): Server
{
/** @var \App\Models\Server $server */
$server = $this->connection->transaction(function () use ($server, $data) {
@ -66,6 +67,10 @@ class BuildModificationService
$this->daemonServerRepository->setServer($server)->sync();
} catch (ConnectionException $exception) {
logger()->warning($exception, ['server_id' => $server->id]);
if ($shouldThrow) {
throw $exception;
}
}
}

View File

@ -103,6 +103,6 @@ return [
'databases' => 'Databases',
'backups' => 'Backups',
'error_connecting' => 'Error connecting to the node',
'error_connecting' => 'Error connecting to :node',
'error_connecting_description' => 'The configuration could not be automatically updated on Wings, you will need to manually update the configuration file.',
];

View File

@ -117,5 +117,7 @@ return [
'invalid_port_body' => ':i is not in the valid port range between :portFloor-:portCeil',
'already_exists' => 'Port already in use',
'already_exists_body' => ':i is already with an allocation',
'error_connecting' => 'Error connecting to :node',
'error_connecting_description' => 'The configuration could not be automatically synced on Wings, you will need to manually restart the server.',
],
];