Class: LogicalConstruct::TCPPortOpenCheck
- Inherits:
-
Object
- Object
- LogicalConstruct::TCPPortOpenCheck
- Defined in:
- lib/logical-construct/port-open-check.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#retry_delay ⇒ Object
Returns the value of attribute retry_delay.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #fail_if_closed! ⇒ Object
- #fail_if_open! ⇒ Object
-
#initialize(port) {|_self| ... } ⇒ TCPPortOpenCheck
constructor
A new instance of TCPPortOpenCheck.
- #open?(timeout = nil) ⇒ Boolean
- #open_socket ⇒ Object
Constructor Details
#initialize(port) {|_self| ... } ⇒ TCPPortOpenCheck
Returns a new instance of TCPPortOpenCheck.
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
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/logical-construct/port-open-check.rb', line 10 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/logical-construct/port-open-check.rb', line 10 def port @port end |
#retry_delay ⇒ Object
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 |
#timeout ⇒ Object
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
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_socket ⇒ Object
13 14 15 |
# File 'lib/logical-construct/port-open-check.rb', line 13 def open_socket TCPSocket.new @host, @port end |