Module: LVS::JsonService::Request::ClassMethods

Defined in:
lib/lvs/json_service/request.rb

Instance Method Summary collapse

Instance Method Details

#http_request_with_timeout(service, args, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lvs/json_service/request.rb', line 14

def http_request_with_timeout(service, args, options)

  uri = URI.parse(service)

  http = Net::HTTP.new(uri.host, uri.port)

  if options[:encrypted] && !SSL_DISABLED
    http.use_ssl = true
    if options[:auth_cert]
      http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
      http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
    end
  end

  http.open_timeout = options[:timeout] || 1
  http.read_timeout = options[:timeout] || 1

  req = Net::HTTP::Post.new(uri.path)
  req.form_data = { "object_request" => args.to_json }

  retries = options[:retries] || 0

  begin
    retries -= 1          
    response = http.start { |connection| connection.request(req) }
  
  rescue Timeout::Error
    if retries >= 0
      LVS::JsonService::Logger.debug(
        "Retrying #{service} due to TimeoutError"
      )
      retry
    end
    raise LVS::JsonService::TimeoutError.new("Backend failed to respond in time", 500, service, args)
            
  rescue Errno::ECONNREFUSED
    if retries >= 0
      LVS::JsonService::Logger.debug(
        "Retrying #{service} due to Errno::ECONNREFUSED"
      )
      sleep(1)  
      retry
    end
    raise LVS::JsonService::BackendUnavailableError.new("Backend unavailable", 500, service, args)
  end

  if response.is_a?(Net::HTTPNotFound)
    raise LVS::JsonService::NotFoundError.new("404 Found for the service", 404, service, args)
  end

  response
end

#run_remote_request(service, args, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lvs/json_service/request.rb', line 67

def run_remote_request(service, args, options = {})
  LVS::JsonService::Logger.debug "run_remote_request('#{service}', #{args.to_json}"
  response = http_request_with_timeout(service, args, options)
  if response.body.size < 1024
    LVS::JsonService::Logger.debug "Response: #{response.body.gsub(/\n/, '')}"
  else
    LVS::JsonService::Logger.debug "Response Snippet: #{response.body.gsub(/\n/, '')[0..1024]}"
  end
  result = JSON.parse(response.body)
  if result.is_a?(Hash) && result.has_key?("PCode")
    raise LVS::JsonService::Error.new(result["message"], result["PCode"], service, args, result)
  end
  result
end