health_rails 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ rvm: 1.9.2
@@ -0,0 +1 @@
1
+ h1. HealthRails "!https://secure.travis-ci.org/parcydo/health_rails.png?branch=master!":http://travis-ci.org/parcydo/health_rails
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require 'bundler/gem_tasks'
3
3
  require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,4 +1,6 @@
1
1
  class HealthRails::HealthController < ApplicationController
2
+ before_filter :login_required
3
+
2
4
  # GET /health
3
5
  def index
4
6
  health_check = HealthRails::HealthCheck.new
@@ -8,4 +10,13 @@ class HealthRails::HealthController < ApplicationController
8
10
  render text: health_check.error_messages, status: :service_unavailable
9
11
  end
10
12
  end
13
+
14
+ private
15
+ def login_required
16
+ if HealthRails.authentication
17
+ authenticate_or_request_with_http_basic("#{Rails.application.class.parent_name}'s health is important to us!") do |username, password|
18
+ HealthRails.authentication.has_key?(username) && HealthRails.authentication[username] == password
19
+ end
20
+ end
21
+ end
11
22
  end
@@ -0,0 +1,15 @@
1
+ require 'securerandom'
2
+
3
+ module HealthRails
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a HealthRails initializer."
9
+
10
+ def copy_initializer
11
+ template "health_rails.rb", "config/initializers/health_rails.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # Use this hook to configure health rails.
2
+ HealthRails.setup do |config|
3
+ # Configure who can access your health check url.
4
+ # You can configure as much users as you want,
5
+ # or pass another backend for authentification.
6
+ # If you don't want to have authentification,
7
+ # remove the next line. This is not recommended!
8
+ config.authentication = { "admin" => <%= SecureRandom.hex(16).inspect %> }
9
+
10
+ # Configure which health checks should be processed.
11
+ config.health_checks = [ 'ActiveRecord connection' ]
12
+
13
+ # Custom checks
14
+ #
15
+ # check "Fail" do
16
+ # raise HealthCheckFailure, "It failed!"
17
+ # end
18
+ #
19
+ # check "Pass" do
20
+ # if false
21
+ # raise HealthCheckFailure, "It failed, OMG!"
22
+ # end
23
+ # end
24
+ end
@@ -1,5 +1,19 @@
1
- require 'health_rails/engine'
1
+ require 'health_rails/dsl'
2
+ require 'health_rails/health_check'
3
+ require 'health_rails/rails'
2
4
 
3
5
  module HealthRails
4
- autoload :HealthCheck, 'health_rails/health_check'
6
+ # Users which have access to the health check url.
7
+ mattr_accessor :authentication
8
+ @@authentication = false
9
+
10
+ # Health checks which will be processed.
11
+ mattr_accessor :health_checks
12
+ @@health_checks = []
13
+
14
+ # Default way to setup HealthRails. Run rails generate health_rails:install to create
15
+ # a fresh initializer with all configuration values.
16
+ def self.setup
17
+ yield self
18
+ end
5
19
  end
@@ -0,0 +1,12 @@
1
+ module HealthRails
2
+ module DSL
3
+ class HealthCheckFailure < Exception
4
+ end
5
+
6
+ def check(description, auto_activated=true, &health_check_block)
7
+ HealthRails::HealthCheck.check(description, auto_activated, &health_check_block)
8
+ end
9
+ end
10
+ end
11
+
12
+ include HealthRails::DSL
@@ -1,13 +1,18 @@
1
1
  module HealthRails
2
2
  class HealthCheck
3
- class HealthCheckFailure < Exception
4
- end
5
-
6
3
  def all_ok?
7
4
  process_checks
8
5
  !errors.any?
9
6
  end
10
7
 
8
+ def check(description)
9
+ checks[description]
10
+ end
11
+
12
+ def checks
13
+ HealthCheck.checks
14
+ end
15
+
11
16
  def error_messages
12
17
  errors.join("\n")
13
18
  end
@@ -18,26 +23,26 @@ module HealthRails
18
23
 
19
24
  def process_check(description)
20
25
  begin
21
- HealthCheck.check(description).call
26
+ check(description).call
22
27
  rescue HealthCheckFailure => error_message
23
28
  errors << "#{description}: #{error_message}"
24
29
  end
25
30
  end
26
31
 
27
32
  def process_checks
28
- HealthCheck.checks.each do |description, proc|
33
+ checks.each do |description, proc|
34
+ next unless HealthRails.health_checks.include?(description)
29
35
  process_check(description)
30
36
  end
31
37
  end
32
38
 
33
39
  class << self
34
40
 
35
- def check(description, &block)
36
- if block.nil?
37
- checks[description]
38
- else
39
- checks[description] = block
41
+ def check(description, auto_activated, &block)
42
+ if auto_activated && !HealthRails.health_checks.include?(description)
43
+ HealthRails.health_checks << description
40
44
  end
45
+ checks[description] = block
41
46
  end
42
47
 
43
48
  def checks
@@ -1,12 +1,8 @@
1
1
  require 'active_record'
2
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
3
+ check "ActiveRecord connection", false do
4
+ active_record_connection = ActiveRecord::Base.connection
5
+ unless active_record_connection.active?
6
+ raise HealthCheckFailure, "#{active_record_connection.adapter_name} connection is gone from us!"
11
7
  end
12
8
  end
@@ -1,4 +1,3 @@
1
- require 'health_rails'
2
1
  require 'rails'
3
2
 
4
3
  module HealthRails
@@ -1,3 +1,3 @@
1
1
  module HealthRails
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -6,35 +6,51 @@ module HealthRails
6
6
 
7
7
  before(:each) do
8
8
  HealthRails::HealthCheck.stub(:checks).and_return({})
