Class: Nonnative::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/nonnative/port.rb

Overview

Performs TCP port readiness/shutdown checks for a configured runner.

Nonnative uses this to decide whether a process/server port is ready after start, and whether it has shut down after stop. The checks repeatedly attempt to open a TCP connection to ‘process.host` and the configured `port` until either:

  • the expected condition is met, or

  • the configured timeout elapses (in which case the method returns ‘false`)

The ‘process` argument is a runner configuration object (e.g. ConfigurationProcess or ConfigurationServer) that responds to `host` and `timeout`.

Instance Method Summary collapse

Constructor Details

#initialize(process, port = process.port) ⇒ Port

Returns a new instance of Port.

Parameters:

  • process (#host, #timeout)

    runner configuration providing connection details

  • port (Integer) (defaults to: process.port)

    port to check



20
21
22
23
24
# File 'lib/nonnative/port.rb', line 20

def initialize(process, port = process.port)
  @process = process
  @port = port
  @timeout = Nonnative::Timeout.new(process.timeout)
end

Instance Method Details

#closed?Boolean

Returns whether the configured host/port becomes non-connectable before the timeout elapses.

This method treats a successful connection as “not closed yet” and keeps retrying until it observes connection failure (returns ‘true`) or the timeout elapses (returns `false`).

Returns:

  • (Boolean)

    ‘true` if the port closed in time; otherwise `false`



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nonnative/port.rb', line 50

def closed?
  Nonnative.logger.info "checking if port '#{port}' is closed on host '#{process.host}'"

  timeout.perform do
    open_socket
    raise Nonnative::Error
  rescue Nonnative::Error
    sleep_interval
    retry
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET
    true
  end
end

#open?Boolean

Returns whether the configured host/port becomes connectable before the timeout elapses.

This method retries on common connection errors until either a connection succeeds (returns ‘true`) or the timeout elapses (returns `false`).

Returns:

  • (Boolean)

    ‘true` if the port opened in time; otherwise `false`



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nonnative/port.rb', line 32

def open?
  Nonnative.logger.info "checking if port '#{port}' is open on host '#{process.host}'"

  timeout.perform do
    open_socket
    true
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    sleep_interval
    retry
  end
end