class ServiceWait::Runner

Public Class Methods

new(ip, port, timeout = 20) click to toggle source
# File lib/service_wait/runner.rb, line 6
def initialize(ip, port, timeout = 20)
  @ip = ip
  @port = port
  @timeout = timeout
end

Public Instance Methods

wait() { |:failure| ... } click to toggle source
# File lib/service_wait/runner.rb, line 12
def wait(&block)
  Timeout::timeout(@timeout) do
    while !port_open?(@ip, @port)
      yield :failure if block_given?

      sleep 1
      yield :attempt if block_given?
    end

    yield :success if block_given?
  end

rescue Timeout::Error
  yield :timeout if block_given?
end

Private Instance Methods

port_open?(ip, port, seconds=1) click to toggle source
# File lib/service_wait/runner.rb, line 30
def port_open?(ip, port, seconds=1)
  Timeout.timeout(seconds) do
    begin
      TCPSocket.new(ip, port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      false
    end
  end

rescue Timeout::Error
  false
end