Skip to content

Commit

Permalink
Merge pull request #205 from pulibrary/i176-jrgriffiniii-health-endpoint
Browse files Browse the repository at this point in the history
Integrates a health endpoint which ensures that Solr availability can be determined by HTTP request
  • Loading branch information
carolyncole authored Oct 7, 2024
2 parents e0e2275 + 4d2763b commit d9661b1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem "grape", "~> 1.8"
gem "grape-entity", "~> 0.9"
gem "grape-swagger", "~> 1.4"
gem "grape-swagger-rails", "~> 0.4"
gem "health-monitor-rails", "~> 12.3"
gem "honeybadger", "~> 5.8"
gem "jbuilder", "~> 2.0"
gem "jquery-rails", "~> 4.5"
Expand Down Expand Up @@ -72,5 +73,6 @@ group :development, :test do
gem "rails-controller-testing"
gem "rspec-rails", "5.1"
gem "simplecov", "~> 0.22"
gem "webmock"
gem "yard"
end
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ GEM
term-ansicolor (~> 1.7)
thor (~> 1.2)
tins (~> 1.32)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6)
dartsass-rails (0.5.0)
railties (>= 6.0.0)
Expand Down Expand Up @@ -209,7 +212,10 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
hashdiff (1.1.1)
hashie (5.0.0)
health-monitor-rails (12.3.0)
railties (>= 6.1)
highline (2.1.0)
honeybadger (5.8.0)
i18n (1.14.5)
Expand Down Expand Up @@ -500,6 +506,10 @@ GEM
zeitwerk (~> 2.2)
warden (1.2.9)
rack (>= 2.0.9)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand Down Expand Up @@ -553,6 +563,7 @@ DEPENDENCIES
grape-swagger (~> 1.4)
grape-swagger-rails (~> 0.4)
guard-rspec
health-monitor-rails (~> 12.3)
honeybadger (~> 5.8)
jbuilder (~> 2.0)
jquery-rails (~> 4.5)
Expand Down Expand Up @@ -584,6 +595,7 @@ DEPENDENCIES
sunspot_rails (~> 2.6)
sunspot_solr (~> 2.6)
vite_rails (~> 3.0)
webmock
will_paginate (~> 3.3)
yard

Expand Down
21 changes: 21 additions & 0 deletions config/initializers/health_monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
Rails.application.config.after_initialize do
HealthMonitor.configure do |config|
config.cache

config.solr.configure do |c|
url = Sunspot.config.solr.url
uri = URI(url)
status_uri = URI("#{uri.scheme}://#{uri.hostname}:#{uri.port}/solr/admin/cores?action=STATUS")

c.url = status_uri.to_s
end

config.path = :health

config.error_callback = proc do |e|
Rails.logger.error "Health check failed with: #{e.message}"
Honeybadger.notify(e)
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
mount HealthMonitor::Engine, at: "/"
root to: "application#start"

get("/start", to: "application#start", as: "start")
Expand Down
47 changes: 47 additions & 0 deletions spec/requests/health_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "Health Check", type: :request do
describe "GET /health" do
context "when the service is up" do
before do
url = Sunspot.config.solr.url
uri = URI(url)

response_body = {
responseHeader: { status: 0 }
}
stub_request(:get, "#{uri.scheme}://#{uri.hostname}:#{uri.port}/solr/admin/cores?action=STATUS").to_return(status: 200, body: response_body.to_json)
end

it "has a health check" do
get "/health.json"

expect(response).to be_successful
end
end

context "when the service is down" do
before do
url = Sunspot.config.solr.url
uri = URI(url)

response_body = {
responseHeader: { status: 500 }
}
stub_request(:get, "#{uri.scheme}://#{uri.hostname}:#{uri.port}/solr/admin/cores?action=STATUS").to_return(status: 200, body: response_body.to_json)
end

it "errors when a service is down" do
get "/health.json"

expect(response).not_to be_successful
expect(response.status).to eq 503
json_response = JSON.parse(response.body)
results = json_response["results"]
solr_response = results.find { |x| x["name"] == "Solr" }
expect(solr_response["message"]).to start_with "The solr has an invalid status"
end
end
end
end
3 changes: 3 additions & 0 deletions spec/requests/waiver_infos_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
before do
waiver_info
waiver_info2
waiver_info.index!

sign_in(admin_user)
end

Expand Down Expand Up @@ -262,6 +264,7 @@
before do
waiver_info
waiver_info2
stub_request(:post, "#{Sunspot.config.solr.url}/select?wt=json").to_return(status: 404)
sign_in(admin_user)
end

Expand Down
5 changes: 4 additions & 1 deletion spec/services/waiver_info_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
describe WaiverInfoReport do
subject(:waiver_info_report) { described_class.new(models: models, path: path) }
let(:models) { WaiverInfo.all }
let(:path) { "tmp/waiver_info_report.csv" }
let(:dir_path) { "tmp" }
let(:path) { "#{dir_path}/waiver_info_report.csv" }

describe "#generate" do
let(:waiver_info1) { FactoryBot.create(:waiver_info) }
let(:waiver_info2) { FactoryBot.create(:waiver_info) }

before do
Dir.mkdir(dir_path) unless File.exist?(dir_path)

fh = File.new(path, "w+")
fh.close

Expand Down
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
require "rspec/retry"

require "webmock/rspec"

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down Expand Up @@ -30,6 +32,14 @@
ex.run_with_retry retry: 3
end

config.before :example do |_ex|
stub_request(:post, "#{Sunspot.config.solr.url}/update?wt=json").to_return(status: 200)
response_body = {
docs: []
}
stub_request(:post, "#{Sunspot.config.solr.url}/select?wt=json").to_return(status: 200, body: response_body.to_json)
end

# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
Expand Down

0 comments on commit d9661b1

Please sign in to comment.