Class: NatsWork::HealthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/natswork/health_check.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHealthChecker

Returns a new instance of HealthChecker.



78
79
80
81
82
83
84
85
86
# File 'lib/natswork/health_check.rb', line 78

def initialize
  @checks = Concurrent::Hash.new
  @thresholds = {
    memory_mb: 1000,
    cpu_percent: 90.0,
    queue_depth: 1000,
    error_rate: 0.05
  }
end

Class Method Details

.globalObject



152
153
154
# File 'lib/natswork/health_check.rb', line 152

def global
  @global ||= new.tap { |checker| setup_default_checks(checker) }
end

Instance Method Details

#add_check(name, &block) ⇒ Object



88
89
90
# File 'lib/natswork/health_check.rb', line 88

def add_check(name, &block)
  @checks[name.to_s] = HealthCheck.new(name, &block)
end

#check(name) ⇒ Object



106
107
108
109
# File 'lib/natswork/health_check.rb', line 106

def check(name)
  check = @checks[name.to_s]
  check&.check!
end

#check_all!Object



96
97
98
99
100
101
102
103
104
# File 'lib/natswork/health_check.rb', line 96

def check_all!
  results = {}

  @checks.each do |name, check|
    results[name] = check.check!
  end

  results
end

#healthy?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/natswork/health_check.rb', line 119

def healthy?(name = nil)
  if name
    @checks[name.to_s]&.healthy? || false
  else
    @checks.values.all?(&:healthy?)
  end
end

#remove_check(name) ⇒ Object



92
93
94
# File 'lib/natswork/health_check.rb', line 92

def remove_check(name)
  @checks.delete(name.to_s)
end

#reportObject



127
128
129
130
131
132
133
# File 'lib/natswork/health_check.rb', line 127

def report
  {
    status: overall_status,
    timestamp: Time.now.iso8601,
    checks: @checks.transform_values(&:to_h)
  }
end

#set_threshold(metric, value) ⇒ Object



135
136
137
# File 'lib/natswork/health_check.rb', line 135

def set_threshold(metric, value)
  @thresholds[metric.to_sym] = value
end

#status(name = nil) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/natswork/health_check.rb', line 111

def status(name = nil)
  if name
    @checks[name.to_s]&.status || :unknown
  else
    overall_status
  end
end