Class: Idempotent::Rack

Inherits:
Handler show all
Defined in:
lib/idempotent/rack.rb

Instance Attribute Summary

Attributes inherited from Handler

#rescue_policy, #retry_policy

Instance Method Summary collapse

Methods inherited from Handler

#initialize

Constructor Details

This class inherits a constructor from Idempotent::Handler

Instance Method Details

#call(env) ⇒ Object



7
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
# File 'lib/idempotent/rack.rb', line 7

def call(env)
  request   = ::Rack::Request.new(env)
  response  = nil
  exception = nil

  while true
    retry_policy.call(request, response, exception) if response || exception
    response, exception = nil

    begin
      status, headers, body = @app.call(env.dup)
      response = ::Rack::Response.new(body, status, headers)

      next if rescue_policy.call(request, response, exception)

      return [status, headers, body]
    rescue Idempotent::Retryable => exception
      store_exception(exception, request)
      next
    rescue => exception
      if rescue_policy.call(request, response, exception)
        store_exception(exception, request)
        next
      end
      raise
    end
  end
end

#store_exception(exception, request) ⇒ Object



36
37
38
39
# File 'lib/idempotent/rack.rb', line 36

def store_exception(exception, request)
  request.env["idempotent.requests.exceptions"] ||= []
  request.env["idempotent.requests.exceptions"] << exception
end