health_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@health_rails --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in health_rails.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ class HealthRails::HealthController < ApplicationController
2
+ # GET /health
3
+ def index
4
+ health_check = HealthRails::HealthCheck.new
5
+ if health_check.all_ok?
6
+ render text: "All OK"
7
+ else
8
+ render text: health_check.error_messages, status: :service_unavailable
9
+ end
10
+ end
11
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ match "/health" => "health_rails/health#index"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "health_rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "health_rails"
7
+ s.version = HealthRails::VERSION
8
+ s.authors = ["Daniel Spangenberg"]
9
+ s.email = ["daniel.spangenberg@parcydo.com"]
10
+ s.homepage = "http://github.com/parcydo/health_rails"
11
+ s.summary = %q{Health rails provides your rails app a simple password protected health status page for other web services.}
12
+ s.description = %q{Health rails gives you some default checks and a DSL for defining your own checks.}
13
+
14
+ s.add_development_dependency "rails"
15
+ s.add_development_dependency "rspec"
16
+
17
+ s.rubyforge_project = "health_rails"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,5 @@
1
+ require 'health_rails/engine'
2
+
3
+ module HealthRails
4
+ autoload :HealthCheck, 'health_rails/health_check'
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'health_rails'
2
+ require 'rails'
3
+
4
+ module HealthRails
5
+ class Engine < Rails::Engine
6
+ end
7
+ end
@@ -0,0 +1,51 @@
1
+ module HealthRails
2
+ class HealthCheck
3
+ class HealthCheckFailure < Exception
4
+ end
5
+
6
+ def all_ok?
7
+ process_checks
8
+ !errors.any?
9
+ end
10
+
11
+ def error_messages
12
+ errors.join("\n")
13
+ end
14
+
15
+ def errors
16
+ @errors ||= []
17
+ end
18
+
19
+ def process_check(description)
20
+ begin
21
+ HealthCheck.check(description).call
22
+ rescue HealthCheckFailure => error_message
23
+ errors << "#{description}: #{error_message}"
24
+ end
25
+ end
26
+
27
+ def process_checks
28
+ HealthCheck.checks.each do |description, proc|
29
+ process_check(description)
30
+ end
31
+ end
32
+
33
+ class << self
34
+
35
+ def check(description, &block)
36
+ if block.nil?
37
+ checks[description]
38
+ else
39
+ checks[description] = block
40
+ end
41
+ end
42
+
43
+ def checks
44
+ @@checks ||= {}
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ # Load all health checks
51
+ Dir["#{File.dirname(__FILE__)}/health_checks/**/*.rb"].each { |f| require f }
@@ -0,0 +1,12 @@
1
+ require 'active_record'
2
+
3
+ module HealthRails
4
+ class HealthCheck
5
+ check "ActiveRecord connection" do
6
+ active_record_connection = ActiveRecord::Base.connection
7
+ unless active_record_connection.active?
8
+ raise HealthCheckFailure, "#{active_record_connection.adapter_name} connection is gone from us!"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module HealthRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ module HealthRails
4
+ describe HealthCheck do
5
+ let(:health_check) { HealthRails::HealthCheck.new }
6
+
7
+ before(:each) do
8
+ HealthRails::HealthCheck.stub(:checks).and_return({})
9
+ end
10
+
11
+ describe "run all checks and return status" do
12
+ it "fails" do
13
+ HealthRails::HealthCheck.check("Failing") do
14
+ raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
15
+ end
16
+ HealthRails::HealthCheck.check("Passing") do
17
+ nil
18
+ end
19
+ health_check.all_ok?.should be(false)
20
+ end
21
+
22
+ it "passes" do
23
+ HealthRails::HealthCheck.check("Passing 1") do
24
+ nil
25
+ end
26
+ HealthRails::HealthCheck.check("Passing 2") do
27
+ nil
28
+ end
29
+ health_check.all_ok?.should be(true)
30
+ end
31
+ end
32
+
33
+ it "push check in checks hash" do
34
+ block = lambda {}
35
+ HealthRails::HealthCheck.check("Foo Bar", &block)
36
+ HealthRails::HealthCheck.checks.size.should be(1)
37
+ HealthRails::HealthCheck.check("Foo Bar").should be(block)
38
+ end
39
+
40
+ it "join error messages" do
41
+ health_check.stub(:errors).and_return(["Foo", "Bar", "Baz"])
42
+ health_check.error_messages.should eql("Foo\nBar\nBaz")
43
+ end
44
+
45
+ describe "process check" do
46
+ it "pushes error on exception" do
47
+ HealthRails::HealthCheck.check("Foo Bar") do
48
+ raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
49
+ end
50
+ health_check.process_check("Foo Bar")
51
+ health_check.errors.size.should be(1)
52
+ health_check.errors.first.should eql("Foo Bar: Exception")
53
+ end
54
+ end
55
+
56
+ describe "process checks" do
57
+ it "run process check on each" do
58
+ HealthRails::HealthCheck.check("Foo") do
59
+ raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
60
+ end
61
+ HealthRails::HealthCheck.check("Bar") do
62
+ raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
63
+ end
64
+ HealthRails::HealthCheck.check("Baz") do
65
+ nil
66
+ end
67
+ health_check.process_checks
68
+ health_check.errors.size.should be(2)
69
+ health_check.errors.first.should eql("Foo: Exception")
70
+ health_check.errors.last.should eql("Bar: Exception")
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ describe "active record health check" do
5
+ let(:health_check) { HealthRails::HealthCheck.new }
6
+
7
+ it "raise exception if connection is not active" do
8
+ foo_bar_adapter = mock("ActiveRecord::ConnectionAdapters::FooBarAdapter", active?: false, adapter_name: "FooBar")
9
+ ActiveRecord::Base.stub(:connection).and_return(foo_bar_adapter)
10
+ health_check.process_check("ActiveRecord connection")
11
+ health_check.errors.size.should be(1)
12
+ health_check.errors.first.should eql("ActiveRecord connection: FooBar connection is gone from us!")
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require 'health_rails'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: health_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Spangenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-27 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &2164618080 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2164618080
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &2164617500 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2164617500
37
+ description: Health rails gives you some default checks and a DSL for defining your
38
+ own checks.
39
+ email:
40
+ - daniel.spangenberg@parcydo.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - .rspec
47
+ - .rvmrc
48
+ - Gemfile
49
+ - Rakefile
50
+ - app/controllers/health_rails/health_controller.rb
51
+ - config/routes.rb
52
+ - health_rails.gemspec
53
+ - lib/health_rails.rb
54
+ - lib/health_rails/engine.rb
55
+ - lib/health_rails/health_check.rb
56
+ - lib/health_rails/health_checks/active_record_health_check.rb
57
+ - lib/health_rails/version.rb
58
+ - spec/health_check_spec.rb
59
+ - spec/health_checks/active_record_health_check_spec.rb
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/parcydo/health_rails
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: -1905505185732161742
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -1905505185732161742
86
+ requirements: []
87
+ rubyforge_project: health_rails
88
+ rubygems_version: 1.6.2
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Health rails provides your rails app a simple password protected health status
92
+ page for other web services.
93
+ test_files:
94
+ - spec/health_check_spec.rb
95
+ - spec/health_checks/active_record_health_check_spec.rb
96
+ - spec/spec_helper.rb