Class: Magellan::Rails::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/magellan/rails/executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(exchange) ⇒ Executor

Returns a new instance of Executor.



6
7
8
9
# File 'lib/magellan/rails/executor.rb', line 6

def initialize(exchange)
  @exchange     = exchange
  @app     = ::Rails.application
end

Instance Method Details

#execute(reply_to, correlation_id, delivery_tag, request_message) ⇒ Object



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/magellan/rails/executor.rb', line 11

def execute(reply_to, correlation_id, delivery_tag, request_message)
  response = Magellan::Rails::Response.new()

  request  = Magellan::Rails::Request.new()
  request.parse_message(request_message)

  begin
    rack_response = @app.call(request.to_rack_env)
    response.parse_rack_response(rack_response)
  rescue Exception => e
    response.status  = '500'
    response.headers = {'Content-Type' => 'text/plain', 'charset' => 'utf-8'}
    response.body    = 'Internal Server Error'
    Magellan.logger.error(e)
  ensure
    @exchange.publish(
      response.to_message,
      {
        expiration: request.reply_ttl,
        correlation_id: correlation_id,
        routing_key: reply_to
      })
  end
end