Skip to content

Commit

Permalink
Merge branch 'named_custom_checks'
Browse files Browse the repository at this point in the history
  • Loading branch information
ianheggie committed Dec 4, 2016
2 parents 0707a4a + 36c2683 commit 7a4488d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ To change the configuration of health_check, create a file `config/initializers/
CustomHealthCheck.perform_check # any code that returns blank on success and non blank string upon failure
end

# Add another custom check with a name, so you can call just specific custom checks. This can also be run using
# the standard 'custom' check.
# You can define multiple tests under the same name - they will be run one after the other.
config.add_custom_check('sometest') do
CustomHealthCheck.perform_another_check # any code that returns blank on success and non blank string upon failure
end

# max-age of response in seconds
# cache-control is public when max_age > 1 and basic_auth_username is not set
# You can force private without authentication for longer max_age by
Expand Down
7 changes: 4 additions & 3 deletions lib/health_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Engine < Rails::Engine
mattr_accessor :custom_checks
mattr_accessor :full_checks
mattr_accessor :standard_checks
self.custom_checks = [ ]
self.custom_checks = { }
self.full_checks = ['database', 'migrations', 'custom', 'email', 'cache', 'redis-if-present', 'sidekiq-redis-if-present', 'resque-redis-if-present', 's3-if-present']
self.standard_checks = [ 'database', 'migrations', 'custom', 'emailconf' ]

Expand All @@ -63,8 +63,9 @@ class Engine < Rails::Engine

mattr_accessor :installed_as_middleware

def self.add_custom_check(&block)
custom_checks << block
def self.add_custom_check(name = 'custom', &block)
custom_checks[name] ||= [ ]
custom_checks[name] << block
end

def self.setup
Expand Down
14 changes: 11 additions & 3 deletions lib/health_check/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ def self.process_checks(checks, called_from_middleware = false)
when "middleware"
errors << "Health check not called from middleware - probably not installed as middleware." unless called_from_middleware
when "custom"
HealthCheck.custom_checks.each do |custom_check|
errors << custom_check.call(self)
HealthCheck.custom_checks.each do |name, list|
list.each do |custom_check|
errors << custom_check.call(self)
end
end
when "all", "full"
errors << HealthCheck::Utils.process_checks(HealthCheck.full_checks, called_from_middleware)
else
return "invalid argument to health_test."
if HealthCheck.custom_checks.include? check
HealthCheck.custom_checks[check].each do |custom_check|
errors << custom_check.call(self)
end
else
return "invalid argument to health_test."
end
end
end
return errors
Expand Down
5 changes: 5 additions & 0 deletions test/setup_railsapp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ HealthCheck.setup do |config|
config.add_custom_check do
File.exists?("$custom_file") ? '' : '$custom_file is missing!'
end
config.add_custom_check('pass') do
''
end
end
!

Expand Down
13 changes: 13 additions & 0 deletions test/test_with_railsapp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,13 @@ common_tests()
echo
fi

test_no=`expr 1 + $test_no`
if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then
echo "${test_no}: TESTING ${route_prefix}/pass should pass ..."
$testurl ${host}/${route_prefix}/pass 200 text/plain $success
echo
fi

test_no=`expr 1 + $test_no`
if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then
echo "${test_no}: TESTING ${route_prefix}/custom should pass ..."
Expand Down Expand Up @@ -440,6 +447,12 @@ common_tests()
echo
fi

if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then
echo "${test_no}: TESTING ${route_prefix}/pass should pass even if other custom test returns string ..."
$testurl ${host}/${route_prefix}/pass 200 text/plain $success
echo
fi

test_no=`expr 1 + $test_no`
if [ -z "$run_test" ] || [ $test_no == "$run_test" ]; then
echo "${test_no}: TESTING ${route_prefix} (all) should fail when custom check fails ..."
Expand Down

0 comments on commit 7a4488d

Please sign in to comment.