Class: Scmd::Command::ChildProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/scmd/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd_str, env) ⇒ ChildProcess

Returns a new instance of ChildProcess.



168
169
170
171
# File 'lib/scmd/command.rb', line 168

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

#pidObject (readonly)

Returns the value of attribute pid.



166
167
168
# File 'lib/scmd/command.rb', line 166

def pid
  @pid
end

#stderrObject (readonly)

Returns the value of attribute stderr.



166
167
168
# File 'lib/scmd/command.rb', line 166

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



166
167
168
# File 'lib/scmd/command.rb', line 166

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



166
167
168
# File 'lib/scmd/command.rb', line 166

def stdout
  @stdout
end

Instance Method Details

#check_for_exitObject



173
174
175
176
177
178
179
# File 'lib/scmd/command.rb', line 173

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

#exitstatusObject



185
186
187
188
# File 'lib/scmd/command.rb', line 185

def exitstatus
  return nil if @wait_status.nil?
  @wait_status.exitstatus || @wait_status.termsig
end

#flush_stderrObject



205
# File 'lib/scmd/command.rb', line 205

def flush_stderr; @stderr.read; end

#flush_stdoutObject



204
# File 'lib/scmd/command.rb', line 204

def flush_stdout; @stdout.read; end

#read(size) ⇒ Object



197
198
199
200
201
202
# File 'lib/scmd/command.rb', line 197

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

Returns:

  • (Boolean)


181
182
183
# File 'lib/scmd/command.rb', line 181

def running?
  @wait_pid.nil?
end

#teardownObject



207
208
209
# File 'lib/scmd/command.rb', line 207

def teardown
  [@stdin, @stdout, @stderr].each{ |fd| fd.close if fd && !fd.closed? }
end

#write(input) ⇒ Object



190
191
192
193
194
195
# File 'lib/scmd/command.rb', line 190

def write(input)
  if !input.nil?
    [*input].each{ |line| @stdin.puts line.to_s }
    @stdin.close
  end
end