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;
|
2017-07-23 19:51:18 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2019-11-25 04:19:31 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
class SuspensionService
|
|
|
|
{
|
2018-01-20 19:48:02 +00:00
|
|
|
const ACTION_SUSPEND = 'suspend';
|
|
|
|
const ACTION_UNSUSPEND = 'unsuspend';
|
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2019-11-30 23:37:13 +00:00
|
|
|
private $connection;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2019-11-25 04:19:31 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonServerRepository
|
|
|
|
*/
|
|
|
|
private $daemonServerRepository;
|
2017-07-24 00:57:43 +00:00
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* SuspensionService constructor.
|
|
|
|
*
|
2019-11-30 23:37:13 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2019-11-25 04:19:31 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonServerRepository
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2019-11-30 23:37:13 +00:00
|
|
|
ConnectionInterface $connection,
|
2020-10-08 04:56:44 +00:00
|
|
|
DaemonServerRepository $daemonServerRepository
|
2017-07-23 19:51:18 +00:00
|
|
|
) {
|
2019-11-30 23:37:13 +00:00
|
|
|
$this->connection = $connection;
|
2019-11-25 04:19:31 +00:00
|
|
|
$this->daemonServerRepository = $daemonServerRepository;
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Suspends a server on the system.
|
|
|
|
*
|
2020-10-08 04:56:44 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param string $action
|
2017-07-23 19:51:18 +00:00
|
|
|
*
|
2019-11-30 23:37:13 +00:00
|
|
|
* @throws \Throwable
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
2019-11-30 23:37:13 +00:00
|
|
|
public function toggle(Server $server, $action = self::ACTION_SUSPEND)
|
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;
|
|
|
|
// 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) {
|
2019-11-30 23:37:13 +00:00
|
|
|
return;
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 23:37:13 +00:00
|
|
|
$this->connection->transaction(function () use ($action, $server) {
|
2020-10-08 04:56:44 +00:00
|
|
|
$server->update([
|
2019-11-30 23:37:13 +00:00
|
|
|
'suspended' => $action === self::ACTION_SUSPEND,
|
|
|
|
]);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2019-11-30 23:37:13 +00:00
|
|
|
});
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
}
|