class Silicon::Chain

Public Class Methods

new(container, silicon_config, commands, catch_command = nil) click to toggle source
# File lib/silicon/chain.rb, line 5
def initialize(container, silicon_config, commands, catch_command = nil)
  @commands = commands
  @container = container
  @catch_command = catch_command
  @async_timeout = silicon_config[:action][:async_timeout]
end

Public Instance Methods

execute() click to toggle source
# File lib/silicon/chain.rb, line 12
def execute
  if @catch_command.nil?
    run_commands
  else
    begin
      run_commands
    rescue Exception => error
      @container
        .register_instance(error, :silicon_error)
        .using_lifetime(:scope)
        .bound_to(:silicon_request)

      action = @container.resolve(@catch_command.name.to_sym)
      action.call
    end
  end
end

Private Instance Methods

run_command(command) click to toggle source
# File lib/silicon/chain.rb, line 58
def run_command(command)
  action = @container.resolve(command.name.to_sym)
  result = action.call

  @container
    .register_instance(result, command.result_name.to_sym)
    .using_lifetime(:scope)
    .bound_to(:silicon_request)
end
run_commands() click to toggle source
# File lib/silicon/chain.rb, line 32
def run_commands
  threads = []
  Thread.abort_on_exception = true
  @commands.each_with_index do |command, index|
    if command.sequential?
      # all previous parallel commands should be done before the current sequential
      threads.each {|t| t.join}
      threads = []

      run_command(command)
    else
      thread = Thread.new {
        Timeout::timeout(@async_timeout) {
          run_command(command)
        }
      }
      threads << thread if command.parallel?
    end

    # if it's last item in chain then wait until parallel commands are done
    if index == @commands.length - 1
      threads.each {|t| t.join}
    end
  end
end