class R10K::Util::Subprocess::Runner::Pump

Perform nonblocking reads on a streaming IO instance.

@api private

Attributes

max_delay[RW]

@!attribute [r] #max_delay

@return [Float] The maximum time to wait while polling the IO device
min_delay[RW]

@!attribute [r] #min_delay

@return [Float] The minimum time to wait while polling the IO device
string[R]

!@attribute [r] string

@return [String] The output collected from the IO device

Public Class Methods

new(io) click to toggle source
# File lib/r10k/util/subprocess/runner/pump.rb, line 20
def initialize(io)
  @io     = io
  @thread = nil
  @string = ''
  @run    = true
  @min_delay = 0.05
  @max_delay = 1.0
end

Public Instance Methods

halt!() click to toggle source
# File lib/r10k/util/subprocess/runner/pump.rb, line 33
def halt!
  @run = false
  @thread.join
end
start() click to toggle source
# File lib/r10k/util/subprocess/runner/pump.rb, line 29
def start
  @thread = Thread.new { pump }
end
wait() click to toggle source

Block until the pumping thread reaches EOF on the IO object.

# File lib/r10k/util/subprocess/runner/pump.rb, line 39
def wait
  @thread.join
end

Private Instance Methods

pump() click to toggle source
# File lib/r10k/util/subprocess/runner/pump.rb, line 45
def pump
  backoff = @min_delay
  while @run
    begin
      @string << @io.read_nonblock(4096)
      backoff /= 2 if backoff > @min_delay
    rescue Errno::EWOULDBLOCK, Errno::EAGAIN
      backoff *= 2 if backoff < @max_delay
      IO.select([@io], [], [], backoff)
    rescue EOFError
      @run = false
    end
  end
end