Module: MingleEvents::Http

Extended by:
Http
Included in:
Http
Defined in:
lib/mingle_events/http.rb

Constant Summary collapse

MAX_RETRY_TIMES =
5

Instance Method Summary collapse

Instance Method Details

#get(url, retry_count = 0, max_retry_times = MAX_RETRY_TIMES, &block) ⇒ Object

get response body for a url, a block can be passed in for request pre-processing



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mingle_events/http.rb', line 8

def get(url, retry_count=0, max_retry_times=MAX_RETRY_TIMES, &block)
  rsp = nil
  retrying = lambda do
    raise HttpError.new(rsp, url) if retry_count >= max_retry_times
    cooldown = retry_count * 2
    MingleEvents.log.info "Getting service error when get page at #{url}, retry after #{cooldown}s..."
    sleep cooldown
    get(url, retry_count + 1, max_retry_times, &block)
  end

  begin
    rsp = fetch_page_response(url, &block)
  rescue EOFError => e
    return retrying.call
  end

  case rsp
  when Net::HTTPSuccess
    rsp.body
  when Net::HTTPUnauthorized
    raise HttpError.new(rsp, url, %{
If you think you are passing correct credentials, please check
that you have enabled Mingle for basic authentication.
See <http://www.thoughtworks-studios.com/mingle/3.3/help/configuring_mingle_authentication.html>.})
  when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
    retrying.call
  else
    raise HttpError.new(rsp, url)
  end
end