Class: Toys::Utils::Exec::Controller
- Inherits:
-
Object
- Object
- Toys::Utils::Exec::Controller
- 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
-
#err ⇒ IO?
readonly
Return the subcommand's standard error stream (which can be read from), if the command was configured with
err: :controller
. -
#in ⇒ IO?
readonly
Return the subcommand's standard input stream (which can be written to), if the command was configured with
in: :controller
. -
#out ⇒ IO?
readonly
Return the subcommand's standard output stream (which can be read from), if the command was configured with
out: :controller
. -
#pid ⇒ Integer
readonly
Returns the process ID.
Instance Method Summary collapse
-
#executing? ⇒ Boolean
Determine whether the subcommand is still executing.
-
#kill(signal) ⇒ Object
Send the given signal to the process.
-
#result(timeout: nil) ⇒ Toys::Utils::Exec::Result?
Wait for the subcommand to complete, and return a result object.
Instance Attribute Details
#err ⇒ IO? (readonly)
Return the subcommand's standard error stream (which can be read
from), if the command was configured with err: :controller
.
Returns nil
otherwise.
432 433 434 |
# File 'lib/toys/utils/exec.rb', line 432 def err @err end |
#in ⇒ IO? (readonly)
Return the subcommand's standard input stream (which can be written
to), if the command was configured with in: :controller
.
Returns nil
otherwise.
416 417 418 |
# File 'lib/toys/utils/exec.rb', line 416 def in @in end |
#out ⇒ IO? (readonly)
Return the subcommand's standard output stream (which can be read
from), if the command was configured with out: :controller
.
Returns nil
otherwise.
424 425 426 |
# File 'lib/toys/utils/exec.rb', line 424 def out @out end |
#pid ⇒ Integer (readonly)
Returns the process ID.
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
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.
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.
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 |