Class: Scmd::Command::ChildProcess
- Inherits:
-
Object
- Object
- Scmd::Command::ChildProcess
- Defined in:
- lib/scmd/command.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#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
- #check_for_exit ⇒ Object
- #exitstatus ⇒ Object
- #flush_stderr ⇒ Object
- #flush_stdout ⇒ Object
-
#initialize(cmd_str, env, options) ⇒ ChildProcess
constructor
A new instance of ChildProcess.
- #read(size) ⇒ Object
- #running? ⇒ Boolean
- #send_signal(sig) ⇒ Object
- #teardown ⇒ Object
- #write(input) ⇒ Object
Constructor Details
#initialize(cmd_str, env, options) ⇒ ChildProcess
Returns a new instance of ChildProcess.
170 171 172 173 174 175 176 177 |
# File 'lib/scmd/command.rb', line 170 def initialize(cmd_str, env, ) @pid, @stdin, @stdout, @stderr = *::POSIX::Spawn::popen4( env, cmd_str, ) @wait_pid, @wait_status = nil, nil end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
168 169 170 |
# File 'lib/scmd/command.rb', line 168 def pid @pid end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
168 169 170 |
# File 'lib/scmd/command.rb', line 168 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
168 169 170 |
# File 'lib/scmd/command.rb', line 168 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
168 169 170 |
# File 'lib/scmd/command.rb', line 168 def stdout @stdout end |
Instance Method Details
#check_for_exit ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/scmd/command.rb', line 179 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 ⇒ Object
191 192 193 194 |
# File 'lib/scmd/command.rb', line 191 def exitstatus return nil if @wait_status.nil? @wait_status.exitstatus || @wait_status.termsig end |
#flush_stderr ⇒ Object
215 |
# File 'lib/scmd/command.rb', line 215 def flush_stderr; @stderr.read; end |
#flush_stdout ⇒ Object
214 |
# File 'lib/scmd/command.rb', line 214 def flush_stdout; @stdout.read; end |
#read(size) ⇒ Object
203 204 205 206 207 208 |
# File 'lib/scmd/command.rb', line 203 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? ⇒ Boolean
187 188 189 |
# File 'lib/scmd/command.rb', line 187 def running? @wait_pid.nil? end |
#send_signal(sig) ⇒ Object
210 211 212 |
# File 'lib/scmd/command.rb', line 210 def send_signal(sig) process_kill(sig, self.pid) end |
#teardown ⇒ Object
217 218 219 |
# File 'lib/scmd/command.rb', line 217 def teardown [@stdin, @stdout, @stderr].each{ |fd| fd.close if fd && !fd.closed? } end |
#write(input) ⇒ Object
196 197 198 199 200 201 |
# File 'lib/scmd/command.rb', line 196 def write(input) if !input.nil? [*input].each{ |line| @stdin.puts line.to_s } @stdin.close end end |