class DaemonRunner::ShellOut

Attributes

command[R]
cwd[R]
runner[R]
stdout[R]
timeout[R]
valid_exit_codes[R]
wait[R]

Public Class Methods

new(command: nil, cwd: '/tmp', timeout: 15, wait: true, valid_exit_codes: [0]) click to toggle source

@param command [String] the command to run @param cwd [String] the working directory to run the command @param timeout [Fixnum] the command timeout @param wait [Boolean] wheather to wait for the command to finish @param valid_exit_codes [Array<Fixnum>] exit codes that aren't flagged as failures

# File lib/daemon_runner/shell_out.rb, line 24
def initialize(command: nil, cwd: '/tmp', timeout: 15, wait: true, valid_exit_codes: [0])
  @command = command
  @cwd = cwd
  @timeout = timeout
  @wait = wait
  @valid_exit_codes = valid_exit_codes
end
wait2(pid = nil, flags = 0) click to toggle source

Wait for the process with the given pid to finish @param pid [Fixnum] the pid to wait on @param flags [Fixnum] flags to Process.wait2 @return [Process::Status, nil] the process status or nil if no process was found

# File lib/daemon_runner/shell_out.rb, line 12
def self.wait2(pid = nil, flags = 0)
  return nil if pid.nil?
  Process.wait2(pid, flags)[1]
rescue Errno::ECHILD
  nil
end

Public Instance Methods

run!() click to toggle source

Run command @return [Mixlib::ShellOut, Fixnum] mixlib shellout client or a pid depending on the value of {#wait}

# File lib/daemon_runner/shell_out.rb, line 34
def run!
  validate_command
  if wait
    run_and_wait
  else
    run_and_detach
  end
end
wait2(flags = 0) click to toggle source

Wait for the process to finish @param flags [Fixnum] flags to Process.wait2 @return [Process::Status, nil] the process status or nil if no process was found

# File lib/daemon_runner/shell_out.rb, line 46
def wait2(flags = 0)
  self.class.wait2(@pid, flags)
end

Private Instance Methods

run_and_detach() click to toggle source

Run a command in a new process group, thus ignoring any furthur updates about the status of the process @return [Fixnum] process id

# File lib/daemon_runner/shell_out.rb, line 66
def run_and_detach
  log_r, log_w = IO.pipe
  @pid = Process.spawn(command, pgroup: true, err: :out, out: log_w)
  log_r.close
  log_w.close
  @pid
end
run_and_wait() click to toggle source

Run a command and wait for it to finish @return [Mixlib::ShellOut] client

# File lib/daemon_runner/shell_out.rb, line 54
def run_and_wait
  validate_args
  runner
  @runner.run_command
  @runner.error!
  @stdout = @runner.stdout
  @runner
end
validate_args() click to toggle source

Validate arguments before trying to start the command @ raise [ArgumentError] if any of the arguments are missing

# File lib/daemon_runner/shell_out.rb, line 84
def validate_args
  if @cwd.nil? && !respond_to?(:cwd)
    raise ArgumentError, 'Must pass a cwd or implement a cwd method'
  end

  if @timeout.nil? && !respond_to?(:timeout)
    raise ArgumentError, 'Must pass a timeout or implement a timeout method'
  end
end
validate_command() click to toggle source

Validate command is defined before trying to start the command @ raise [ArgumentError] if any of the arguments are missing

# File lib/daemon_runner/shell_out.rb, line 76
def validate_command
  if @command.nil? && !respond_to?(:command)
    raise ArgumentError, 'Must pass a command or implement a command method'
  end
end