Skip to content

Commit 5c77f53

Browse files
committed
fix: solve sidekiq organization permission issue
1 parent a077be8 commit 5c77f53

7 files changed

Lines changed: 43 additions & 19 deletions

File tree

app/modules/matches/jobs/sync_match_job.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class SyncMatchJob < ApplicationJob
1010
# retry_on RiotApiService::RiotApiError, wait: 1.minute, attempts: 3
1111

1212
def perform(match_id, organization_id, region = 'BR', force_update = false)
13+
# Set organization context for multi-tenant scoping
14+
Current.organization_id = organization_id
15+
1316
puts "SyncMatchJob: Starting sync for #{match_id} (force_update: #{force_update})"
1417
$stdout.flush
1518
organization = Organization.find(organization_id)
@@ -53,6 +56,9 @@ def perform(match_id, organization_id, region = 'BR', force_update = false)
5356
rescue StandardError => e
5457
Rails.logger.error("Failed to sync match #{match_id}: #{e.message}")
5558
raise
59+
ensure
60+
# Clean up context
61+
Current.organization_id = nil
5662
end
5763

5864
private

app/modules/players/controllers/players_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def bulk_sync
266266

267267
begin
268268
players.each do |player|
269-
SyncPlayerFromRiotJob.perform_later(player.id)
269+
SyncPlayerFromRiotJob.perform_later(player.id, current_organization.id)
270270
end
271271

