Class: LogicalConstruct::TCPPortOpenCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/logical-construct/port-open-check.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) {|_self| ... } ⇒ TCPPortOpenCheck

Returns a new instance of TCPPortOpenCheck.

Yields:

  • (_self)

Yield Parameters:



3
4
5
6
7
8
9
# File 'lib/logical-construct/port-open-check.rb', line 3

def initialize(port)
  @host = "localhost"
  @port = port
  @timeout = 0
  @retry_delay = 0.5
  yield self if block_given?
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/logical-construct/port-open-check.rb', line 10

def host
  @host
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/logical-construct/port-open-check.rb', line 10

def port
  @port
end

#retry_delayObject

Returns the value of attribute retry_delay.



11
12
13
# File 'lib/logical-construct/port-open-check.rb', line 11

def retry_delay
  @retry_delay
end

#timeoutObject

Returns the value of attribute timeout.



11
12
13
# File 'lib/logical-construct/port-open-check.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#fail_if_closed!Object



37
38
39
# File 'lib/logical-construct/port-open-check.rb', line 37

def fail_if_closed!
  raise "Port is closed, should be open: #{@host}:#{@port}" unless open?
end

#fail_if_open!Object



33
34
35
# File 'lib/logical-construct/port-open-check.rb', line 33

def fail_if_open!
  raise "Port is open, should be closed: #{@host}:#{@port}" if open?(0)
end

#open?(timeout = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/logical-construct/port-open-check.rb', line 17

def open?(timeout = nil)
  timeout ||= @timeout
  start_time = Time.now
  test_conn = open_socket()
  return true
rescue Errno::ECONNREFUSED
  if Time.now - start_time > timeout
    return false
  else
    sleep @retry_delay
    retry
  end
ensure
  test_conn.close if test_conn.respond_to? :close
end

#open_socketObject



13
14
15
# File 'lib/logical-construct/port-open-check.rb', line 13

def open_socket
  TCPSocket.new @host, @port
end