Class: HealthCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/health_checks.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

@@checks =
{}
@@callbacks =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callback(cb = nil, &block) ⇒ Object



34
35
36
# File 'lib/liquid/health_checks.rb', line 34

def self.callback(cb = nil, &block)
  @@callbacks << (cb || block)
end

.healthy?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/liquid/health_checks.rb', line 73

def self.healthy?
  run.values.all?(&:healthy?)
end

.poll(interval = 5) ⇒ Object



55
56
57
58
59
60
# File 'lib/liquid/health_checks.rb', line 55

def self.poll(interval = 5)
  loop do
    sleep(interval)
    trigger
  end
end

.register(name, &block) ⇒ Object



26
27
28
# File 'lib/liquid/health_checks.rb', line 26

def self.register(name, &block)
  @@checks[name.to_s] = block
end

.runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/liquid/health_checks.rb', line 38

def self.run
  @@checks.inject({}) do |results, (name, handler)|
    if handler.is_a? Proc
      result = handler.call
    else
      result = handler.new.execute
    end

    unless result.is_a? Result
      result = Result.new(result , nil, nil)
    end

    results[name] = result
    results
  end
end

.triggerObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/liquid/health_checks.rb', line 62

def self.trigger
  results = run
  @@callbacks.each do |cb|
    begin
      cb.call(results)
    rescue => e
      $log.exception(e, "failed to run health check callback")
    end
  end
end

Instance Method Details

#executeObject



77
78
79
80
81
# File 'lib/liquid/health_checks.rb', line 77

def execute
  check
rescue => e
  Result.new(false, "failed to execute check", e)
end