Skip to content

Commit ed50259

Browse files
committed
[#3896cn] Clean up code handling server suspension
1 parent 2eee6f3 commit ed50259

File tree

3 files changed

+45
-54
lines changed

3 files changed

+45
-54
lines changed

app/Http/Controllers/Api/Application/Servers/ServerManagementController.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ public function __construct(
5050
* Suspend a server on the Panel.
5151
*
5252
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
53+
* @param \Pterodactyl\Models\Server $server
5354
* @return \Illuminate\Http\Response
5455
*
5556
* @throws \Pterodactyl\Exceptions\DisplayException
5657
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5758
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5859
*/
59-
public function suspend(ServerWriteRequest $request): Response
60+
public function suspend(ServerWriteRequest $request, Server $server): Response
6061
{
61-
$this->suspensionService->toggle($request->getModel(Server::class), SuspensionService::ACTION_SUSPEND);
62+
$this->suspensionService->toggle($server, SuspensionService::ACTION_SUSPEND);
6263

6364
return $this->returnNoContent();
6465
}
@@ -67,15 +68,16 @@ public function suspend(ServerWriteRequest $request): Response
6768
* Unsuspend a server on the Panel.
6869
*
6970
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
71+
* @param \Pterodactyl\Models\Server $server
7072
* @return \Illuminate\Http\Response
7173
*
7274
* @throws \Pterodactyl\Exceptions\DisplayException
7375
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
7476
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
7577
*/
76-
public function unsuspend(ServerWriteRequest $request): Response
78+
public function unsuspend(ServerWriteRequest $request, Server $server): Response
7779
{
78-
$this->suspensionService->toggle($request->getModel(Server::class), SuspensionService::ACTION_UNSUSPEND);
80+
$this->suspensionService->toggle($server, SuspensionService::ACTION_UNSUSPEND);
7981

8082
return $this->returnNoContent();
8183
}
@@ -84,15 +86,16 @@ public function unsuspend(ServerWriteRequest $request): Response
8486
* Mark a server as needing to be reinstalled.
8587
*
8688
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
89+
* @param \Pterodactyl\Models\Server $server
8790
* @return \Illuminate\Http\Response
8891
*
8992
* @throws \Pterodactyl\Exceptions\DisplayException
9093
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
9194
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
9295
*/
93-
public function reinstall(ServerWriteRequest $request): Response
96+
public function reinstall(ServerWriteRequest $request, Server $server): Response
9497
{
95-
$this->reinstallServerService->reinstall($request->getModel(Server::class));
98+
$this->reinstallServerService->reinstall($server);
9699

97100
return $this->returnNoContent();
98101
}
@@ -101,13 +104,14 @@ public function reinstall(ServerWriteRequest $request): Response
101104
* Mark a server as needing its container rebuilt the next time it is started.
102105
*
103106
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest $request
107+
* @param \Pterodactyl\Models\Server $server
104108
* @return \Illuminate\Http\Response
105109
*
106110
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
107111
*/
108-
public function rebuild(ServerWriteRequest $request): Response
112+
public function rebuild(ServerWriteRequest $request, Server $server): Response
109113
{
110-
$this->rebuildService->handle($request->getModel(Server::class));
114+
$this->rebuildService->handle($server);
111115

112116
return $this->returnNoContent();
113117
}

app/Repositories/Wings/DaemonServerRepository.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,25 @@ public function reinstall(): void
8787
throw new BadMethodCallException('Method is not implemented.');
8888
}
8989

90-
public function suspend(): void
90+
/**
91+
* By default this function will suspend a server instance on the daemon. However, passing
92+
* "true" as the first argument will unsuspend the server.
93+
*
94+
* @param bool $unsuspend
95+
*
96+
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
97+
*/
98+
public function suspend(bool $unsuspend = false): void
9199
{
92-
throw new BadMethodCallException('Method is not implemented.');
93-
}
100+
Assert::isInstanceOf($this->server, Server::class);
94101

95-
public function unsuspend(): void
96-
{
97-
throw new BadMethodCallException('Method is not implemented.');
102+
try {
103+
$this->getHttpClient()->patch(
104+
'/api/servers/' . $this->server->uuid,
105+
['json' => ['suspended' => ! $unsuspend]]
106+
);
107+
} catch (TransferException $exception) {
108+
throw new DaemonConnectionException($exception);
109+
}
98110
}
99111
}

