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, options) ⇒ ChildProcess

Returns a new instance of ChildProcess.



180
181
182
183
184
185
186
187
# File 'lib/scmd/command.rb', line 180

def initialize(cmd_str, env, options)
  @pid, @stdin, @stdout, @stderr = *::POSIX::Spawn.popen4(
    env,
    cmd_str,
    options,
  )
  @wait_pid, @wait_status = nil, nil
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



178
179
180
# File 'lib/scmd/command.rb', line 178

def pid
  @pid
end

#stderrObject (readonly)

Returns the value of attribute stderr.



178
179
180
# File 'lib/scmd/command.rb', line 178

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



178
179
180
# File 'lib/scmd/command.rb', line 178

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



178
179
180
# File 'lib/scmd/command.rb', line 178

def stdout
  @stdout
end

Instance Method Details

#check_for_exitObject



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

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



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

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

#flush_stderrObject



232
233
234
# File 'lib/scmd/command.rb', line 232

def flush_stderr
  @stderr.read
end

#flush_stdoutObject



228
229
230
# File 'lib/scmd/command.rb', line 228

def flush_stdout
  @stdout.read
end

#read(size) ⇒ Object



213
214
215
216
217
218
219
220
221
222
# File 'lib/scmd/command.rb', line 213

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)


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

def running?
  @wait_pid.nil?
end

#send_signal(sig) ⇒ Object



224
225
226
# File 'lib/scmd/command.rb', line 224

def send_signal(sig)
  process_kill(sig, pid)
end

#teardownObject



236
237
238
# File 'lib/scmd/command.rb', line 236

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

#write(input) ⇒ Object



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

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