1818use Pterodactyl \Services \Deployment \FindViableNodesService ;
1919use Pterodactyl \Repositories \Eloquent \ServerVariableRepository ;
2020use Pterodactyl \Services \Deployment \AllocationSelectionService ;
21+ use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
2122
2223class ServerCreationService
2324{
@@ -71,6 +72,11 @@ class ServerCreationService
7172 */
7273 private $ daemonServerRepository ;
7374
75+ /**
76+ * @var \Pterodactyl\Services\Servers\ServerDeletionService
77+ */
78+ private $ serverDeletionService ;
79+
7480 /**
7581 * CreationService constructor.
7682 *
@@ -81,6 +87,7 @@ class ServerCreationService
8187 * @param \Pterodactyl\Repositories\Eloquent\EggRepository $eggRepository
8288 * @param \Pterodactyl\Services\Deployment\FindViableNodesService $findViableNodesService
8389 * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService
90+ * @param \Pterodactyl\Services\Servers\ServerDeletionService $serverDeletionService
8491 * @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
8592 * @param \Pterodactyl\Repositories\Eloquent\ServerVariableRepository $serverVariableRepository
8693 * @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
@@ -93,6 +100,7 @@ public function __construct(
93100 EggRepository $ eggRepository ,
94101 FindViableNodesService $ findViableNodesService ,
95102 ServerConfigurationStructureService $ configurationStructureService ,
103+ ServerDeletionService $ serverDeletionService ,
96104 ServerRepository $ repository ,
97105 ServerVariableRepository $ serverVariableRepository ,
98106 VariableValidatorService $ validatorService
@@ -107,6 +115,7 @@ public function __construct(
107115 $ this ->repository = $ repository ;
108116 $ this ->serverVariableRepository = $ serverVariableRepository ;
109117 $ this ->daemonServerRepository = $ daemonServerRepository ;
118+ $ this ->serverDeletionService = $ serverDeletionService ;
110119 }
111120
112121 /**
@@ -157,14 +166,26 @@ public function handle(array $data, DeploymentObject $deployment = null): Server
157166
158167 // Create the server and assign any additional allocations to it.
159168 $ server = $ this ->createModel ($ data );
169+
160170 $ this ->storeAssignedAllocations ($ server , $ data );
161171 $ this ->storeEggVariables ($ server , $ eggVariableData );
162172
173+ // Due to the design of the Daemon, we need to persist this server to the disk
174+ // before we can actually create it on the Daemon.
175+ //
176+ // If that connection fails out we will attempt to perform a cleanup by just
177+ // deleting the server itself from the system.
178+ $ this ->connection ->commit ();
179+
163180 $ structure = $ this ->configurationStructureService ->handle ($ server );
164181
165- $ this -> connection -> transaction ( function () use ( $ server , $ structure ) {
182+ try {
166183 $ this ->daemonServerRepository ->setServer ($ server )->create ($ structure );
167- });
184+ } catch (DaemonConnectionException $ exception ) {
185+ $ this ->serverDeletionService ->withForce (true )->handle ($ server );
186+
187+ throw $ exception ;
188+ }
168189
169190 return $ server ;
170191 }
0 commit comments