Class: Harvest::HardyClient::HardyCollection

Inherits:
Delegator
  • Object
show all
Defined in:
lib/harvest/hardy_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, client, max_retries) ⇒ HardyCollection

Returns a new instance of HardyCollection.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/harvest/hardy_client.rb', line 27

def initialize(collection, client, max_retries)
  super(collection)
  @_sd_obj = @collection = collection
  @client = client
  @max_retries = max_retries
  (@collection.public_methods - Object.public_instance_methods).each do |name|
    instance_eval <<-END
      def #{name}(*args)
        retry_rate_limits do
          @collection.send('#{name}', *args)
        end
      end
    END
  end
end

Instance Method Details

#__getobj__Object



43
# File 'lib/harvest/hardy_client.rb', line 43

def __getobj__; @_sd_obj; end

#__setobj__(obj) ⇒ Object



44
# File 'lib/harvest/hardy_client.rb', line 44

def __setobj__(obj); @_sd_obj = obj; end

#retry_rate_limitsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/harvest/hardy_client.rb', line 46

def retry_rate_limits
  retries = 0
  
  retry_func = lambda do |e|
    if retries < @max_retries
      retries += 1
      true
    else
      raise e
    end
  end
  
  begin
    yield
  rescue Harvest::RateLimited => e
    seconds = if e.response.headers["retry-after"]
      e.response.headers["retry-after"].first.to_i
    else
      16
    end
    sleep(seconds)
    retry
  rescue Harvest::Unavailable, Harvest::InformHarvest => e
    would_retry = retry_func.call(e)
    sleep(16) if @client..rate_limit_status.over_limit?
    retry if would_retry
  rescue Net::HTTPError, Net::HTTPFatalError => e
    retry if retry_func.call(e)
  rescue SystemCallError => e
    retry if e.is_a?(Errno::ECONNRESET) && retry_func.call(e)
  end
end