Module: TinyCI::Subprocesses

Included in:
Compactor, Executor, Runner, Scheduler
Defined in:
lib/tinyci/subprocesses.rb

Overview

Methods for executing subprocesses in various ways and collecting the results.

Defined Under Namespace

Classes: SubprocessError

Instance Method Summary collapse

Instance Method Details

#execute(*command, label: nil) ⇒ String

Synchronously execute a command as a subprocess and return the output.

Parameters:

  • command (Array<String>)

    The command line

  • label (String) (defaults to: nil)

    A label for debug and logging purposes

Returns:

  • (String)

    The output of the command

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tinyci/subprocesses.rb', line 15

def execute(*command, label: nil)
  output, status = Open3.capture2(*command.flatten)
  
  log_debug caller[0]
  log_debug "CMD: #{command.join(' ')}"
  log_debug "OUT: #{output}"
  
  unless status.success?
    log_error output
    raise SubprocessError.new(label, command.join(' '), status)
  end
  
  output.chomp
end

#execute_pipe(*commands, label: nil) ⇒ String

Synchronously execute a chain multiple commands piped into each other as a subprocess and return the output.

Parameters:

  • commands (Array<Array<String>>)

    The command lines

  • label (String) (defaults to: nil)

    A label for debug and logging purposes

Returns:

  • (String)

    The output of the command

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tinyci/subprocesses.rb', line 38

def execute_pipe(*commands, label: nil)
  stdout, waiters = Open3.pipeline_r(*commands)
  output = stdout.read
  
  waiters.each_with_index do |waiter, i|
    status = waiter.value
    unless status.success?
      log_error output
      raise SubprocessError.new(label, commands[i].join(' '), status)
    end
  end
  
  output.chomp
end

#execute_stream(*command, label: nil, pwd: nil) ⇒ TrueClass

Synchronously execute a command as a subprocess and and stream the output to STDOUT

Parameters:

  • command (Array<String>)

    The command line

  • label (String) (defaults to: nil)

    A label for debug and logging purposes

  • pwd (String) (defaults to: nil)

    Optionally specify a different working directory in which to execute the command

Returns:

  • (TrueClass)

    true if the command executed successfully

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tinyci/subprocesses.rb', line 62

def execute_stream(*command, label: nil, pwd: nil)
  opts = {}
  opts[:chdir] = pwd unless pwd.nil?
  
  Open3.popen2e(command.join(' '), opts) do |stdin, stdout_and_stderr, wait_thr|
    stdin.close
    
    until stdout_and_stderr.closed? || stdout_and_stderr.eof?
      line = stdout_and_stderr.gets
      log_info line.chomp
      $stdout.flush
    end
    
    unless wait_thr.value.success?  
      raise SubprocessError.new(label, command.join(' '), wait_thr.value)
    end
    stdout_and_stderr.close
  end
  
  true
end