从health_check
官方网站上,我们知道它可以在配置文件中添加一个config.add_custom_check
块:
https://github.com/ianheggie/health_check
# Add one or more custom checks that return a blank string if ok, or an error message if there is an error
config.add_custom_check do
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
但是关于CustomHealthCheck
类,如何定义它?
对于okcomputer
宝石,它提供了一种这样的方式:
https://github.com/sportngin/okcomputer
# config/initializers/okcomputer.rb
class MyCustomCheck < OkComputer::Check
def check
if rand(10).even?
mark_message "Even is great!"
else
mark_failure
mark_message "We don't like odd numbers"
end
end
end
OkComputer::Registry.register "check_for_odds", MyCustomCheck.new
没有找到关于health_check
宝石的用法。
更新
我试过:
在config/initializers/health_check.rb
文件中添加以下源:
class CustomHealthCheck
def perform_check
if rand(10).even?
p "Even is great!"
else
p "We don't like odd numbers"
end
end
end
HealthCheck.setup do |config|
...
运行curl -v localhost:3000/health_check.json
,得到:
{"healthy":false,"message":"health_check failed: undefined method `perform_check' for CustomHealthCheck:Class"}%
更新 2
编辑来源config/initializers/health_check.rb
:
class CustomHealthCheck
def self.perform_check
p 'OK'
end
end
HealthCheck.setup do |config|
...
有:
{"healthy":false,"message":"health_check failed: OK"}%
成功是通过返回空或空字符串来定义的。现在,您的perform_check
始终返回字符串"OK",这将被视为失败。
尝试此操作以获得通过的运行状况检查:
class CustomHealthCheck
def self.perform_check
everything_is_good = true # or call some method to do more elaborate checking
return everything_is_good ? "" : "We've got Problems"
end
end