diff --git a/lib/statistics/client.rb b/lib/statistics/client.rb index c75ac856a..d5200a90a 100644 --- a/lib/statistics/client.rb +++ b/lib/statistics/client.rb @@ -1,5 +1,7 @@ module Statistics class Client + class APIRateLimitError < ::StandardError; end + class_attribute :debug self.debug = false @@ -99,6 +101,12 @@ def fetch_events(group_id:, since_at: @default_since, until_at: @default_until) end events + rescue Faraday::ClientError => e + if e.response[:status] == 429 + raise Client::APIRateLimitError + else + raise e + end end end end diff --git a/lib/tasks/statistics.rake b/lib/tasks/statistics.rake index 257124f3c..6b9b115fa 100644 --- a/lib/tasks/statistics.rake +++ b/lib/tasks/statistics.rake @@ -42,7 +42,15 @@ namespace :statistics do raise StopIteration if nm > to list << nm }.each { |date| - Statistics::Aggregation.run(date: date) + begin + puts "Aggregate for #{date.strftime('%Y/%m')}" + Statistics::Aggregation.run(date: date) + rescue Statistics::Client::APIRateLimitError + puts 'API rate limit exceeded.' + puts "This task will retry in 60 seconds from now(#{Time.zone.now})." + sleep 60 + retry + end } end