44
55use Illuminate \Http \Request ;
66use Pterodactyl \Models \Server ;
7- use Pterodactyl \Models \Backup ;
8- use Pterodactyl \Models \AuditLog ;
97use Illuminate \Http \JsonResponse ;
108use Pterodactyl \Facades \Activity ;
11- use Illuminate \Database \Query \Builder ;
12- use Illuminate \Database \Query \JoinClause ;
139use Pterodactyl \Http \Controllers \Controller ;
14- use Pterodactyl \ Repositories \ Eloquent \ NodeRepository ;
10+ use Illuminate \ Database \ ConnectionInterface ;
1511use Pterodactyl \Services \Eggs \EggConfigurationService ;
1612use Pterodactyl \Repositories \Eloquent \ServerRepository ;
1713use Pterodactyl \Http \Resources \Wings \ServerConfigurationCollection ;
1814use Pterodactyl \Services \Servers \ServerConfigurationStructureService ;
1915
2016class ServerDetailsController extends Controller
2117{
18+ /**
19+ * @var \Illuminate\Database\ConnectionInterface
20+ */
21+ protected ConnectionInterface $ connection ;
22+
2223 /**
2324 * @var \Pterodactyl\Services\Eggs\EggConfigurationService
2425 */
@@ -38,14 +39,15 @@ class ServerDetailsController extends Controller
3839 * ServerConfigurationController constructor.
3940 */
4041 public function __construct (
42+ ConnectionInterface $ connection ,
4143 ServerRepository $ repository ,
4244 ServerConfigurationStructureService $ configurationStructureService ,
43- EggConfigurationService $ eggConfigurationService ,
44- NodeRepository $ nodeRepository
45+ EggConfigurationService $ eggConfigurationService
4546 ) {
4647 $ this ->eggConfigurationService = $ eggConfigurationService ;
4748 $ this ->repository = $ repository ;
4849 $ this ->configurationStructureService = $ configurationStructureService ;
50+ $ this ->connection = $ connection ;
4951 }
5052
5153 /**
@@ -110,45 +112,38 @@ public function resetState(Request $request)
110112 // For each of those servers we'll track a new audit log entry to mark them as
111113 // failed and then update them all to be in a valid state.
112114 $ servers = Server::query ()
113- ->select ('servers.* ' )
114- ->selectRaw ('JSON_UNQUOTE(JSON_EXTRACT(started.metadata, "$.backup_uuid")) as backup_uuid ' )
115- ->leftJoinSub (function (Builder $ builder ) {
116- $ builder ->select ('* ' )->from ('audit_logs ' )
117- ->where ('action ' , AuditLog::SERVER__BACKUP_RESTORE_STARTED )
118- ->orderByDesc ('created_at ' )
119- ->limit (1 );
120- }, 'started ' , 'started.server_id ' , '= ' , 'servers.id ' )
121- ->leftJoin ('audit_logs as completed ' , function (JoinClause $ clause ) {
122- $ clause ->whereColumn ('completed.created_at ' , '> ' , 'started.created_at ' )
123- ->whereIn ('completed.action ' , [
124- AuditLog::SERVER__BACKUP_RESTORE_COMPLETED ,
125- AuditLog::SERVER__BACKUP_RESTORE_FAILED ,
126- ]);
127- })
128- ->whereNotNull ('started.id ' )
129- ->whereNull ('completed.id ' )
130- ->where ('servers.node_id ' , $ node ->id )
131- ->where ('servers.status ' , Server::STATUS_RESTORING_BACKUP )
115+ ->with ([
116+ 'activity ' => fn ($ builder ) => $ builder
117+ ->where ('activity_logs.event ' , 'server:backup.restore-started ' )
118+ ->latest ('timestamp ' ),
119+ ])
120+ ->where ('node_id ' , $ node ->id )
121+ ->where ('status ' , Server::STATUS_RESTORING_BACKUP )
132122 ->get ();
133123
134- $ backups = Backup::query ()->whereIn ('uuid ' , $ servers ->pluck ('backup_uuid ' ))->get ();
135-
136- /** @var \Pterodactyl\Models\Server $server */
137- foreach ($ servers as $ server ) {
138- $ server ->update (['status ' => null ]);
139-
140- if ($ backup = $ backups ->where ('uuid ' , $ server ->getAttribute ('backup_uuid ' ))->first ()) {
141- // Just create a new audit entry for this event and update the server state
142- // so that power actions, file management, and backups can resume as normal.
143- Activity::event ('server:backup.restore-failed ' )->subject ($ server , $ backup )->log ();
124+ $ this ->connection ->transaction (function () use ($ node , $ servers ) {
125+ /** @var \Pterodactyl\Models\Server $server */
126+ foreach ($ servers as $ server ) {
127+ /** @var \Pterodactyl\Models\ActivityLog|null $activity */
128+ $ activity = $ server ->activity ->first ();
129+ if (!is_null ($ activity )) {
130+ if ($ subject = $ activity ->subjects ->where ('subject_type ' , 'backup ' )->first ()) {
131+ // Just create a new audit entry for this event and update the server state
132+ // so that power actions, file management, and backups can resume as normal.
133+ Activity::event ('server:backup.restore-failed ' )
134+ ->subject ($ server , $ subject ->subject )
135+ ->property ('name ' , $ subject ->subject ->name )
136+ ->log ();
137+ }
138+ }
144139 }
145- }
146140
147- // Update any server marked as installing or restoring as being in a normal state
148- // at this point in the process.
149- Server::query ()->where ('node_id ' , $ node ->id )
150- ->whereIn ('status ' , [Server::STATUS_INSTALLING , Server::STATUS_RESTORING_BACKUP ])
151- ->update (['status ' => null ]);
141+ // Update any server marked as installing or restoring as being in a normal state
142+ // at this point in the process.
143+ Server::query ()->where ('node_id ' , $ node ->id )
144+ ->whereIn ('status ' , [Server::STATUS_INSTALLING , Server::STATUS_RESTORING_BACKUP ])
145+ ->update (['status ' => null ]);
146+ });
152147
153148 return new JsonResponse ([], JsonResponse::HTTP_NO_CONTENT );
154149 }
0 commit comments