From d8d1eacb42019228032e798c417a89ca53cc89f4 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 29 Aug 2021 13:19:24 -0700 Subject: [PATCH] Don't require Wings API call to pass in order to update server details --- .../Servers/BuildModificationService.php | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php index 9a8c1c3c4..f93ad21e5 100644 --- a/app/Services/Servers/BuildModificationService.php +++ b/app/Services/Servers/BuildModificationService.php @@ -33,6 +33,8 @@ class BuildModificationService * BuildModificationService constructor. * * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $structureService + * @param \Illuminate\Database\ConnectionInterface $connection + * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository */ public function __construct( ServerConfigurationStructureService $structureService, @@ -56,34 +58,37 @@ class BuildModificationService { $this->connection->beginTransaction(); - $this->processAllocations($server, $data); + /** @var \Pterodactyl\Models\Server $server */ + $server = $this->connection->transaction(function() use ($server, $data) { + $this->processAllocations($server, $data); - if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) { - try { - Allocation::query()->where('id', $data['allocation_id'])->where('server_id', $server->id)->firstOrFail(); - } catch (ModelNotFoundException $ex) { - throw new DisplayException('The requested default allocation is not currently assigned to this server.'); + if (isset($data['allocation_id']) && $data['allocation_id'] != $server->allocation_id) { + try { + Allocation::query()->where('id', $data['allocation_id'])->where('server_id', $server->id)->firstOrFail(); + } catch (ModelNotFoundException $ex) { + throw new DisplayException('The requested default allocation is not currently assigned to this server.'); + } } - } - // If any of these values are passed through in the data array go ahead and set - // them correctly on the server model. - $merge = Arr::only($data, ['oom_disabled', 'memory', 'swap', 'io', 'cpu', 'threads', 'disk', 'allocation_id']); + // If any of these values are passed through in the data array go ahead and set + // them correctly on the server model. + $merge = Arr::only($data, ['oom_disabled', 'memory', 'swap', 'io', 'cpu', 'threads', 'disk', 'allocation_id']); - $server->forceFill(array_merge($merge, [ - 'database_limit' => Arr::get($data, 'database_limit', 0) ?? null, - 'allocation_limit' => Arr::get($data, 'allocation_limit', 0) ?? null, - 'backup_limit' => Arr::get($data, 'backup_limit', 0) ?? 0, - ]))->saveOrFail(); + $server->forceFill(array_merge($merge, [ + 'database_limit' => Arr::get($data, 'database_limit', 0) ?? null, + 'allocation_limit' => Arr::get($data, 'allocation_limit', 0) ?? null, + 'backup_limit' => Arr::get($data, 'backup_limit', 0) ?? 0, + ]))->saveOrFail(); - $server = $server->fresh(); + return $server->refresh(); + }); $updateData = $this->structureService->handle($server); // Because Wings always fetches an updated configuration from the Panel when booting // a server this type of exception can be safely "ignored" and just written to the logs. - // Ideally this request succeedes so we can apply resource modifications on the fly - // but if it fails it isn't the end of the world. + // Ideally this request succeedes so we can apply resource modifications on the fly, but + // if it fails we can just continue on as normal. if (!empty($updateData['build'])) { try { $this->daemonServerRepository->setServer($server)->update([ @@ -94,8 +99,6 @@ class BuildModificationService } } - $this->connection->commit(); - return $server; }