Class: OkComputer::HttpCheck
- 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
Constant Summary collapse
- ConnectionFailed =
Class.new(StandardError)
Constants inherited from Check
Instance Attribute Summary collapse
-
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from Check
#failure_occurred, #message, #registrant_name
Instance Method Summary collapse
-
#check ⇒ Object
Public: Return the status of the HTTP check.
-
#initialize(url, request_timeout = 5) ⇒ HttpCheck
constructor
Public: Initialize a new HTTP check.
-
#perform_request ⇒ Object
Public: Actually performs the request against the URL.
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_timeout ⇒ Object
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 |
#url ⇒ Object
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
#check ⇒ Object
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 "HTTP check successful" end rescue => e "Error: '#{e}'" mark_failure end |
#perform_request ⇒ Object
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 |