-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from pulibrary/add_solr_monitor
Add solr monitor
- Loading branch information
Showing
9 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,6 @@ gem 'sidekiq', '>= 3.0' | |
gem 'spork' | ||
gem 'sqlite3', '~> 1.3' | ||
gem 'timecop' | ||
gem 'webmock' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,7 @@ The following services are currently supported: | |
* Sidekiq | ||
* Resque | ||
* Delayed Job | ||
* Solr | ||
|
||
## Configuration | ||
|
||
|
@@ -312,6 +313,10 @@ Please note that `url` or `connection` can't be used at the same time. | |
|
||
* `queue_size`: the size (maximum) of a queue which is considered unhealthy (the default is 100). | ||
|
||
### Solr | ||
|
||
* `url`: the URL used to connect to your Solr instance - must be a string. You can also use `url` to explicitly configure authentication (e.g., `'https://user:[email protected]:8983/'`) | ||
|
||
### Adding a Custom Provider | ||
|
||
It's also possible to add custom health check providers suited for your needs (of course, it's highly appreciated and encouraged if you'd contribute useful providers to the project). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ gem 'sidekiq', '>= 3.0' | |
gem 'spork' | ||
gem 'sqlite3', '~> 1.3' | ||
gem 'timecop' | ||
gem 'webmock' | ||
|
||
gemspec path: '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,6 @@ gem 'sidekiq', '>= 3.0' | |
gem 'spork' | ||
gem 'sqlite3', '~> 1.3' | ||
gem 'timecop' | ||
gem 'webmock' | ||
|
||
gemspec path: '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'health_monitor/providers/base' | ||
|
||
module HealthMonitor | ||
module Providers | ||
class SolrException < StandardError; end | ||
|
||
class Solr < Base | ||
class Configuration < Base::Configuration | ||
DEFAULT_URL = nil | ||
attr_accessor :url | ||
|
||
def initialize(provider) | ||
super(provider) | ||
|
||
@url = DEFAULT_URL | ||
end | ||
end | ||
|
||
def check! | ||
check_solr_connection! | ||
rescue Exception => e | ||
raise SolrException.new(e.message) | ||
end | ||
|
||
private | ||
|
||
def configuration_class | ||
::HealthMonitor::Providers::Solr::Configuration | ||
end | ||
|
||
def check_solr_connection! | ||
json = JSON.parse(solr_response.body) | ||
raise "The solr has an invalid status #{status_uri}" if json['responseHeader']['status'] != 0 | ||
end | ||
|
||
def status_uri | ||
@status_uri ||= begin | ||
uri = URI(configuration.url) | ||
uri.path = '/solr/admin/cores' | ||
uri.query = 'action=STATUS' | ||
uri | ||
end | ||
end | ||
|
||
def solr_request | ||
@solr_request ||= begin | ||
req = Net::HTTP::Get.new(status_uri) | ||
req.basic_auth(status_uri.user, status_uri.password) if status_uri.user && status_uri.password | ||
req | ||
end | ||
end | ||
|
||
def solr_response | ||
Net::HTTP.start(status_uri.hostname, status_uri.port) { |http| http.request(solr_request) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe HealthMonitor::Providers::Solr do | ||
subject { described_class.new } | ||
|
||
context 'with defaults' do | ||
it { expect(subject.configuration.name).to eq('Solr') } | ||
it { expect(subject.configuration.url).to eq(HealthMonitor::Providers::Solr::Configuration::DEFAULT_URL) } | ||
end | ||
|
||
describe '#name' do | ||
it { expect(subject.name).to eq('Solr') } | ||
end | ||
|
||
describe '#check!' do | ||
let(:solr_url_config) { 'http://www.example-solr.com:8983' } | ||
|
||
before do | ||
subject.request = test_request | ||
subject.configure do |config| | ||
config.url = solr_url_config | ||
end | ||
Providers.stub_solr | ||
end | ||
|
||
context 'with a standard connection' do | ||
it 'checks against the configured solr url' do | ||
subject.check! | ||
expect(Providers.stub_solr).to have_been_requested | ||
end | ||
|
||
it 'succesfully checks' do | ||
expect { | ||
subject.check! | ||
}.not_to raise_error | ||
end | ||
|
||
context 'when failing' do | ||
before do | ||
Providers.stub_solr_failure | ||
end | ||
|
||
it 'fails check!' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::SolrException) | ||
end | ||
|
||
it 'checks against the configured solr url' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::SolrException) | ||
expect(Providers.stub_solr_failure).to have_been_requested | ||
end | ||
end | ||
end | ||
|
||
context 'with a configured url that includes a path' do | ||
let(:solr_url_config) { 'http://www.example-solr.com:8983/solr/blacklight-core-development' } | ||
|
||
it 'checks against the configured solr url' do | ||
subject.check! | ||
expect(Providers.stub_solr).to have_been_requested | ||
end | ||
end | ||
|
||
context 'with a connection with authentication' do | ||
let(:solr_url_config) { 'http://solr:SolrRocks@localhost:8888' } | ||
|
||
before { Providers.stub_solr_with_auth } | ||
|
||
it 'checks against the configured solr url' do | ||
subject.check! | ||
expect(Providers.stub_solr_with_auth).to have_been_requested | ||
end | ||
|
||
it 'succesfully checks' do | ||
expect { | ||
subject.check! | ||
}.not_to raise_error | ||
end | ||
|
||
context 'when failing' do | ||
before do | ||
Providers.stub_solr_failure_with_auth | ||
end | ||
|
||
it 'fails check!' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::SolrException) | ||
end | ||
|
||
it 'checks against the configured solr url' do | ||
expect { | ||
subject.check! | ||
}.to raise_error(HealthMonitor::Providers::SolrException) | ||
expect(Providers.stub_solr_failure_with_auth).to have_been_requested | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#configure' do | ||
before do | ||
subject.configure | ||
end | ||
|
||
let(:url) { 'solr://user:[email protected]:8983/' } | ||
|
||
it 'url can be configured' do | ||
expect { | ||
subject.configure do |config| | ||
config.url = url | ||
end | ||
}.to change { subject.configuration.url }.to(url) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters