Skip to content

Commit 7676f7d

Browse files
committed
Allow modification of server build settings even when node is offline
1 parent 28148f4 commit 7676f7d

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

app/Services/Servers/BuildModificationService.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Illuminate\Support\Arr;
66
use Pterodactyl\Models\Server;
77
use Pterodactyl\Models\Allocation;
8+
use Illuminate\Support\Facades\Log;
89
use Illuminate\Database\ConnectionInterface;
910
use Pterodactyl\Exceptions\DisplayException;
1011
use Illuminate\Database\Eloquent\ModelNotFoundException;
1112
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
13+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
1214

1315
class BuildModificationService
1416
{
@@ -78,10 +80,18 @@ public function handle(Server $server, array $data)
7880

7981
$updateData = $this->structureService->handle($server);
8082

83+
// Because Wings always fetches an updated configuration from the Panel when booting
84+
// a server this type of exception can be safely "ignored" and just written to the logs.
85+
// Ideally this request succeedes so we can apply resource modifications on the fly
86+
// but if it fails it isn't the end of the world.
8187
if (!empty($updateData['build'])) {
82-
$this->daemonServerRepository->setServer($server)->update([
83-
'build' => $updateData['build'],
84-
]);
88+
try {
89+
$this->daemonServerRepository->setServer($server)->update([
90+
'build' => $updateData['build'],
91+
]);
92+
} catch (DaemonConnectionException $exception) {
93+
Log::warning($exception, ['server_id' => $server->id]);
94+
}
8595
}
8696

8797
$this->connection->commit();

tests/Integration/Services/Servers/BuildModificationServiceTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Pterodactyl\Tests\Integration\Services\Servers;
44

55
use Mockery;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
68
use Pterodactyl\Models\Server;
79
use Pterodactyl\Models\Allocation;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Exception\TransferException;
812
use Pterodactyl\Exceptions\DisplayException;
913
use Pterodactyl\Tests\Integration\IntegrationTestCase;
1014
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
1115
use Pterodactyl\Services\Servers\BuildModificationService;
16+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
1217

1318
class BuildModificationServiceTest extends IntegrationTestCase
1419
{
@@ -149,6 +154,30 @@ public function testServerBuildDataIsProperlyUpdatedOnWings()
149154
$this->assertSame(20, $response->allocation_limit);
150155
}
151156

157+
/**
158+
* Test that an exception when connecting to the Wings instance is properly ignored
159+
* when making updates. This allows for a server to be modified even when the Wings
160+
* node is offline.
161+
*/
162+
public function testConnectionExceptionIsIgnoredWhenUpdatingServerSettings()
163+
{
164+
$server = $this->createServerModel();
165+
166+
$this->daemonServerRepository->expects('setServer->update')->andThrows(
167+
new DaemonConnectionException(
168+
new RequestException('Bad request', new Request('GET', '/test'), new Response())
169+
)
170+
);
171+
172+
$response = $this->getService()->handle($server, ['memory' => 256, 'disk' => 10240]);
173+
174+
$this->assertInstanceOf(Server::class, $response);
175+
$this->assertSame(256, $response->memory);
176+
$this->assertSame(10240, $response->disk);
177+
178+
$this->assertDatabaseHas('servers', ['id' => $response->id, 'memory' => 256, 'disk' => 10240]);
179+
}
180+
152181
/**
153182
* Test that no exception is thrown if we are only removing an allocation.
154183
*/
@@ -215,7 +244,9 @@ public function testUsingSameAllocationIdMultipleTimesDoesNotError()
215244

216245
/**
217246
* Test that any changes we made to the server or allocations are rolled back if there is an
218-
* exception while performing any action.
247+
* exception while performing any action. This is different than the connection exception
248+
* test which should properly ignore connection issues. We want any other type of exception
249+
* to properly be thrown back to the caller.
219250
*/
220251
public function testThatUpdatesAreRolledBackIfExceptionIsEncountered()
221252
{

0 commit comments

Comments
 (0)