Path: blob/1.0-develop/app/Services/Servers/SuspensionService.php
10276 views
<?php12namespace Pterodactyl\Services\Servers;34use Webmozart\Assert\Assert;5use Pterodactyl\Models\Server;6use Pterodactyl\Repositories\Wings\DaemonServerRepository;7use Symfony\Component\HttpKernel\Exception\ConflictHttpException;89class SuspensionService10{11public const ACTION_SUSPEND = 'suspend';12public const ACTION_UNSUSPEND = 'unsuspend';1314/**15* SuspensionService constructor.16*/17public function __construct(18private DaemonServerRepository $daemonServerRepository,19) {20}2122/**23* Suspends a server on the system.24*25* @throws \Throwable26*/27public function toggle(Server $server, string $action = self::ACTION_SUSPEND): void28{29Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);3031$isSuspending = $action === self::ACTION_SUSPEND;32// Nothing needs to happen if we're suspending the server, and it is already33// suspended in the database. Additionally, nothing needs to happen if the server34// is not suspended, and we try to un-suspend the instance.35if ($isSuspending === $server->isSuspended()) {36return;37}3839// Check if the server is currently being transferred.40if (!is_null($server->transfer)) {41throw new ConflictHttpException('Cannot toggle suspension status on a server that is currently being transferred.');42}4344// Update the server's suspension status.45$server->update([46'status' => $isSuspending ? Server::STATUS_SUSPENDED : null,47]);4849try {50// Tell wings to re-sync the server state.51$this->daemonServerRepository->setServer($server)->sync();52} catch (\Exception $exception) {53// Rollback the server's suspension status if wings fails to sync the server.54$server->update([55'status' => $isSuspending ? null : Server::STATUS_SUSPENDED,56]);57throw $exception;58}59}60}616263