class Scmd::Command::ChildProcess

Attributes

pid[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(cmd_str, env, options) click to toggle source
# File lib/scmd/command.rb, line 179
def initialize(cmd_str, env, options)
  @pid, @stdin, @stdout, @stderr = *::POSIX::Spawn.popen4(
    env,
    cmd_str,
    options,
  )
  @wait_pid, @wait_status = nil, nil
end

Public Instance Methods

check_for_exit() click to toggle source
# File lib/scmd/command.rb, line 188
def check_for_exit
  if @wait_pid.nil?
    @wait_pid, @wait_status = ::Process.waitpid2(@pid, ::Process::WNOHANG)
    @wait_pid = nil if @wait_pid == 0 # may happen on jruby
  end
  @wait_pid.nil?
end
exitstatus() click to toggle source
# File lib/scmd/command.rb, line 200
def exitstatus
  return nil if @wait_status.nil?
  @wait_status.exitstatus || @wait_status.termsig
end
flush_stderr() click to toggle source
# File lib/scmd/command.rb, line 231
def flush_stderr
  @stderr.read
end
flush_stdout() click to toggle source
# File lib/scmd/command.rb, line 227
def flush_stdout
  @stdout.read
end
read(size) { |read_if_ready(ios, stdout, size), read_if_ready(ios, stderr, size)| ... } click to toggle source
# File lib/scmd/command.rb, line 212
def read(size)
  ios, _, _ =
    IO.select([@stdout, @stderr], nil, nil, READ_CHECK_TIMEOUT)
  if ios && block_given?
    yield(
      read_if_ready(ios, @stdout, size),
      read_if_ready(ios, @stderr, size)
    )
  end
end
running?() click to toggle source
# File lib/scmd/command.rb, line 196
def running?
  @wait_pid.nil?
end
send_signal(sig) click to toggle source
# File lib/scmd/command.rb, line 223
def send_signal(sig)
  process_kill(sig, pid)
end
teardown() click to toggle source
# File lib/scmd/command.rb, line 235
def teardown
  [@stdin, @stdout, @stderr].each{ |fd| fd.close if fd && !fd.closed? }
end
write(input) click to toggle source
# File lib/scmd/command.rb, line 205
def write(input)
  unless input.nil?
    [*input].each{ |line| @stdin.puts line.to_s }
    @stdin.close
  end
end

Private Instance Methods

child_pids(pid) click to toggle source
# File lib/scmd/command.rb, line 254
def child_pids(pid)
  Command.new("#{pgrep} -P #{pid}").run.stdout.split("\n").map(&:to_i)
end
pgrep() click to toggle source
# File lib/scmd/command.rb, line 258
def pgrep
  @pgrep ||= Command.new("which pgrep").run.stdout.strip
end
process_kill(sig, pid) click to toggle source
# File lib/scmd/command.rb, line 249
def process_kill(sig, pid)
  child_pids(pid).each{ |p| process_kill(sig, p) }
  ::Process.kill(sig, pid)
end
read_by_size(io, size) click to toggle source
# File lib/scmd/command.rb, line 245
def read_by_size(io, size)
  io.read_nonblock(size)
end
read_if_ready(ready_ios, io, size) click to toggle source
# File lib/scmd/command.rb, line 241
def read_if_ready(ready_ios, io, size)
  ready_ios.include?(io) ? read_by_size(io, size) : ""
end