-
Notifications
You must be signed in to change notification settings - Fork 0
AP 547: Add other healthchecks to Geodata #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cfa5c6a
Add http head check etc.
yzhoubk b8fed47
consolidated
yzhoubk 5c6fbc0
uncheck Lint/MissingSuper rule
yzhoubk abc3ad6
code formating
yzhoubk b3fb91e
Handling incorrect server type
yzhoubk b474242
change url check
yzhoubk 36a1dfc
Refactor to follow the same registry call style in okcomputer.rb
yzhoubk bb8d737
Remove spatial server ucb endpoint check
yzhoubk c0b8678
Add rspec tests for endpoint http head checks
yzhoubk 71904ba
HttpHeadCheck with blank url
yzhoubk 9b0d79b
Handle special cases
yzhoubk b460b5b
Update message
yzhoubk 331d7ad
Update a test
yzhoubk 0c4b921
To store checkpoint urls directly in Environment variables
yzhoubk a3af94a
Updated to store health check endpoint urls in environment variables
yzhoubk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| module GeoDataHealthCheck | ||
| class HttpHeadCheck < OkComputer::Check | ||
| ConnectionFailed = Class.new(StandardError) | ||
| attr_accessor :url, :request_timeout | ||
|
|
||
| # rubocop:disable Lint/MissingSuper | ||
| def initialize(url, request_timeout = 5) | ||
| self.url = url | ||
| self.request_timeout = request_timeout | ||
| end | ||
| # rubocop:enable Lint/MissingSuper | ||
|
|
||
| def check | ||
| response = perform_request | ||
|
|
||
| if response.is_a?(Net::HTTPOK) || response.is_a?(Net::HTTPRedirection) | ||
| mark_message 'Http head check successful.' | ||
| else | ||
| mark_message "Error: '#{url}' http head check responded, but returned unexpeced HTTP status: #{response.code} #{response.class}. Expected 200 Net::HTTPOK." | ||
| mark_failure | ||
| end | ||
| rescue StandardError => e | ||
| mark_message "Error: '#{e.message}'" | ||
| mark_failure | ||
| end | ||
|
|
||
| def perform_request | ||
| head_request | ||
| rescue Net::OpenTimeout, Net::ReadTimeout => e | ||
| raise ConnectionFailed, "#{url} did not respond within #{request_timeout} seconds: #{e.message}" | ||
| rescue ArgumentError => e | ||
| raise ConnectionFailed, "Invalid URL format for '#{url}': #{e.class}: #{e.message}" | ||
| rescue StandardError => e | ||
| raise ConnectionFailed, e.message | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def head_request | ||
| uri = URI(url) | ||
| Net::HTTP.start( | ||
| uri.host, | ||
| uri.port, | ||
| use_ssl: uri.scheme == 'https', | ||
| verify_mode: OpenSSL::SSL::VERIFY_PEER, | ||
| open_timeout: request_timeout, | ||
| read_timeout: request_timeout | ||
| ) do |http| | ||
| http.head(uri.request_uri) | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| require 'rails_helper' | ||
| require_relative '../../lib/http_head_check' | ||
|
|
||
| RSpec.describe GeoDataHealthCheck::HttpHeadCheck do | ||
| let(:url) { 'https://example.com/endpoint' } | ||
| subject(:check) { described_class.new(url) } | ||
|
|
||
| describe 'initialization' do | ||
| it 'sets the URL' do | ||
| expect(check.url).to eq url | ||
| end | ||
|
|
||
| it 'sets default timeout to 5 seconds' do | ||
| expect(check.request_timeout).to eq 5 | ||
| end | ||
|
|
||
| it 'allows custom timeout' do | ||
| check = described_class.new(url, 10) | ||
| expect(check.request_timeout).to eq 10 | ||
| end | ||
| end | ||
|
|
||
| describe '#check' do | ||
| context 'when request returns 200 OK' do | ||
| it 'marks check as successful' do | ||
| response = Net::HTTPOK.new('1.1', 200, 'OK') | ||
| allow(check).to receive(:perform_request).and_return(response) | ||
|
|
||
| check.check | ||
|
|
||
| expect(check.message).to include('Http head check successful.') | ||
| end | ||
| end | ||
|
|
||
| context 'when request returns 500 error' do | ||
| it 'marks check as failed' do | ||
| response = Net::HTTPInternalServerError.new('1.1', 500, 'Error') | ||
| allow(check).to receive(:perform_request).and_return(response) | ||
|
|
||
| check.check | ||
|
|
||
| expect(check.message).to include('Error') | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| describe '#perform_request' do | ||
| it 'Net::OpenTimeout with ConnectionFailed and formated message' do | ||
| check_with_timeout = described_class.new(url, 7) | ||
| allow(check_with_timeout).to receive(:head_request).and_raise(Net::OpenTimeout.new('open timeout')) | ||
|
|
||
| expect { check_with_timeout.perform_request }.to raise_error( | ||
| GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, | ||
| a_string_including('did not respond within 7 seconds: open timeout') | ||
| ) | ||
| end | ||
|
|
||
| it 'Net::ReadTimeout with ConnectionFailed and formated message' do | ||
| check_with_timeout = described_class.new(url, 9) | ||
| allow(check_with_timeout).to receive(:head_request).and_raise(Net::ReadTimeout.new('read timeout')) | ||
|
|
||
| expect { check_with_timeout.perform_request }.to raise_error( | ||
| GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, | ||
| a_string_including('did not respond within 9 seconds: Net::ReadTimeout') | ||
| ) | ||
| end | ||
|
|
||
| it 'StandardError and passes through the message' do | ||
| err_msg = 'generic failure' | ||
| allow(check).to receive(:head_request).and_raise(StandardError, err_msg) | ||
|
|
||
| expect { check.perform_request }.to raise_error( | ||
| GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, | ||
| err_msg | ||
| ) | ||
| end | ||
|
|
||
| it 'wraps ArgumentError with ConnectionFailed and includes URL and error class' do | ||
| bad_check = described_class.new(url) | ||
| allow(bad_check).to receive(:head_request).and_raise(ArgumentError, 'invalid URI') | ||
|
|
||
| expect { bad_check.perform_request }.to raise_error( | ||
| GeoDataHealthCheck::HttpHeadCheck::ConnectionFailed, | ||
| a_string_including("Invalid URL format for '#{url}'", 'ArgumentError', 'invalid URI') | ||
| ) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? Could we just do the following instead?
Alternately, you could omit this line here and modify
#perform_requestto use the class fromOkComputerdirectly.