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.
- data/.travis.yml +1 -0
- data/README.textile +1 -0
- data/Rakefile +1 -1
- data/app/controllers/health_rails/health_controller.rb +11 -0
- data/lib/generators/health_rails/install_generator.rb +15 -0
- data/lib/generators/templates/health_rails.rb +24 -0
- data/lib/health_rails.rb +16 -2
- data/lib/health_rails/dsl.rb +12 -0
- data/lib/health_rails/health_check.rb +15 -10
- data/lib/health_rails/health_checks/active_record_health_check.rb +4 -8
- data/lib/health_rails/{engine.rb → rails.rb} +0 -1
- data/lib/health_rails/version.rb +1 -1
- data/spec/health_check_spec.rb +49 -18
- metadata +14 -9
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm: 1.9.2
|
data/README.textile
ADDED
@@ -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
@@ -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
|
data/lib/health_rails.rb
CHANGED
@@ -1,5 +1,19 @@
|
|
1
|
-
require 'health_rails/
|
1
|
+
require 'health_rails/dsl'
|
2
|
+
require 'health_rails/health_check'
|
3
|
+
require 'health_rails/rails'
|
2
4
|
|
3
5
|
module HealthRails
|
4
|
-
|
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
|
-
|
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
|
-
|
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
|
37
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
data/lib/health_rails/version.rb
CHANGED
data/spec/health_check_spec.rb
CHANGED
@@ -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
|
-
|
14
|
-
raise
|
14
|
+
check "Failing" do
|
15
|
+
raise HealthCheckFailure, "Exception"
|
15
16
|
end
|
16
|
-
|
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
|
-
|
24
|
+
check "Passing 1" do
|
24
25
|
nil
|
25
26
|
end
|
26
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
48
|
-
raise
|
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
|
-
|
59
|
-
raise
|
73
|
+
it "run process check on each check" do
|
74
|
+
check "Foo" do
|
75
|
+
raise HealthCheckFailure, "Exception"
|
60
76
|
end
|
61
|
-
|
62
|
-
raise
|
77
|
+
check "Bar" do
|
78
|
+
raise HealthCheckFailure, "Exception"
|
63
79
|
end
|
64
|
-
|
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.
|
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-
|
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: &
|
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: *
|
25
|
+
version_requirements: *2169069860
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rspec
|
28
|
-
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: *
|
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/
|
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: -
|
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: -
|
90
|
+
hash: -2292776394260562768
|
86
91
|
requirements: []
|
87
92
|
rubyforge_project: health_rails
|
88
93
|
rubygems_version: 1.6.2
|