class OkComputer::HttpCheck
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.
Constants
- ConnectionFailed
Attributes
Public Class Methods
Source
# File lib/ok_computer/built_in_checks/http_check.rb, line 18 def initialize(url, request_timeout = 5) parse_url(url) self.request_timeout = request_timeout.to_i end
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.
Public Instance Methods
Source
# File lib/ok_computer/built_in_checks/http_check.rb, line 58 def basic_auth_options [self.basic_auth_username, self.basic_auth_password] end
Source
# File lib/ok_computer/built_in_checks/http_check.rb, line 24 def check if perform_request mark_message "HTTP check successful" end rescue => e mark_message "Error: '#{e}'" mark_failure end
Public: Return the status of the HTTP check
Source
# File lib/ok_computer/built_in_checks/http_check.rb, line 50 def parse_url(url) self.url = URI.parse(url) if self.url.userinfo self.basic_auth_username, self.basic_auth_password = self.url.userinfo.split(':') self.url.userinfo = '' end end
Source
# File lib/ok_computer/built_in_checks/http_check.rb, line 36 def perform_request Timeout.timeout(request_timeout) do options = { read_timeout: request_timeout } if basic_auth_options.any? options[:http_basic_authentication] = basic_auth_options end url.read(options) end rescue => e raise ConnectionFailed, e end
Public: Actually performs the request against the URL. Returns response body if the request was successful. Otherwise raises a HttpCheck::ConnectionFailed error.