connection = $connection; $this->daemonServerRepository = $daemonServerRepository; } /** * Suspends a server on the system. * * @param \Pterodactyl\Models\Server $server * @param string $action * * @throws \Throwable */ public function toggle(Server $server, $action = self::ACTION_SUSPEND) { Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]); $isSuspending = $action === self::ACTION_SUSPEND; // Nothing needs to happen if we're suspending the server and it is already // suspended in the database. Additionally, nothing needs to happen if the server // is not suspended and we try to un-suspend the instance. if ($isSuspending === $server->suspended) { return; } // Check if the server is currently being transferred. if ($server->transfer !== null) { throw new ConflictHttpException('Server is currently being transferred'); } $this->connection->transaction(function () use ($action, $server) { $server->update([ 'suspended' => $action === self::ACTION_SUSPEND, ]); // Only send the suspension request to wings if the server is not currently being transferred. if ($server->transfer === null) { $this->daemonServerRepository->setServer($server)->suspend($action === self::ACTION_UNSUSPEND); } }); } }