Module: RVideo::CommandExecutor

Defined in:
lib/rvideo/command_executor.rb

Defined Under Namespace

Classes: ProcessHungError

Constant Summary collapse

STDOUT_TIMEOUT =
200

Class Method Summary collapse

Class Method Details

.execute_tailing_stderr(command, number_of_lines = 500) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rvideo/command_executor.rb', line 82

def self.execute_tailing_stderr(command, number_of_lines = 500)
  result = String.new
  open4(command) do |pid, i, o, e|
    open4.spawn "tail -n #{number_of_lines}", :stdin=>e, :stdout=>result, :stdin_timeout => 24*60*60
  end
  result
end

.execute_with_block(command, line_separator = $/, use_stderr = true) ⇒ Object



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
# File 'lib/rvideo/command_executor.rb', line 55

def self.execute_with_block(command, line_separator=$/, use_stderr = true)
  begin
    pid, stdin, stdout, stderr = Open4::open4(command)
    c_pipe = use_stderr ? stderr : stdout
    pipe_result = ''

    c_pipe.each_with_timeout(STDOUT_TIMEOUT, line_separator) do |line|
      yield line
      pipe_result += line
    end
      Process.kill("SIGKILL", pid)
      Process.waitpid2 pid
      
      stdout_result = use_stderr ? stdout.read : pipe_result
      stderr_result =  use_stderr ? pipe_result : stderr.read
      
      [stdin, stdout, stderr].each{|io| io.close}
      
      return [stderr_result, stdout_result]
  rescue Timeout::Error => e
      Process.kill("SIGKILL", pid)
      Process.waitpid2 pid
      [stdin, stdout, stderr].each{|io| io.close}
    raise ProcessHungError
  end
end