Module: MultiTimeout::CLI

Defined in:
lib/multi_timeout.rb

Constant Summary collapse

TICK =
1
VALID_SIGNAL =
/^(-(\d+|[A-Z\d]+))$/

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



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
# File 'lib/multi_timeout.rb', line 11

def run(argv)
  options = parse_options(argv)
  command = options[:command]

  pid = fork { exec command }
  Thread.new do
    now = 0
    loop do
      break if dead?(pid)

      options[:timeouts].each do |signal, t|
        if now >= t
          options[:timeouts].delete([signal, t])
          puts "Killing '#{command}' with signal #{signal} after #{now} seconds"
          STDOUT.flush # wait instantly terminates and sometimes hides puts
          Process.kill(signal, pid)
        end
      end
      now += TICK
      sleep TICK
    end
  end

  Process.wait2.last.exitstatus || 1
end