Skip to content

Commit

Permalink
Move the ping method into the repository
Browse files Browse the repository at this point in the history
This will allow different types of repositories to add their own ping
handler
  • Loading branch information
jcoyne committed Aug 1, 2018
1 parent e10e9fb commit b114541
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
7 changes: 6 additions & 1 deletion lib/blacklight/abstract_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ def search(_params = {})
raise NotImplementedError
end

##
# Query the fields that exist from the index
# @return [Hash]
def reflect_fields
raise NotImplementedError
end

##
# Is the repository in a working state?
def ping
raise NotImplementedError
end

private

def connection_config
Expand Down
8 changes: 8 additions & 0 deletions lib/blacklight/solr/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def reflect_fields
send_and_receive('admin/luke', params: { fl: '*', 'json.nl' => 'map' })['fields']
end

##
# @return [boolean] true if the repository is reachable
def ping
response = connection.send_and_receive 'admin/ping', {}
Blacklight.logger.info("Ping [#{connection.uri}] returned: '#{response['status']}'")
response['status'] == "OK"
end

##
# Execute a solr query
# TODO: Make this private after we have a way to abstract admin/luke and ping
Expand Down
21 changes: 8 additions & 13 deletions lib/railties/blacklight.rake
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ namespace :blacklight do
namespace :check do
desc "Check the Solr connection and controller configuration"
task :solr, [:controller_name] => [:environment] do
errors = 0
verbose = ENV.fetch('VERBOSE', false).present?

conn = Blacklight.default_index.connection
puts "[#{conn.uri}]"

print " - admin/ping: "
begin
response = conn.send_and_receive 'admin/ping', {}
puts response['status']
errors += 1 unless response['status'] == "OK"
conn = Blacklight.default_index
if conn.ping
puts "OK"
else
puts "Unable to reach: #{conn.uri}"
exit 1
end
rescue => e
errors += 1
puts e.to_s
exit 1
end

exit 1 if errors > 0
end

task :controller, [:controller_name] => [:environment] do |_, args|
Expand Down
10 changes: 8 additions & 2 deletions spec/models/blacklight/solr/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

RSpec.describe Blacklight::Solr::Repository, api: true do
subject do
Blacklight::Solr::Repository.new blacklight_config
subject(:repository) do
described_class.new blacklight_config
end

let :blacklight_config do
Expand Down Expand Up @@ -144,4 +144,10 @@
expect(response.docs.length).to be >= 1
end
end

describe '#ping' do
subject { repository.ping }

it { is_expected.to be true }
end
end

0 comments on commit b114541

Please sign in to comment.