Method: Wrapbox::Runner::Docker#run_cmd

Defined in:
lib/wrapbox/runner/docker.rb

#run_cmd(cmds, container_definition_overrides: {}, environments: [], ignore_signal: false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wrapbox/runner/docker.rb', line 54

def run_cmd(cmds,  container_definition_overrides: {}, environments: [], ignore_signal: false)
  ths = []
  definition = container_definition
    .merge(container_definition_overrides)

  environments = extract_environments(environments)

  cmds << nil if cmds.empty?
  cmds.each_with_index do |cmd, idx|
    ths << Thread.new(cmd, idx) do |c, i|
      envs = environments + ["WRAPBOX_CMD_INDEX=#{idx}"]
      exec_docker(
        definition: definition,
        cmd: c&.shellsplit,
        environments: envs
      )
    end
  end
  ths.each { |th| th&.join }

  true
rescue SignalException => e
  sig = "SIG#{Signal.signame(e.signo)}"
  if ignore_signal
    @logger.info("Receive #{sig} signal. But Docker container continue running")
  else
    @logger.info("Receive #{sig} signal. Stop All tasks")
    ths.each do |th|
      th.report_on_exception = false
      th.raise(e)
    end
    thread_timeout = 15
    ths.each { |th| th.join(thread_timeout) }
  end
  nil
end