Class: NatsWork::HealthCheck
- Inherits:
-
Object
- Object
- NatsWork::HealthCheck
- Defined in:
- lib/natswork/health_check.rb
Constant Summary collapse
- STATUSES =
i[healthy degraded unhealthy unknown].freeze
Instance Attribute Summary collapse
-
#last_checked ⇒ Object
readonly
Returns the value of attribute last_checked.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #check! ⇒ Object
- #degraded? ⇒ Boolean
- #healthy? ⇒ Boolean
-
#initialize(name, &check_block) ⇒ HealthCheck
constructor
A new instance of HealthCheck.
- #to_h ⇒ Object
- #unhealthy? ⇒ Boolean
Constructor Details
#initialize(name, &check_block) ⇒ HealthCheck
Returns a new instance of HealthCheck.
11 12 13 14 15 16 17 18 19 |
# File 'lib/natswork/health_check.rb', line 11 def initialize(name, &check_block) @name = name.to_s @check_block = check_block @status = :unknown = '' = {} @last_checked = nil @mutex = Mutex.new end |
Instance Attribute Details
#last_checked ⇒ Object (readonly)
Returns the value of attribute last_checked.
7 8 9 |
# File 'lib/natswork/health_check.rb', line 7 def last_checked @last_checked end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/natswork/health_check.rb', line 7 def end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/natswork/health_check.rb', line 7 def end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/natswork/health_check.rb', line 7 def name @name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/natswork/health_check.rb', line 7 def status @status end |
Instance Method Details
#check! ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/natswork/health_check.rb', line 21 def check! @mutex.synchronize do @last_checked = Time.now begin if @check_block result = @check_block.call if result.is_a?(Hash) @status = result.fetch(:status, :healthy) = result.fetch(:message, '') = result.fetch(:metadata, {}) elsif result @status = :healthy = 'Check passed' else @status = :unhealthy = 'Check failed' end else @status = :unknown = 'No check defined' end rescue StandardError => e @status = :unhealthy = "Check error: #{e.message}" = { error: e.class.name, backtrace: e.backtrace&.first(5) } end @status end end |
#degraded? ⇒ Boolean
62 63 64 |
# File 'lib/natswork/health_check.rb', line 62 def degraded? @status == :degraded end |
#healthy? ⇒ Boolean
54 55 56 |
# File 'lib/natswork/health_check.rb', line 54 def healthy? @status == :healthy end |
#to_h ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/natswork/health_check.rb', line 66 def to_h { name: @name, status: @status, message: , last_checked: @last_checked, metadata: } end |
#unhealthy? ⇒ Boolean
58 59 60 |
# File 'lib/natswork/health_check.rb', line 58 def unhealthy? @status == :unhealthy end |