Sha256: 443790417457f5d6be864220d3c7808210b7621cfadd0ceb2ed7336ad409056d

Contents?: true

Size: 955 Bytes

Versions: 10

Compression:

Stored size: 955 Bytes

Contents

require 'liquid/logger'

class HealthCheck
  class Result
    attr_reader :message
    attr_reader :exception

    def initialize(healthy, message, exception)
      @healthy = healthy
      @message = message
      @exception = exception
    end

    def healthy?
      @healthy
    end

    def to_s
      "Result{isHealthy=#{healthy?},message=#{message.inspect},exception=#{exception.inspect}}"
    end
  end

  @@checks = {}

  def self.inherited(child)
    @@checks[child.name.demodulize] = child
  end

  def self.run
    @@checks.inject({}) do |result, (name, handler)|
      result[name] = handler.new.execute
      result
    end
  end

  def self.poll(interval = 5)
    loop do
      @healthy = run.values.all?(&:healthy?)
      sleep(interval)
    end
  end

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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
liquid-ext-3.3.10 lib/liquid/health_checks.rb
liquid-ext-3.3.9 lib/liquid/health_checks.rb
liquid-ext-3.3.8 lib/liquid/health_checks.rb
liquid-ext-3.3.7 lib/liquid/health_checks.rb
liquid-ext-3.3.6 lib/liquid/health_checks.rb
liquid-ext-3.3.5 lib/liquid/health_checks.rb
liquid-ext-3.3.4 lib/liquid/health_checks.rb
liquid-ext-3.3.3 lib/liquid/health_checks.rb
liquid-ext-3.3.2 lib/liquid/health_checks.rb
liquid-ext-3.3.0 lib/liquid/health_checks.rb