2017-07-23 19:51:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Servers;
|
|
|
|
|
2019-11-30 23:37:13 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2021-01-30 21:28:31 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
class SuspensionService
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
public const ACTION_SUSPEND = 'suspend';
|
|
|
|
public const ACTION_UNSUSPEND = 'unsuspend';
|
2018-01-20 19:48:02 +00:00
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* SuspensionService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private DaemonServerRepository $daemonServerRepository
|
2017-07-23 19:51:18 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suspends a server on the system.
|
|
|
|
*
|
2019-11-30 23:37:13 +00:00
|
|
|
* @throws \Throwable
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function toggle(Server $server, string $action = self::ACTION_SUSPEND): void
|
2017-07-23 19:51:18 +00:00
|
|
|
{
|
2019-11-30 23:37:13 +00:00
|
|
|
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2020-10-08 04:56:44 +00:00
|
|
|
$isSuspending = $action === self::ACTION_SUSPEND;
|
2022-10-14 16:59:20 +00:00
|
|
|
// Nothing needs to happen if we're suspending the server, and it is already
|
2020-10-08 04:56:44 +00:00
|
|
|
// suspended in the database. Additionally, nothing needs to happen if the server
|
2022-10-14 16:59:20 +00:00
|
|
|
// is not suspended, and we try to un-suspend the instance.
|
2021-01-17 23:51:56 +00:00
|
|
|
if ($isSuspending === $server->isSuspended()) {
|
2019-11-30 23:37:13 +00:00
|
|
|
return;
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:55:44 +00:00
|
|
|
// Check if the server is currently being transferred.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!is_null($server->transfer)) {
|
2021-01-30 21:28:31 +00:00
|
|
|
throw new ConflictHttpException('Cannot toggle suspension status on a server that is currently being transferred.');
|
2020-12-16 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 15:46:09 +00:00
|
|
|
// Update the server's suspension status.
|
|
|
|
$server->update([
|
|
|
|
'status' => $isSuspending ? Server::STATUS_SUSPENDED : null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Tell wings to re-sync the server state.
|
|
|
|
$this->daemonServerRepository->setServer($server)->sync();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// Rollback the server's suspension status if wings fails to sync the server.
|
2020-10-08 04:56:44 +00:00
|
|
|
$server->update([
|
2021-10-07 15:46:09 +00:00
|
|
|
'status' => $isSuspending ? null : Server::STATUS_SUSPENDED,
|
2019-11-30 23:37:13 +00:00
|
|
|
]);
|
2021-10-07 15:46:09 +00:00
|
|
|
throw $exception;
|
|
|
|
}
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
}
|