Skip to content

Commit

Permalink
Merge pull request #86 from psu-libraries/healthchecks
Browse files Browse the repository at this point in the history
version and solr checks
  • Loading branch information
whereismyjetpack authored Jul 25, 2022
2 parents a8debb5 + 85db53c commit af2c1ba
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gem 'importmap-rails'
gem 'jbuilder'
gem 'jquery-rails'
gem 'mysql2'
gem 'okcomputer'
gem 'puma', '~> 5.0'
gem 'rails', '~> 7.0.3'
gem 'redis', '~> 4.0'
Expand All @@ -44,6 +45,7 @@ group :test do
gem 'rspec-rails'
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'webmock'
end

group :development, :test do
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ GEM
coderay (1.1.3)
colorize (0.8.1)
concurrent-ruby (1.1.10)
crack (0.4.5)
rexml
crass (1.0.6)
deprecation (1.1.0)
activesupport
Expand Down Expand Up @@ -242,6 +244,7 @@ GEM
builder (>= 3.1.0)
faraday (< 3)
faraday-follow_redirects (>= 0.3.0, < 2)
okcomputer (1.18.4)
orm_adapter (0.5.0)
ostruct (0.5.5)
parallel (1.22.1)
Expand Down Expand Up @@ -419,6 +422,10 @@ GEM
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
webmock (3.14.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
Expand Down Expand Up @@ -451,6 +458,7 @@ DEPENDENCIES
jquery-rails
mysql2
niftany (>= 0.10)
okcomputer
pry-byebug
puma (~> 5.0)
rails (~> 7.0.3)
Expand All @@ -469,6 +477,7 @@ DEPENDENCIES
tzinfo-data
web-console
webdrivers
webmock

RUBY VERSION
ruby 3.1.2p20
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module EtdaExplore
class Application < Rails::Application
require 'etda_explore/solr_config'
require 'overrides/resumption_token'
require 'healthchecks'

config.solr = EtdaExplore::SolrConfig.new

Expand Down
15 changes: 15 additions & 0 deletions config/initializers/okcomputer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

OkComputer.mount_at = false

OkComputer::Registry.register(
'version',
HealthChecks::VersionCheck.new
)

OkComputer::Registry.register(
'solr',
HealthChecks::SolrCheck.new
)

OkComputer.make_optional %w(version)
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
concern :searchable, Blacklight::Routes::Searchable.new
concern :oai_provider, BlacklightOaiProvider::Routes.new

mount OkComputer::Engine, at: '/health'

authenticate :user do
get '/login', to: 'application#login', as: :login
end
Expand Down
6 changes: 6 additions & 0 deletions lib/healthchecks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Healthchecks
require 'healthchecks/version_check'
require 'healthchecks/solr_check'
end
18 changes: 18 additions & 0 deletions lib/healthchecks/solr_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module HealthChecks
class SolrCheck < OkComputer::Check
def check
solr = RSolr.connect(url: Rails.configuration.solr.query_url)
resp = solr.get('admin/ping')
status = resp&.dig('status')
zk_connected = resp&.dig('responseHeader')&.dig('zkConnected')
@message = Hash.new
@message['zkConnected'] = zk_connected
@message['status'] = status
mark_message(@message)
mark_failure unless status == 'OK'
mark_failure unless zk_connected == true
end
end
end
10 changes: 10 additions & 0 deletions lib/healthchecks/version_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module HealthChecks
class VersionCheck < OkComputer::Check
def check
version = ENV.fetch('APP_VERSION', 'unknown')
mark_message version.to_s
end
end
end
51 changes: 51 additions & 0 deletions spec/lib/healthchecks/solr_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe HealthChecks::SolrCheck do
describe '#check' do
before(:all) do
WebMock.disable_net_connect!
end

after(:all) do
WebMock.enable_net_connect!
end

context 'when we can connect to solr' do
before do
stub_request(:get, /admin\/ping/)
.to_return(status: 200, body: {
responseHeader: {
zkConnected: true
},
status: 'OK'
}.to_json)
end

it 'returns no failure' do
hc = described_class.new
hc.check
expect(hc.failure_occurred).to be_nil
end
end

context 'when zk is not connected' do
before do
stub_request(:get, /admin\/ping/)
.to_return(status: 200, body: {
responseHeader: {
zkConnected: false
},
status: 'OK'
}.to_json)
end

it 'returns a failure' do
hc = described_class.new
hc.check
expect(hc.failure_occurred).to be true
end
end
end
end
37 changes: 37 additions & 0 deletions spec/lib/healthchecks/version_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe HealthChecks::VersionCheck do
describe '#check' do
context 'when version' do
before do
ENV['APP_VERSION'] = '3'
end

it 'returns no failure' do
hc = described_class.new
hc.check
expect(hc.failure_occurred).to be_nil
end

it 'writes a message' do
hc = described_class.new
hc.check
expect(hc.message).to eq('3')
end
end

context 'when no version' do
before do
ENV['APP_VERSION'] = nil
end

it 'writes unknown' do
hc = described_class.new
hc.check
expect(hc.message).to eq('unknown')
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require 'simplecov'
require 'webmock/rspec'
SimpleCov.start
WebMock.allow_net_connect!

# 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`.
Expand Down

0 comments on commit af2c1ba

Please sign in to comment.