Class: OkComputer::HttpCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/http_check.rb

Overview

Performs a health check by reading a URL over HTTP. A successful response is considered passing. To implement your own pass/fail criteria, inherit from this class, override #check, and call #perform_request to get the response body.

Direct Known Subclasses

ElasticsearchCheck, SolrCheck

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name

Instance Method Summary collapse

Methods inherited from Check

#clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text

Constructor Details

#initialize(url, request_timeout = 5) ⇒ HttpCheck

Public: Initialize a new HTTP check.

url - The URL to check request_timeout - How long to wait to connect before timing out. Defaults to 5 seconds.



19
20
21
22
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 19

def initialize(url, request_timeout = 5)
  self.url = URI(url)
  self.request_timeout = request_timeout.to_i
end

Instance Attribute Details

#request_timeoutObject

Returns the value of attribute request_timeout.



13
14
15
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 13

def request_timeout
  @request_timeout
end

#urlObject

Returns the value of attribute url.



12
13
14
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 12

def url
  @url
end

Instance Method Details

#checkObject

Public: Return the status of the HTTP check



25
26
27
28
29
30
31
32
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 25

def check
  if perform_request
    mark_message "HTTP check successful"
  end
rescue => e
  mark_message "Error: '#{e}'"
  mark_failure
end

#perform_requestObject

Public: Actually performs the request against the URL. Returns response body if the request was successful. Otherwise raises a HttpCheck::ConnectionFailed error.



37
38
39
40
41
42
43
# File 'lib/ok_computer/built_in_checks/http_check.rb', line 37

def perform_request
  timeout(request_timeout) do
    url.read(read_timeout: request_timeout)
  end
rescue => e
  raise ConnectionFailed, e
end