mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-31 14:46:53 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Services\Servers;
 | |
| 
 | |
| use App\Enums\ServerState;
 | |
| use App\Enums\SuspendAction;
 | |
| use Filament\Notifications\Notification;
 | |
| use App\Models\Server;
 | |
| use App\Repositories\Daemon\DaemonServerRepository;
 | |
| use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
 | |
| 
 | |
| class SuspensionService
 | |
| {
 | |
|     /**
 | |
|      * SuspensionService constructor.
 | |
|      */
 | |
|     public function __construct(
 | |
|         private DaemonServerRepository $daemonServerRepository
 | |
|     ) {}
 | |
| 
 | |
|     /**
 | |
|      * Suspends a server on the system.
 | |
|      *
 | |
|      * @throws \Throwable
 | |
|      */
 | |
|     public function handle(Server $server, SuspendAction $action): void
 | |
|     {
 | |
|         $isSuspending = $action === SuspendAction::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->isSuspended()) {
 | |
|             Notification::make()->danger()->title('Failed!')->body('Server is already suspended!')->send();
 | |
| 
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         // Check if the server is currently being transferred.
 | |
|         if (!is_null($server->transfer)) {
 | |
|             Notification::make()->danger()->title('Failed!')->body('Server is currently being transferred.')->send();
 | |
|             throw new ConflictHttpException('Cannot toggle suspension status on a server that is currently being transferred.');
 | |
|         }
 | |
| 
 | |
|         // Update the server's suspension status.
 | |
|         $server->update([
 | |
|             'status' => $isSuspending ? ServerState::Suspended : null,
 | |
|         ]);
 | |
| 
 | |
|         // Tell daemon to re-sync the server state.
 | |
|         $this->daemonServerRepository->setServer($server)->sync();
 | |
|     }
 | |
| }
 | 
