Class: Dandy::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/dandy/chain.rb

Instance Method Summary collapse

Constructor Details

#initialize(container, dandy_config) ⇒ Chain



5
6
7
8
# File 'lib/dandy/chain.rb', line 5

def initialize(container, dandy_config)
  @container = container
  @async_timeout = dandy_config[:action][:async_timeout]
end

Instance Method Details

#run_commands(commands, last_command) ⇒ Object



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
42
43
44
45
46
# File 'lib/dandy/chain.rb', line 10

def run_commands(commands, last_command)
  threads = []
  Thread.abort_on_exception = true

  result = nil
  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 = []

      if last_command && (command.name == last_command.name)
        result = run_command(command)
      else
        run_command(command)
      end
    else
      thread = Thread.new {
        Timeout::timeout(@async_timeout) {
          if last_command && (command.name == last_command.name)
            result = run_command(command)
          else
            run_command(command)
          end
        }
      }
      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

  result
end