Skip to content

Commit

Permalink
Added support for custom providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Beder committed Apr 20, 2015
1 parent 5f56ba8 commit 0a19410
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 20 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -75,6 +75,28 @@ The currently supported settings are:

* `latency`: the latency (in seconds) of a queue (now - when the oldest job was enqueued) which is considered unhealthy (the default is 30 seconds, but larger processing queue should have a larger latency value).

### 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).

In order to add a custom provider, you'd need to:

* Implement the `HealthMonitor::Providers::Base` class and its `check!` method (a check is considered as failed if it raises an exception):

```ruby
class CustomProvider < HealthMonitor::Providers::Base
def check!
raise 'Oh oh!'
end
end
```
* Add its class to the configuration:

```ruby
HealthMonitor.configure do |config|
config.add_custom_provider(CustomProvider)
end
```

### Adding a custom error callback
If you need to perform any additional error handling (for example, for additional error reporting), you can configure a custom error callback:

Expand Down
24 changes: 19 additions & 5 deletions lib/health_monitor/configuration.rb
Expand Up @@ -9,14 +9,28 @@ def initialize
database
end

PROVIDERS.each do |provider|
define_method provider do |&block|
require "health_monitor/providers/#{provider}"
PROVIDERS.each do |provider_name|
define_method provider_name do |&block|
require "health_monitor/providers/#{provider_name}"

(@providers ||= Set.new) << provider
add_provider("HealthMonitor::Providers::#{provider_name.capitalize}".constantize)
end
end

"HealthMonitor::Providers::#{provider.capitalize}".constantize
def add_custom_provider(custom_provider_class)
unless custom_provider_class < HealthMonitor::Providers::Base
raise ArgumentError.new('custom provider class must implement HealthMonitor::Providers::Base')
end

add_provider(custom_provider_class)
end

private

def add_provider(provider_class)
(@providers ||= Set.new) << provider_class

provider_class
end
end
end
2 changes: 1 addition & 1 deletion lib/health_monitor/monitor.rb
Expand Up @@ -13,7 +13,7 @@ def configure

def check!(request: nil)
configuration.providers.each do |provider|
monitor = "HealthMonitor::Providers::#{provider.capitalize}".constantize.new(request: request)
monitor = provider.new(request: request)
monitor.check!
end
rescue Exception => e
Expand Down
2 changes: 1 addition & 1 deletion lib/health_monitor/version.rb
@@ -1,3 +1,3 @@
module HealthMonitor
VERSION = '2.0.2'.freeze
VERSION = '2.1.0'.freeze
end
52 changes: 42 additions & 10 deletions spec/lib/health_monitor/configuration_spec.rb
Expand Up @@ -2,31 +2,63 @@

describe HealthMonitor::Configuration do
describe 'defaults' do
it { expect(subject.providers).to eq(Set.new([:database])) }
it { expect(subject.providers).to eq(Set.new([HealthMonitor::Providers::Database])) }
it { expect(subject.error_callback).to be_nil }
it { expect(subject.basic_auth_credentials).to be_nil }
end

describe 'providers' do
HealthMonitor::Configuration::PROVIDERS.each do |provider|
HealthMonitor::Configuration::PROVIDERS.each do |provider_name|
before do
subject.instance_variable_set('@providers', Set.new)

stub_const("HealthMonitor::Providers::#{provider.capitalize}", Class.new)
stub_const("HealthMonitor::Providers::#{provider_name.capitalize}", Class.new)
end

it "responds to #{provider}" do
expect(subject).to respond_to(provider)
it "responds to #{provider_name}" do
expect(subject).to respond_to(provider_name)
end

it "configures #{provider}" do
it "configures #{provider_name}" do
expect {
subject.send(provider)
}.to change { subject.providers }.to(Set.new([provider]))
subject.send(provider_name)
}.to change { subject.providers }.to(Set.new(["HealthMonitor::Providers::#{provider_name.capitalize}".constantize]))
end

it "returns #{provider}'s class" do
expect(subject.send(provider)).to eq("HealthMonitor::Providers::#{provider.capitalize}".constantize)
it "returns #{provider_name}'s class" do
expect(subject.send(provider_name)).to eq("HealthMonitor::Providers::#{provider_name.capitalize}".constantize)
end
end
end

describe '#add_custom_provider' do
before do
subject.instance_variable_set('@providers', Set.new)
end

context 'inherits' do
class CustomProvider < HealthMonitor::Providers::Base
end

it 'accepts' do
expect {
subject.add_custom_provider(CustomProvider)
}.to change { subject.providers }.to(Set.new([CustomProvider]))
end

it "returns CustomProvider class" do
expect(subject.add_custom_provider(CustomProvider)).to eq(CustomProvider)
end
end

context 'does not inherit' do
class TestClass
end

it 'does not accept' do
expect {
subject.add_custom_provider(TestClass)
}.to raise_error(ArgumentError)
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions spec/lib/health_monitor/monitor_spec.rb
Expand Up @@ -14,7 +14,8 @@
subject.configure do |config|
config.redis
end
}.to change { HealthMonitor.configuration.providers }.to(Set.new([:database, :redis]))
}.to change { HealthMonitor.configuration.providers }.
to(Set.new([HealthMonitor::Providers::Database, HealthMonitor::Providers::Redis]))
end

it 'configures a multiple providers' do
Expand All @@ -23,15 +24,18 @@
config.redis
config.sidekiq
end
}.to change { HealthMonitor.configuration.providers }.to(Set.new([:database, :redis, :sidekiq]))
}.to change { HealthMonitor.configuration.providers }.
to(Set.new([HealthMonitor::Providers::Database, HealthMonitor::Providers::Redis,
HealthMonitor::Providers::Sidekiq]))
end

it 'appends new providers' do
expect {
subject.configure do |config|
config.resque
end
}.to change { HealthMonitor.configuration.providers }.to(Set.new([:database, :resque]))
}.to change { HealthMonitor.configuration.providers }.
to(Set.new([HealthMonitor::Providers::Database, HealthMonitor::Providers::Resque]))
end
end

Expand Down

0 comments on commit 0a19410

Please sign in to comment.