Skip to content
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

Implement /health/readiness without using Timeout.timeout #1304

Merged
merged 3 commits into from Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions app/controllers/health_controller.rb
Expand Up @@ -22,13 +22,20 @@ def api_key_tester
private

def run_mongo_check
Timeout.timeout(0.75) do
# collections might be empty which is ok but it will raise an exception if
# database cannot be contacted
Mongoid.default_client.collections
end
# collections might be empty which is ok but it will raise an exception if
# database cannot be contacted
impatient_mongoid_client.collections
{ check_name: 'mongo', ok: true }
rescue StandardError => e
{ check_name: 'mongo', ok: false, error_details: "#{e.class}: #{e.message}" }
{ check_name: 'mongo', ok: false, error_details: e.class.to_s }
end

def impatient_mongoid_client
Mongo::Client.new(
Errbit::Config.mongo_url,
server_selection_timeout: 0.5,
connect_timeout: 0.5,
socket_timeout: 0.5
)
end
end
4 changes: 3 additions & 1 deletion spec/requests/health_controller_spec.rb
Expand Up @@ -8,7 +8,9 @@
end

it 'can indicate if a check fails' do
expect(Mongoid.default_client).to receive(:collections).and_raise(Mongo::Error::NoServerAvailable)
expect(Errbit::Config).to receive(:mongo_url) {
'mongodb://localhost:27000'
}
get '/health/readiness'
expect(response).to be_error
parsed_response = JSON.parse(response.body)
Expand Down