Class: Wakame::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ Dispatcher

Returns a new instance of Dispatcher.



208
209
210
# File 'lib/wakame/agent.rb', line 208

def initialize(agent)
  @agent = agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



206
207
208
# File 'lib/wakame/agent.rb', line 206

def agent
  @agent
end

Instance Method Details

#handle_request(request) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/wakame/agent.rb', line 212

def handle_request(request)
  slash = request[:path].rindex('/')
  raise "Invalid request path: #{request[:path]}" unless slash

  prefix = request[:path][0, slash]
  action = request[:path][slash+1, request[:path].length]

  actor = agent.actor_registry.find_actor(prefix)
  unless actor
    Wakame.log.error("No refered actor instance: #{prefix}")
    raise
  end

  EM.defer(proc {
             begin
               Wakame.log.debug("#{self.class}: Started to run the actor: #{actor.class}, token=#{request[:token]}")
               agent.publish_to('agent_event', Packets::ActorResponse.new(agent, request[:token], Actor::STATUS_RUNNING).marshal)
               if request[:args].nil?
                 actor.send(action)
               else
                 actor.send(action, *request[:args])
               end
               Wakame.log.debug("#{self.class}: Finished to run the actor: #{actor.class}, token=#{request[:token]}")
               actor.return_value
             rescue => e
               Wakame.log.error("#{self.class}: Failed the actor: #{actor.class}, token=#{request[:token]}")
               Wakame.log.error(e)
               e
             end
           }, proc { |res|
             status = Actor::STATUS_SUCCESS
             if res.is_a?(Exception)
               status = Actor::STATUS_FAILED
             else
               opts = {:return_value=>res}
             end
             agent.publish_to('agent_event', Packets::ActorResponse.new(self.agent, request[:token], status, opts).marshal)
           })
end