Class: Replr::ProcessRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/replr/process_runner.rb

Overview

Executes and manages processes

Instance Method Summary collapse

Instance Method Details

#execute_command(command) ⇒ Object

Executes command using system. Use this when you have no need for a return value



8
9
10
# File 'lib/replr/process_runner.rb', line 8

def execute_command(command)
  system(command)
end

#execute_command_if_not_stderr(new_command, stderr, process_thread) ⇒ Object

Executes new_command if the previous process_thread had a non-zero return. Otherwise prints out stderr of the previous process to STDERR



39
40
41
42
43
44
45
46
# File 'lib/replr/process_runner.rb', line 39

def execute_command_if_not_stderr(new_command, stderr, process_thread)
  outerror = stderr.read.chomp
  if process_thread.value.to_i.zero?
    system(new_command)
  else
    STDERR.puts outerror
  end
end

#execute_filtered_process(command, matching_regex, not_matching_regex, &_block) ⇒ Object

Runs command and only prints those lines that match matching_regex and doesn’t match not_matching_regex. After execution, it passes stderr and the waiting process_thread to a block.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/replr/process_runner.rb', line 21

def execute_filtered_process(command, matching_regex,
                             not_matching_regex, &_block)
  Open3.popen3(command) do |_stdin, stdout, stderr, process_thread|
    stdout.each_line do |outline|
      if outline.match(matching_regex) &&
         !outline.match(not_matching_regex)
        puts outline
      end
    end

    # Yield to block to process next set of commands or handle stderr
    yield(stderr, process_thread)
  end
end

#process_exists?(process) ⇒ Boolean

Checks whether process exists using ‘which`

Returns:

  • (Boolean)


13
14
15
# File 'lib/replr/process_runner.rb', line 13

def process_exists?(process)
  system("which #{process} >/dev/null")
end