Sha256: 4ea4bca5e4329c6fd3cf1eaf8f3a38be906044066014295ff104e7e4c107a0be

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

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(healthy=#{healthy?},message=#{message.inspect},exception=#{exception.inspect})"
    end
  end

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

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

  def self.callback(cb = nil, &block)
    @@callbacks << (cb || block)
  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
      sleep(interval)
      trigger
    end
  end

  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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liquid-ext-3.5.0 lib/liquid/health_checks.rb