272272
render_success({

app/modules/players/jobs/sync_player_from_riot_job.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ module Players
66
class SyncPlayerFromRiotJob < ApplicationJob
77
queue_as :default
88

9-
def perform(player_id)
9+
def perform(player_id, organization_id)
10+
# Set organization context for multi-tenant scoping
11+
Current.organization_id = organization_id
12+
1013
player = Player.find(player_id)
1114
riot_api_key = ENV['RIOT_API_KEY']
1215

1316
return mark_error!(player, "Player #{player_id} missing Riot info") unless riot_info_present?(player)
1417
return mark_error!(player, 'Riot API key not configured') unless riot_api_key.present?
1518

1619
sync_player_from_riot!(player, riot_api_key)
20+
ensure
21+
# Clean up context
22+
Current.organization_id = nil
1723
end
1824

1925
private

app/modules/players/jobs/sync_player_job.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class SyncPlayerJob < ApplicationJob
1010
retry_on RiotApiService::RateLimitError, wait: :polynomially_longer, attempts: 5
1111
retry_on RiotApiService::RiotApiError, wait: 1.minute, attempts: 3
1212

13-
def perform(player_id, region = 'BR')
13+
def perform(player_id, organization_id, region = 'BR')
14+
# Set organization context for multi-tenant scoping
15+
Current.organization_id = organization_id
16+
1417
player = Player.find(player_id)
1518
riot_service = RiotApiService.new
1619

@@ -32,6 +35,9 @@ def perform(player_id, region = 'BR')
3235
rescue StandardError => e
3336
Rails.logger.error("Failed to sync player #{player.id}: #{e.message}")
3437
raise
38+
ensure
39+
# Clean up context
40+
Current.organization_id = nil
3541
end
3642

3743
private

app/modules/scouting/jobs/sync_scouting_target_job.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class SyncScoutingTargetJob < ApplicationJob
1111
retry_on RiotApiService::RateLimitError, wait: :polynomially_longer, attempts: 5
1212
retry_on RiotApiService::RiotApiError, wait: 1.minute, attempts: 3
1313

14-
def perform(scouting_target_id)
14+
def perform(scouting_target_id, organization_id)
15+
# Set organization context for multi-tenant scoping
16+
Current.organization_id = organization_id
17+
1518
target = ScoutingTarget.find(scouting_target_id)
1619
riot_service = RiotApiService.new
1720

@@ -27,6 +30,9 @@ def perform(scouting_target_id)
2730
rescue StandardError => e
2831
Rails.logger.error("Failed to sync scouting target #{target.id}: #{e.message}")
2932
raise
33+
ensure
34+
# Clean up context
35+
Current.organization_id = nil
3036
end
3137

3238
private

lib/tasks/riot.rake

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,23 @@ namespace :riot do
9898
task sync_all_players: :environment do
9999
puts '🔄 Syncing all active players from Riot API...'
100100

101-
Player.active.find_each do |player|
102-
puts " Syncing #{player.summoner_name} (#{player.id})..."
103-
SyncPlayerFromRiotJob.perform_later(player.id)
101+
Player.unscoped_by_organization.active.find_each do |player|
102+
puts " Syncing #{player.summoner_name} (#{player.id}) from org #{player.organization_id}..."
103+
SyncPlayerFromRiotJob.perform_later(player.id, player.organization_id)
104104
end
105105

106-
puts "✅ Queued #{Player.active.count} players for sync!"
106+
puts "✅ Queued #{Player.unscoped_by_organization.active.count} players for sync!"
107107
end
108108

109109
desc 'Sync all scouting targets from Riot API'
110110
task sync_all_scouting_targets: :environment do
111111
puts '🔄 Syncing all scouting targets from Riot API...'
112112

113-
ScoutingTarget.find_each do |target|
114-
puts " Syncing #{target.summoner_name} (#{target.id})..."
115-
SyncScoutingTargetJob.perform_later(target.id)
113+
ScoutingTarget.unscoped_by_organization.find_each do |target|
114+
puts " Syncing #{target.summoner_name} (#{target.id}) from org #{target.organization_id}..."
115+
SyncScoutingTargetJob.perform_later(target.id, target.organization_id)
116116
end
117117

118-
puts "✅ Queued #{ScoutingTarget.count} scouting targets for sync!"
118+
puts "✅ Queued #{ScoutingTarget.unscoped_by_organization.count} scouting targets for sync!"
119119
end
120120
end

spec/jobs/sync_player_from_riot_job_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
expect(Rails.logger).to receive(:error).with("Player #{player_no_info.id} missing Riot info")
2222

23-
described_class.new.perform(player_no_info.id)
23+
described_class.new.perform(player_no_info.id, organization.id)
2424

2525
player_no_info.reload
2626
expect(player_no_info.sync_status).to eq('error')
@@ -34,7 +34,7 @@
3434

3535
expect(Rails.logger).to receive(:error).with('Riot API key not configured')
3636

37-
described_class.new.perform(player.id)
37+
described_class.new.perform(player.id, organization.id)
3838

3939
player.reload
4040
expect(player.sync_status).to eq('error')
@@ -82,7 +82,7 @@
8282

8383
expect(Rails.logger).to receive(:info).with("Successfully synced player #{player.id} from Riot API")
8484

85-
job.perform(player.id)
85+
job.perform(player.id, organization.id)
8686

8787
player.reload
8888
expect(player.riot_puuid).to eq('test-puuid-123')
@@ -113,7 +113,7 @@
113113

114114
allow(job).to receive(:fetch_ranked_stats).and_return(ranked_data)
115115

116-
job.perform(player.id)
116+
job.perform(player.id, organization.id)
117117
end
118118

119119
it 'defaults to BR1 when region is not set' do
@@ -128,7 +128,7 @@
128128

129129
allow(job).to receive(:fetch_ranked_stats).and_return(ranked_data)
130130

131-
job.perform(player.id)
131+
job.perform(player.id, organization.id)
132132
end
133133
end
134134

@@ -144,7 +144,7 @@
144144
expect(Rails.logger).to receive(:error).with("Failed to sync player #{player.id}: API Error")
145145
expect(Rails.logger).to receive(:error).with(anything) # backtrace
146146

147-
job.perform(player.id)
147+
job.perform(player.id, organization.id)
148148

149149
player.reload
150150
expect(player.sync_status).to eq('error')
@@ -181,7 +181,7 @@
181181

182182
allow(job).to receive(:fetch_ranked_stats).and_return([])
183183

184-
job.perform(player_with_puuid.id)
184+
job.perform(player_with_puuid.id, organization.id)
185185
end
186186
end
187187
end

0 commit comments

Comments
 (0)