class OkComputer::PingCheck

Performs a health check by making a TCPSocket request to the host and port. A non-error response is considered passing.

Constants

ConnectionFailed

Attributes

host[RW]
port[RW]
request_timeout[RW]

Public Class Methods

new(host, port, request_timeout = 5) click to toggle source

Public: Initialize a new ping check.

host - the hostname port - the port, as a string request_timeout - How long to wait to connect before timing out. Defaults to 5 seconds.

# File lib/ok_computer/built_in_checks/ping_check.rb, line 14
def initialize(host, port, request_timeout = 5)
  raise ArgumentError if host.blank? || port.blank?
  self.host = host
  self.port = port
  self.request_timeout = request_timeout.to_i
end

Public Instance Methods

check() click to toggle source

Public: Return the status of the Ping check

# File lib/ok_computer/built_in_checks/ping_check.rb, line 22
def check
  tcp_socket_request
  mark_message "Ping check to #{host}:#{port} successful"
rescue => e
  mark_message "Error: '#{e}'"
  mark_failure
end

Private Instance Methods

tcp_socket_request() click to toggle source

Returns true if the request was successful. Otherwise raises a PingCheck::ConnectionFailed error.

# File lib/ok_computer/built_in_checks/ping_check.rb, line 34
def tcp_socket_request
  Timeout.timeout(request_timeout) do
    s = TCPSocket.new(host, port)
    s.close
  end
rescue Errno::ECONNREFUSED => e
  addl_message = "#{host} is not accepting connections on port #{port}: "
  raise ConnectionFailed, addl_message + e.message
rescue SocketError => e
  addl_message = "connection to #{host} on port #{port} failed with '#{e.message}': "
  raise ConnectionFailed, addl_message + e.message
rescue Timeout::Error => e
  addl_message = "#{host} did not respond on port #{port} within #{request_timeout} seconds: "
  raise ConnectionFailed, addl_message + e.message
rescue => e
  raise ConnectionFailed, e
end