class ChildLabor::Task
Attributes
Public Class Methods
Source
# File lib/child_labor.rb, line 21 def initialize(cmd) @cmd = cmd @pid = nil @exit_status = nil @terminated = false end
Public Instance Methods
Source
# File lib/child_labor.rb, line 122 def close close_read close_write close_stderr end
Source
# File lib/child_labor.rb, line 107 def close_read @stdout.close if @stdout @stdout = nil end
Source
# File lib/child_labor.rb, line 117 def close_stderr @stderr.close if @stderr @stderr = nil end
Source
# File lib/child_labor.rb, line 112 def close_write @stdin.close if @stdin @stdin = nil end
Source
# File lib/child_labor.rb, line 66 def exit_status poll_status @exit_status end
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
Source
# File lib/child_labor.rb, line 87 def read(length = nil, buffer = nil) @stdout.read(length, buffer) end
Source
# File lib/child_labor.rb, line 95 def read_stderr(length = nil, buffer = nil) @stderr.read(length, buffer) end
Source
# File lib/child_labor.rb, line 58 def running? poll_status(Process::WNOHANG) end
Source
# File lib/child_labor.rb, line 103 def signal(signal) Process.kill(signal, @pid) end
Source
# File lib/child_labor.rb, line 83 def terminate(signal = 'TERM') signal(signal) end
Private Instance Methods
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
Source
# 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
Handles the state of the Task
in a single call to Process.wait2. Returns true if the process is currently running