Class: Combi::InProcess

Inherits:
Bus
  • Object
show all
Defined in:
lib/combi/buses/in_process.rb

Constant Summary

Constants inherited from Bus

Bus::RPC_DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Bus

#services

Instance Method Summary collapse

Methods inherited from Bus

#add_service, #enable, #initialize, #log, #post_initialize, #restart!, #start!, #stop!

Constructor Details

This class inherits a constructor from Combi::Bus

Instance Method Details

#memory_handlersObject



47
48
49
# File 'lib/combi/buses/in_process.rb', line 47

def memory_handlers
  @memory_handlers ||= {}
end

#request(handler_name, kind, message, options = {}) ⇒ Object



6
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
35
36
37
38
39
40
41
# File 'lib/combi/buses/in_process.rb', line 6

def request(handler_name, kind, message, options = {})
  options[:timeout] ||= RPC_DEFAULT_TIMEOUT
  handler = memory_handlers[handler_name.to_s]
  waiter = EventMachine::DefaultDeferrable.new
  if handler.nil?
    waiter.fail('error' => true, 'message' => 'unknown service')
  else
    service_instance = handler[:service_instance]
    message = JSON.parse(message.to_json)
    if service_instance.respond_to?(kind)
      waiter.timeout(options[:timeout], RuntimeError.new(Timeout::Error))
      begin
        Timeout.timeout(options[:timeout]) do
          response = service_instance.send(kind, message)
          if response.respond_to? :succeed
            response.callback do |service_response|
              waiter.succeed service_response
            end
          else
            waiter.succeed response
          end
        end
      rescue Timeout::Error => e
        log "ERROR"
        waiter.fail RuntimeError.new(Timeout::Error)
      rescue Exception => e
        log "other ERROR"
        log e.inspect
        waiter.fail({'error' => true, 'message' => e.message})
      end
    else
      waiter.fail('error' => true, 'message' => 'unknown action')
    end
  end
  waiter
end

#respond_to(service_instance, action, options = {}) ⇒ Object



43
44
45
# File 'lib/combi/buses/in_process.rb', line 43

def respond_to(service_instance, action, options = {})
  memory_handlers[action.to_s] = {service_instance: service_instance, options: options}
end