diff options
author | nfoy <nfoy@purdue.edu> | 2014-04-28 20:39:29 -0400 |
---|---|---|
committer | nfoy <nfoy@purdue.edu> | 2014-04-28 20:39:29 -0400 |
commit | d2c700eb31e6a81cb5befe2f9dc57afa7fe8dc91 (patch) | |
tree | 7a3447dff226b45bee3d60762fa4138f04327b14 | |
parent | 25ff0e9d1a22cbfa65fbece800d00d7cae0c0d8e (diff) |
Luke did some shit I dont know if he finished
-rw-r--r-- | db/seeds.rb | 3 | ||||
-rw-r--r-- | lib/sampling/riot_api.rb | 11 | ||||
-rw-r--r-- | lib/throttled_api_request.rb | 25 |
3 files changed, 25 insertions, 14 deletions
diff --git a/db/seeds.rb b/db/seeds.rb index c648053..4886325 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -168,8 +168,7 @@ if Rails.env.development? jeff.remote_usernames.create(game: league, value: {"name" => "SenorJeffafa", "id" => 32612067} ) lyra.remote_usernames.create(game: league, value: {"name" => "Lyra Heartstings", "id" => 32240762} ) - g.push(davis, foy, andrew, joey, panda, mesa, jordan, jeff, sarah, josh_league) - #g.push(davis, foy, andrew, joey, panda, mesa, jordan, jeff, sarah, guntas_league) + g.push(davis, joey, panda, mesa, josh_league, jordan, jeff, sarah, foy, andrew) custom = Tournament.create(game: league, name: "Real League Game", min_players_per_team: 5, max_players_per_team: 5, diff --git a/lib/sampling/riot_api.rb b/lib/sampling/riot_api.rb index 7e14551..7d75475 100644 --- a/lib/sampling/riot_api.rb +++ b/lib/sampling/riot_api.rb @@ -39,7 +39,11 @@ module Sampling class Job < ThrottledApiRequest def initialize(request, args={}) @url = Sampling::RiotApi::url(request, args) - super(api_name, 10.seconds, 10) + limits = [ + {:unit_time => 10.seconds, :requests_per => 10}, + {:unit_time => 10.minutes, :requests_per => 500}, + ] + super(api_name, limits) end def perform @@ -153,7 +157,8 @@ module Sampling def start @match.teams.each do |team| team.users.each do |user| - Delayed::Job.enqueue(MatchJob.new(user, @match, @match.stats_from(self.class), nil), :queue => api_name) + #For demo purposes, we are hard coding in a league of legends game id. + Delayed::Job.enqueue(MatchJob.new(user, @match, @match.stats_from(self.class), 1362730546), :queue => api_name) end end end @@ -177,7 +182,7 @@ module Sampling Delayed::Job.enqueue(MatchJob.new(user, match, data["games"][0]["gameId"]), :queue => api_name) else if @last_game_id == data["games"][0]["gameId"] - # TODO: perhaps insert a delay here? + sleep(4.minutes) Delayed::Job.enqueue(MatchJob.new(user, match, @last_game_id), :queue => api_name) else @stats.each do |stat| diff --git a/lib/throttled_api_request.rb b/lib/throttled_api_request.rb index 3f30c56..1020071 100644 --- a/lib/throttled_api_request.rb +++ b/lib/throttled_api_request.rb @@ -1,18 +1,25 @@ -class ThrottledApiRequest < Struct.new(:api_name, :unit_time, :requests_per) +# limits is in the format: +# limits = [ +# {:unit_time => 10.seconds, :requests_per => 10}, +# {:unit_time => 10.minutes, :requests_per => 500}, +# ] +class ThrottledApiRequest < Struct.new(:api_name, :limits) def before(job) loop do sleep_for = -1 ActiveRecord::Base.transaction do ApiRequests.create(:api_name => self.api_name) - recent_requests = ApiRequets. - where(:api_name => self.api_name). - where("updated_at > ?", Time.now.utc - self.unit_time). - order(:updated_at) - if (recent_requests.count > self.requests_per) - sleep_for = Time.now.utc - recent_requests[recent_requests.count-self.requests_per].updated_at + self.limits.each do |limit| + recent_requests = ApiRequets. + where(:api_name => self.api_name). + where("updated_at > ?", Time.now.utc - limit[:unit_time]). + order(:updated_at) + if (recent_requests.count > limit[:requests_per]) + sleep_for = [sleep_for, Time.now.utc - recent_requests[recent_requests.count-limit[:requests_per]].updated_at].max + end + end + if sleep_for != -1 raise ActiveRecord::Rollback - else - sleep_for = -1 end end if sleep_for != -1 |