Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Servers/SuspensionService.php
10276 views
1
<?php
2
3
namespace Pterodactyl\Services\Servers;
4
5
use Webmozart\Assert\Assert;
6
use Pterodactyl\Models\Server;
7
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
8
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
9
10
class SuspensionService
11
{
12
public const ACTION_SUSPEND = 'suspend';
13
public const ACTION_UNSUSPEND = 'unsuspend';
14
15
/**
16
* SuspensionService constructor.
17
*/
18
public function __construct(
19
private DaemonServerRepository $daemonServerRepository,
20
) {
21
}
22
23
/**
24
* Suspends a server on the system.
25
*
26
* @throws \Throwable
27
*/
28
public function toggle(Server $server, string $action = self::ACTION_SUSPEND): void
29
{
30
Assert::oneOf($action, [self::ACTION_SUSPEND, self::ACTION_UNSUSPEND]);
31
32
$isSuspending = $action === self::ACTION_SUSPEND;
33
// Nothing needs to happen if we're suspending the server, and it is already
34
// suspended in the database. Additionally, nothing needs to happen if the server
35
// is not suspended, and we try to un-suspend the instance.
36
if ($isSuspending === $server->isSuspended()) {
37
return;
38
}
39
40
// Check if the server is currently being transferred.
41
if (!is_null($server->transfer)) {
42
throw new ConflictHttpException('Cannot toggle suspension status on a server that is currently being transferred.');
43
}
44
45
// Update the server's suspension status.
46
$server->update([
47
'status' => $isSuspending ? Server::STATUS_SUSPENDED : null,
48
]);
49
50
try {
51
// Tell wings to re-sync the server state.
52
$this->daemonServerRepository->setServer($server)->sync();
53
} catch (\Exception $exception) {
54
// Rollback the server's suspension status if wings fails to sync the server.
55
$server->update([
56
'status' => $isSuspending ? null : Server::STATUS_SUSPENDED,
57
]);
58
throw $exception;
59
}
60
}
61
}
62
63