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
47
48
49
50
51
52
|
# File 'lib/sastbox-sdk/runner.rb', line 21
def run_cmd_with_timeout(cmd)
out_reader = ''
err_reader = ''
Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
stdin.close_write
output, pid = [], wait_thr.pid
begin
Timeout.timeout(@opts.timeout) do
begin
if stdout && !stdout.closed?
out_reader = Thread.new { stdout.read }
end
if stderr && !stderr.closed?
err_reader = Thread.new { stderr.read }
end
rescue IOError => e
puts "Error reading IO: #{e.message}"
end
Process.wait(pid)
end
rescue Errno::ECHILD
rescue Timeout::Error
print_error('Timed out - Skipping...')
Process.kill('TERM', pid)
exit 1
end
[out_reader.value, err_reader.value, wait_thr.value]
end
end
|