Class: Idlc::AWSLambdaProxy

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/idlc-sdk-core/restclient.rb

Instance Method Summary collapse

Methods included from Helpers

debug, err, msg, stderr, stdout, system_command

Instance Method Details

#fetch(request) ⇒ Object



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
# File 'lib/idlc-sdk-core/restclient.rb', line 15

def fetch(request)
  client = Aws::Lambda::Client.new()
  region = ENV['AWS_REGION']
  backend_services = Idlc::SERVICES[region]

  request[:function] = "#{request[:service]}-" + backend_services[request[:service]]['stage'] + "-#{request[:lambda]}"
  request[:httpMethod] = request[:method]

  retries = 0
  max_retries = 5
  sleep_time = 5
  exp_backoff = 2
  result = nil

  loop do
    begin
      resp = client.invoke({
        function_name: "service-lambda-proxy",
        invocation_type: "RequestResponse",
        log_type: "None",
        payload: request.to_json,
      })

      result = JSON.parse(JSON.parse(JSON.parse(resp.payload.string)['Payload'])['body'])
      break
    rescue Exception => e
      break if retries >= max_retries
      retries += 1
      msg("RequestFailed: #{e} - Waiting #{sleep_time}s then retrying... (#{retries} of #{max_retries})")


      sleep sleep_time
      sleep_time *= exp_backoff # use an exponential backoff when retrying requests
    end
  end

  result
end