Class: Harvest::HardyClient::HardyCollection

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

Instance Method Summary (collapse)

Constructor Details

- (HardyCollection) initialize(collection, client, max_retries)

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

- (Object) __getobj__



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

def __getobj__; @_sd_obj; end

- (Object) __setobj__(obj)



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

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

- (Object) retry_rate_limits



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"].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