9
+ HealthRails.stub(:health_checks).and_return([])
9
10
  end
10
11
 
11
12
  describe "run all checks and return status" do
12
13
  it "fails" do
13
- HealthRails::HealthCheck.check("Failing") do
14
- raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
14
+ check "Failing" do
15
+ raise HealthCheckFailure, "Exception"
15
16
  end
16
- HealthRails::HealthCheck.check("Passing") do
17
+ check "Passing" do
17
18
  nil
18
19
  end
19
20
  health_check.all_ok?.should be(false)
20
21
  end
21
22
 
22
23
  it "passes" do
23
- HealthRails::HealthCheck.check("Passing 1") do
24
+ check "Passing 1" do
24
25
  nil
25
26
  end
26
- HealthRails::HealthCheck.check("Passing 2") do
27
+ check "Passing 2" do
27
28
  nil
28
29
  end
29
30
  health_check.all_ok?.should be(true)
30
31
  end
31
32
  end
32
33
 
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)
34
+ describe "check" do
35
+ it "push check in checks hash" do
36
+ block = lambda {}
37
+ check("Foo Bar", &block)
38
+ health_check.checks.size.should be(1)
39
+ health_check.check("Foo Bar").should be(block)
40
+ end
41
+
42
+ it "push check in health_checks config if auto activated" do
43
+ check "Foo" do
44
+ nil
45
+ end
46
+ check "Bar", false do
47
+ nil
48
+ end
49
+ check "Baz" do
50
+ nil
51
+ end
52
+ HealthRails.health_checks.should eql(["Foo", "Baz"])
53
+ end
38
54
  end
39
55
 
40
56
  it "join error messages" do
@@ -44,8 +60,8 @@ module HealthRails
44
60
 
45
61
  describe "process check" do
46
62
  it "pushes error on exception" do
47
- HealthRails::HealthCheck.check("Foo Bar") do
48
- raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
63
+ check "Foo Bar" do
64
+ raise HealthCheckFailure, "Exception"
49
65
  end
50
66
  health_check.process_check("Foo Bar")
51
67
  health_check.errors.size.should be(1)
@@ -54,14 +70,14 @@ module HealthRails
54
70
  end
55
71
 
56
72
  describe "process checks" do
57
- it "run process check on each" do
58
- HealthRails::HealthCheck.check("Foo") do
59
- raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
73
+ it "run process check on each check" do
74
+ check "Foo" do
75
+ raise HealthCheckFailure, "Exception"
60
76
  end
61
- HealthRails::HealthCheck.check("Bar") do
62
- raise HealthRails::HealthCheck::HealthCheckFailure, "Exception"
77
+ check "Bar" do
78
+ raise HealthCheckFailure, "Exception"
63
79
  end
64
- HealthRails::HealthCheck.check("Baz") do
80
+ check "Baz" do
65
81
  nil
66
82
  end
67
83
  health_check.process_checks
@@ -69,6 +85,21 @@ module HealthRails
69
85
  health_check.errors.first.should eql("Foo: Exception")
70
86
  health_check.errors.last.should eql("Bar: Exception")
71
87
  end
88
+
89
+ it "run only checks which are in the health_checks config" do
90
+ check "Foo", false do
91
+ raise HealthCheckFailure, "Exception"
92
+ end
93
+ check "Bar" do
94
+ raise HealthCheckFailure, "Exception"
95
+ end
96
+ check "Baz" do
97
+ nil
98
+ end
99
+ health_check.process_checks
100
+ health_check.errors.size.should be(1)
101
+ health_check.errors.last.should eql("Bar: Exception")
102
+ end
72
103
  end
73
104
 
74
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: health_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-27 00:00:00.000000000 +02:00
12
+ date: 2011-07-28 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
- requirement: &2164618080 !ruby/object:Gem::Requirement
17
+ requirement: &2169069860 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2164618080
25
+ version_requirements: *2169069860
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &2164617500 !ruby/object:Gem::Requirement
28
+ requirement: &2169069200 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2164617500
36
+ version_requirements: *2169069200
37
37
  description: Health rails gives you some default checks and a DSL for defining your
38
38
  own checks.
39
39
  email:
@@ -45,15 +45,20 @@ files:
45
45
  - .gitignore
46
46
  - .rspec
47
47
  - .rvmrc
48
+ - .travis.yml
48
49
  - Gemfile
50
+ - README.textile
49
51
  - Rakefile
50
52
  - app/controllers/health_rails/health_controller.rb
51
53
  - config/routes.rb
52
54
  - health_rails.gemspec
55
+ - lib/generators/health_rails/install_generator.rb
56
+ - lib/generators/templates/health_rails.rb
53
57
  - lib/health_rails.rb
54
- - lib/health_rails/engine.rb
58
+ - lib/health_rails/dsl.rb
55
59
  - lib/health_rails/health_check.rb
56
60
  - lib/health_rails/health_checks/active_record_health_check.rb
61
+ - lib/health_rails/rails.rb
57
62
  - lib/health_rails/version.rb
58
63
  - spec/health_check_spec.rb
59
64
  - spec/health_checks/active_record_health_check_spec.rb
@@ -73,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
78
  version: '0'
74
79
  segments:
75
80
  - 0
76
- hash: -1905505185732161742
81
+ hash: -2292776394260562768
77
82
  required_rubygems_version: !ruby/object:Gem::Requirement
78
83
  none: false
79
84
  requirements:
@@ -82,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
87
  version: '0'
83
88
  segments:
84
89
  - 0
85
- hash: -1905505185732161742
90
+ hash: -2292776394260562768
86
91
  requirements: []
87
92
  rubyforge_project: health_rails
88
93
  rubygems_version: 1.6.2