@@ -9,6 +9,11 @@ class PlayersController < Api::V1::BaseController
99
1010 # GET /api/v1/players
1111 def index
12+ # Optimized query to prevent timeout during bulk sync operations
13+ # PostgreSQL will allow concurrent reads even during updates (MVCC)
14+ # Set a reasonable timeout to prevent 504s
15+ ActiveRecord ::Base . connection . execute ( "SET LOCAL statement_timeout = '5000'" ) # 5 seconds
16+
1217 players = organization_scoped ( Player ) . includes ( :champion_pools )
1318
1419 players = players . by_role ( params [ :role ] ) if params [ :role ] . present?
@@ -25,6 +30,13 @@ def index
2530 players : PlayerSerializer . render_as_hash ( result [ :data ] ) ,
2631 pagination : result [ :pagination ]
2732 } )
33+ rescue ActiveRecord ::QueryCanceled => e
34+ Rails . logger . error "Players index query timeout: #{ e . message } "
35+ render_error (
36+ message : 'Request timeout - please try again' ,
37+ code : 'QUERY_TIMEOUT' ,
38+ status : :request_timeout
39+ )
2840 end
2941
3042 # GET /api/v1/players/:id
@@ -165,8 +177,9 @@ def import
165177
166178 # POST /api/v1/players/:id/sync_from_riot
167179 def sync_from_riot
168- service = Players ::Services ::RiotSyncService . new ( @player , region : params [ :region ] )
169- result = service . sync
180+ region = params [ :region ] || @player . region || 'br1'
181+ service = Players ::Services ::RiotSyncService . new ( current_organization , region )
182+ result = service . sync_player ( @player , import_matches : true )
170183
171184 if result [ :success ]
172185 log_user_action (
0 commit comments