app/Services/Servers/SuspensionService.php

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace Pterodactyl\Services\Servers;
44

55
use Psr\Log\LoggerInterface;
6-
use InvalidArgumentException;
6+
use Webmozart\Assert\Assert;
77
use Pterodactyl\Models\Server;
8-
use GuzzleHttp\Exception\RequestException;
98
use Illuminate\Database\ConnectionInterface;
10-
use Pterodactyl\Exceptions\DisplayException;
119
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
1210
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
1311

@@ -19,7 +17,7 @@ class SuspensionService
1917
/**
2018
* @var \Illuminate\Database\ConnectionInterface
2119
*/
22-
private $database;
20+
private $connection;
2321

2422
/**
2523
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
@@ -39,18 +37,18 @@ class SuspensionService
3937
/**
4038
* SuspensionService constructor.
4139
*
42-
* @param \Illuminate\Database\ConnectionInterface $database
40+
* @param \Illuminate\Database\ConnectionInterface $connection
4341
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
4442
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
4543
* @param \Psr\Log\LoggerInterface $writer
4644
*/
4745
public function __construct(
48-
ConnectionInterface $database,
46+
ConnectionInterface $connection,
4947
DaemonServerRepository $daemonServerRepository,
5048
ServerRepositoryInterface $repository,
5149
LoggerInterface $writer
5250
) {
53-
$this->database = $database;
51+
$this->connection = $connection;
5452
$this->repository = $repository;
5553
$this->writer = $writer;
5654
$this->daemonServerRepository = $daemonServerRepository;
@@ -61,49 +59,26 @@ public function __construct(
6159
*
6260
* @param int|\Pterodactyl\Models\Server $server
6361
* @param string $action
64-
* @return bool
6562
*
66-
* @throws \Pterodactyl\Exceptions\DisplayException
67-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
68-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
63+
* @throws \Throwable
6964
*/
70-
public function toggle($server, $action = self::ACTION_SUSPEND)
65+
public function toggle(Server $server, $action = self::ACTION_SUSPEND)
7166
{
72-
if (! $server instanceof Server) {
73-
$server = $this->repository->find($server);
74-
}
75-
76-
if (! in_array($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND])) {
77-
throw new InvalidArgumentException(sprintf(
78-
'Action must be either ' . self::ACTION_SUSPEND . ' or ' . self::ACTION_UNSUSPEND . ', %s passed.',
79-
$action
80-
));
81-
}
67+
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);
8268

8369
if (
8470
$action === self::ACTION_SUSPEND && $server->suspended ||
8571
$action === self::ACTION_UNSUSPEND && ! $server->suspended
8672
) {
87-
return true;
73+
return;
8874
}
8975

90-
$this->database->beginTransaction();
91-
$this->repository->withoutFreshModel()->update($server->id, [
92-
'suspended' => $action === self::ACTION_SUSPEND,
93-
]);
76+
$this->connection->transaction(function () use ($action, $server) {
77+
$this->repository->withoutFreshModel()->update($server->id, [
78+
'suspended' => $action === self::ACTION_SUSPEND,
79+
]);
9480

95-
try {
96-
$this->daemonServerRepository->setServer($server)->$action();
97-
$this->database->commit();
98-
99-
return true;
100-
} catch (RequestException $exception) {
101-
$response = $exception->getResponse();
102-
$this->writer->warning($exception);
103-
104-
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
105-
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
106-
]));
107-
}
81+
$this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND);
82+
});
10883
}
10984
}

0 commit comments

Comments
 (0)