Class: Toys::Utils::Exec::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/utils/exec.rb

Overview

An object that controls a subprocess. This object is returned from an execution running in the background, or is yielded to a control block for an execution running in the foreground. You may use this object to interact with the subcommand's streams, send signals to the process, and get its result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errIO? (readonly)

Return the subcommand's standard error stream (which can be read from), if the command was configured with err: :controller. Returns nil otherwise.

Returns:

  • (IO, nil)


432
433
434
# File 'lib/toys/utils/exec.rb', line 432

def err
  @err
end

#inIO? (readonly)

Return the subcommand's standard input stream (which can be written to), if the command was configured with in: :controller. Returns nil otherwise.

Returns:

  • (IO, nil)


416
417
418
# File 'lib/toys/utils/exec.rb', line 416

def in
  @in
end

#outIO? (readonly)

Return the subcommand's standard output stream (which can be read from), if the command was configured with out: :controller. Returns nil otherwise.

Returns:

  • (IO, nil)


424
425
426
# File 'lib/toys/utils/exec.rb', line 424

def out
  @out
end

#pidInteger (readonly)

Returns the process ID.

Returns:

  • (Integer)


438
439
440
# File 'lib/toys/utils/exec.rb', line 438

def pid
  @pid
end

Instance Method Details

#executing?Boolean

Determine whether the subcommand is still executing

Returns:

  • (Boolean)


455
456
457
# File 'lib/toys/utils/exec.rb', line 455

def executing?
  @wait_thread.status ? true : false
end

#kill(signal) ⇒ Object

Send the given signal to the process. The signal may be specified by name or number.

Parameters:

  • signal (Integer, String)

    The signal to send.



446
447
448
# File 'lib/toys/utils/exec.rb', line 446

def kill(signal)
  ::Process.kill(signal, pid)
end

#result(timeout: nil) ⇒ Toys::Utils::Exec::Result?

Wait for the subcommand to complete, and return a result object.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    The timeout in seconds, or nil to wait indefinitely.

Returns:



467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/toys/utils/exec.rb', line 467

def result(timeout: nil)
  return nil unless @wait_thread.join(timeout)
  @result ||= begin
    close_streams
    @join_threads.each(&:join)
    status = @wait_thread.value
    if @nonzero_status_handler && status.exitstatus != 0
      @nonzero_status_handler.call(status)
    end
    Result.new(@captures[:out], @captures[:err], status)
  end
end