Class: ChildLabor::Task
- Inherits:
-
Object
- Object
- ChildLabor::Task
- Defined in:
- lib/child_labor.rb
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #close ⇒ Object
- #close_read ⇒ Object
- #close_stderr ⇒ Object
- #close_write ⇒ Object
- #exit_status ⇒ Object
-
#initialize(cmd) ⇒ Task
constructor
A new instance of Task.
- #launch ⇒ Object
- #launched? ⇒ Boolean
- #read(length = nil, buffer = nil) ⇒ Object
- #read_stderr(length = nil, buffer = nil) ⇒ Object
- #readline ⇒ Object
- #resume ⇒ Object
- #running? ⇒ Boolean
- #signal(signal) ⇒ Object
- #suspend ⇒ Object
- #terminate(signal = 'TERM') ⇒ Object
- #terminated? ⇒ Boolean
- #wait ⇒ Object
- #write(str) ⇒ Object
Constructor Details
#initialize(cmd) ⇒ Task
Returns a new instance of Task.
21 22 23 24 25 26 |
# File 'lib/child_labor.rb', line 21 def initialize(cmd) @cmd = cmd @pid = nil @exit_status = nil @terminated = false end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
19 20 21 |
# File 'lib/child_labor.rb', line 19 def cmd @cmd end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
19 20 21 |
# File 'lib/child_labor.rb', line 19 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
19 20 21 |
# File 'lib/child_labor.rb', line 19 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
19 20 21 |
# File 'lib/child_labor.rb', line 19 def stdout @stdout end |
Instance Method Details
#close ⇒ Object
122 123 124 125 126 |
# File 'lib/child_labor.rb', line 122 def close close_read close_write close_stderr end |
#close_read ⇒ Object
107 108 109 110 |
# File 'lib/child_labor.rb', line 107 def close_read @stdout.close if @stdout @stdout = nil end |
#close_stderr ⇒ Object
117 118 119 120 |
# File 'lib/child_labor.rb', line 117 def close_stderr @stderr.close if @stderr @stderr = nil end |
#close_write ⇒ Object
112 113 114 115 |
# File 'lib/child_labor.rb', line 112 def close_write @stdin.close if @stdin @stdin = nil end |
#exit_status ⇒ Object
66 67 68 69 |
# File 'lib/child_labor.rb', line 66 def exit_status poll_status @exit_status end |
#launch ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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? ⇒ Boolean
54 55 56 |
# File 'lib/child_labor.rb', line 54 def launched? @pid end |
#read(length = nil, buffer = nil) ⇒ Object
87 88 89 |
# File 'lib/child_labor.rb', line 87 def read(length = nil, buffer = nil) @stdout.read(length, buffer) end |
#read_stderr(length = nil, buffer = nil) ⇒ Object
95 96 97 |
# File 'lib/child_labor.rb', line 95 def read_stderr(length = nil, buffer = nil) @stderr.read(length, buffer) end |
#readline ⇒ Object
91 92 93 |
# File 'lib/child_labor.rb', line 91 def readline @stdout.readline end |
#resume ⇒ Object
79 80 81 |
# File 'lib/child_labor.rb', line 79 def resume signal('CONT') end |
#running? ⇒ Boolean
58 59 60 |
# File 'lib/child_labor.rb', line 58 def running? poll_status(Process::WNOHANG) end |
#signal(signal) ⇒ Object
103 104 105 |
# File 'lib/child_labor.rb', line 103 def signal(signal) Process.kill(signal, @pid) end |
#suspend ⇒ Object
75 76 77 |
# File 'lib/child_labor.rb', line 75 def suspend signal('STOP') end |
#terminate(signal = 'TERM') ⇒ Object
83 84 85 |
# File 'lib/child_labor.rb', line 83 def terminate(signal = 'TERM') signal(signal) end |
#terminated? ⇒ Boolean
62 63 64 |
# File 'lib/child_labor.rb', line 62 def terminated? launched? && !running? end |
#wait ⇒ Object
71 72 73 |
# File 'lib/child_labor.rb', line 71 def wait exit_status end |
#write(str) ⇒ Object
99 100 101 |
# File 'lib/child_labor.rb', line 99 def write(str) @stdin.write(str) end |