Class: Harbor::HealthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/health_checker.rb

Constant Summary collapse

DEFAULT_CHECKS =
3
DEFAULT_INTERVAL =

seconds between checks

10
DEFAULT_PATH =
"/up"
DEFAULT_TIMEOUT =
5

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HealthChecker

Returns a new instance of HealthChecker.



13
14
15
16
17
18
19
# File 'lib/harbor/health_checker.rb', line 13

def initialize(config)
  @health = config.setting("health_check", {})
  @checks = @health.fetch("checks", DEFAULT_CHECKS)
  @interval = @health.fetch("interval", DEFAULT_INTERVAL)
  @path = @health.fetch("path", DEFAULT_PATH)
  @timeout = @health.fetch("timeout", DEFAULT_TIMEOUT)
end

Instance Method Details

#verify(domain:, checks: nil, interval: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/harbor/health_checker.rb', line 21

def verify(domain:, checks: nil, interval: nil)
  num_checks = checks || @checks
  wait = interval || @interval

  results = []
  num_checks.times do |i|
    sleep(wait) if i > 0
    result = check_once(domain)
    results << result
    # Fail fast if first check fails
    break unless result[:healthy]
  end

  all_healthy = results.all? { |r| r[:healthy] }
  {
    healthy: all_healthy,
    checks: results,
    domain: domain,
    path: @path
  }
end