class Toys::Utils::Exec::Controller


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

def capture(which)

Returns:
  • (self) -

Parameters:
  • which (:out, :err) -- Which stream to capture
def capture(which)
  stream = stream_for(which)
  @join_threads << ::Thread.new do
    begin
      @captures[which] = stream.read
    ensure
      stream.close
    end
  end
  self
end

def capture_err

Returns:
  • (self) -
def capture_err
  capture(:err)
end

def capture_out

Returns:
  • (self) -
def capture_out
  capture(:out)
end

def close_streams

Other tags:
    Private: -
def close_streams
  @in.close if @in && !@in.closed?
  @out.close if @out && !@out.closed?
  @err.close if @err && !@err.closed?
  self
end

def executing?

Returns:
  • (Boolean) -
def executing?
  @wait_thread&.status ? true : false
end

def initialize(name, controller_streams, captures, pid, join_threads, result_callback)

# @private
def initialize(name, controller_streams, captures, pid, join_threads, result_callback)
  @name = name
  @in = controller_streams[:in]
  @out = controller_streams[:out]
  @err = controller_streams[:err]
  @captures = captures
  @pid = @exception = @wait_thread = nil
  case pid
  when ::Integer
    @pid = pid
    @wait_thread = ::Process.detach(pid)
  when ::Exception
    @exception = pid
  end
  @join_threads = join_threads
  @result_callback = result_callback
  @result = nil
end

def kill(sig)

Returns:
  • (self) -

Parameters:
  • sig (Integer, String) -- The signal to send.
def kill(sig)
  ::Process.kill(sig, pid) if pid
  self
end

def redirect(which, io, *io_args)

Returns:
  • (self) -

Parameters:
  • io_args (Object...) -- The mode and permissions for opening the
  • io (IO, StringIO, String, :null) -- Where to redirect the stream
  • which (:in, :out, :err) -- Which stream to redirect
def redirect(which, io, *io_args)
  io = ::File::NULL if io == :null
  if io.is_a?(::String)
    io_args = which == :in ? ["r"] : ["w"] if io_args.empty?
    io = ::File.open(io, *io_args)
  end
  stream = stream_for(which, allow_in: true)
  @join_threads << ::Thread.new do
    begin
      if which == :in
        ::IO.copy_stream(io, stream)
      else
        ::IO.copy_stream(stream, io)
      end
    ensure
      stream.close
      io.close
    end
  end
  self
end

def redirect_err(io, *io_args)

Returns:
  • (self) -

Parameters:
  • io_args (Object...) -- The mode and permissions for opening the
  • io (IO, StringIO, String) -- Where to redirect the stream
def redirect_err(io, *io_args)
  redirect(:err, io, *io_args)
end

def redirect_in(io, *io_args)

Returns:
  • (self) -

Parameters:
  • io_args (Object...) -- The mode and permissions for opening the
  • io (IO, StringIO, String, :null) -- Where to redirect the stream
def redirect_in(io, *io_args)
  redirect(:in, io, *io_args)
end

def redirect_out(io, *io_args)

Returns:
  • (self) -

Parameters:
  • io_args (Object...) -- The mode and permissions for opening the
  • io (IO, StringIO, String, :null) -- Where to redirect the stream
def redirect_out(io, *io_args)
  redirect(:out, io, *io_args)
end

def result(timeout: nil)

Returns:
  • (nil) - if a timeout occurred.
  • (Toys::Utils::Exec::Result) - The result object

Parameters:
  • timeout (Numeric, nil) -- The timeout in seconds, or `nil` to
def result(timeout: nil)
  return nil if @wait_thread && !@wait_thread.join(timeout)
  @result ||= begin
    close_streams
    @join_threads.each(&:join)
    Result.new(name, @captures[:out], @captures[:err], @wait_thread&.value, @exception)
          .tap { |result| @result_callback&.call(result) }
  end
end

def stream_for(which, allow_in: false)

def stream_for(which, allow_in: false)
  stream = nil
  case which
  when :out
    stream = @out
    @out = nil
  when :err
    stream = @err
    @err = nil
  when :in
    if allow_in
      stream = @in
      @in = nil
    end
  else
    raise ::ArgumentError, "Unknown stream #{which}"
  end
  raise ::ArgumentError, "Stream #{which} not available" unless stream
  stream
end