class Amazon::Coral::Orchestrator

Directs a Job through a Handler chain for processing.

Public Class Methods

new(handlers) click to toggle source

Instantiate an orchestrator with the given list of Handlers.

# File lib/amazon/coral/orchestrator.rb, line 14
def initialize(handlers)
  @log = LogFactory.getLog('Amazon::Coral::Orchestrator')
  @handlers = handlers
  
  @log.info "Initialized with handlers: #{handlers}"
end

Public Instance Methods

orchestrate(request) click to toggle source

Direct the specified request down the Handler chain, invoking first each before method, then in reverse order each after method. If any exceptions are thrown along the way, orchestration will stop immediately.

# File lib/amazon/coral/orchestrator.rb, line 24
def orchestrate(request)
  @log.debug "Processing request #{request}"
  
  job = Job.new(request)
  
  stack = []
  
  @handlers.each { |handler|
    stack << handler
    
    @log.debug "Invoking #{handler}.before()"
    handler.before(job)
  }
  
  stack.reverse.each { |handler|
    @log.debug "Invoking #{handler}.after()"
    handler.after(job)
  }
  
  return job.reply
end