class ChildLabor::Task

Attributes

cmd[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(cmd) click to toggle source
# File lib/child_labor.rb, line 21
def initialize(cmd)
  @cmd = cmd
  @pid = nil
  @exit_status = nil
  @terminated  = false
end

Public Instance Methods

close() click to toggle source
# File lib/child_labor.rb, line 122
def close
  close_read
  close_write
  close_stderr
end
close_read() click to toggle source
# File lib/child_labor.rb, line 107
def close_read
  @stdout.close if @stdout
  @stdout = nil
end
close_stderr() click to toggle source
# File lib/child_labor.rb, line 117
def close_stderr
  @stderr.close if @stderr
  @stderr = nil
end
close_write() click to toggle source
# File lib/child_labor.rb, line 112
def close_write
  @stdin.close if @stdin
  @stdin = nil
end
exit_status() click to toggle source
# File lib/child_labor.rb, line 66
def exit_status
  poll_status
  @exit_status
end
launch() click to toggle source
# File lib/child_labor.rb, line 28
def launch
  create_pipes

  @pid = Process.fork

  # child
  unless @pid
    @stdout.close
    STDOUT.reopen @stdout_child

    @stdin.close
    STDIN.reopen @stdin_child

    @stderr.close
    STDERR.reopen @stderr_child

    Process.exec cmd
  end

  @stdout_child.close
  @stdin_child.close
  @stderr_child.close

  true
end
launched?() click to toggle source
# File lib/child_labor.rb, line 54
def launched?
  @pid
end
read(length = nil, buffer = nil) click to toggle source
# File lib/child_labor.rb, line 87
def read(length = nil, buffer = nil)
  @stdout.read(length, buffer)
end
read_stderr(length = nil, buffer = nil) click to toggle source
# File lib/child_labor.rb, line 95
def read_stderr(length = nil, buffer = nil)
  @stderr.read(length, buffer)
end
readline() click to toggle source
# File lib/child_labor.rb, line 91
def readline
  @stdout.readline
end
resume() click to toggle source
# File lib/child_labor.rb, line 79
def resume
  signal('CONT')
end
running?() click to toggle source
# File lib/child_labor.rb, line 58
def running?
  poll_status(Process::WNOHANG)
end
signal(signal) click to toggle source
# File lib/child_labor.rb, line 103
def signal(signal)
  Process.kill(signal, @pid)
end
suspend() click to toggle source
# File lib/child_labor.rb, line 75
def suspend
  signal('STOP')
end
terminate(signal = 'TERM') click to toggle source
# File lib/child_labor.rb, line 83
def terminate(signal = 'TERM')
  signal(signal)
end
terminated?() click to toggle source
# File lib/child_labor.rb, line 62
def terminated?
  launched? && !running?
end
wait() click to toggle source
# File lib/child_labor.rb, line 71
def wait
  exit_status
end
write(str) click to toggle source
# File lib/child_labor.rb, line 99
def write(str)
  @stdin.write(str)
end

Private Instance Methods

create_pipes() click to toggle source
# File lib/child_labor.rb, line 148
def create_pipes
  @stdout, @stdout_child = IO.pipe
  @stdin_child, @stdin   = IO.pipe
  @stderr, @stderr_child = IO.pipe
end
poll_status(flags = 0) click to toggle source

Handles the state of the Task in a single call to Process.wait2. Returns true if the process is currently running

# File lib/child_labor.rb, line 133
def poll_status(flags = 0)
  return false unless @pid
  return false if @terminated

  pid, status = Process.wait2(@pid, flags)

  return true unless pid

  @terminated  = true
  @exit_status = status
  false
rescue Errno::ECHILD
  false
end