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

LIBHYDRA-577: Updating about controller to ping Solr #398

Merged
merged 6 commits into from
Apr 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class ApplicationController < ActionController::Base
layout 'blacklight'
skip_after_action :discard_flash_if_xhr

def solr_connection_error(err)
Rails.logger.error(err.message)
flash[:error] = I18n.t(:solr_is_down)
end

# Causes a "404 - Not Found" error page to be displayed.
def not_found
render file: Rails.root.join('public', '404.html'), status: :not_found, layout: false
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class CatalogController < ApplicationController
include Blacklight::Catalog
before_action :make_current_query_accessible, only: %i[show index] # rubocop:disable Rails/LexicallyScopedActionFilter

rescue_from Blacklight::Exceptions::ECONNREFUSED, with: :solr_connection_error
rescue_from Blacklight::Exceptions::InvalidRequest, with: :solr_connection_error
rescue_from Blacklight::Exceptions::ECONNREFUSED, with: :goto_about_page
rescue_from Blacklight::Exceptions::InvalidRequest, with: :goto_about_page

configure_blacklight do |config| # rubocop:disable Metrics/BlockLength
## Class for sending and receiving requests from a search index
Expand Down Expand Up @@ -215,9 +215,8 @@ def self.show_edit_metadata(component)

private

def solr_connection_error(err)
Rails.logger.error(err.message)
flash[:error] = I18n.t(:solr_is_down)
def goto_about_page(err)
solr_connection_error(err)
redirect_to(about_url)
end

Expand Down
7 changes: 7 additions & 0 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

class StaticPagesController < ApplicationController
def about
# A simple ping to SOLR to see if it's running
uri = URI(ENV['SOLR_URL'])
Net::HTTP.get(uri)
rescue Errno::ECONNREFUSED => e
solr_connection_error(e)
rescue SocketError => e
solr_connection_error(e)
end
end
12 changes: 10 additions & 2 deletions test/controllers/static_pages_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ class StaticPagesControllerTest < ActionController::TestCase
end

test 'should get about' do
get :about
assert_response :success
stub_request(:get, 'http://solr-fedora4:8983/solr/fedora4')
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Host' => 'solr-fedora4:8983',
'User-Agent' => 'Ruby'
}
)
.to_return(status: 200, body: '', headers: {})
end
end