Sha256: 36c003255962e0a15ec114fd818b82d51828acfe7b0bb6e9c663a35ac614c6e0

Contents?: true

Size: 1.43 KB

Versions: 21

Compression:

Stored size: 1.43 KB

Contents

module Minke
  module Docker
    ##
    # HealthCheck checks health of a running container
    class HealthCheck

      def initialize logger, count=nil, pause=nil
        @count = count ||= 180
        @pause = pause ||= 1
        @successes = 2
        @logger = logger
      end

      ##
      # waits until a 200 response is received from the given url
      def wait_for_HTTPOK url
        @logger.debug "Waiting for server #{url} to start #{@count} attempts remaining"

        begin
          response = RestClient.send('get', url)
        rescue
          @logger.debug 'Invalid response from server'
        end

        check_response response, url
      end

      private
      def check_response response, url
        if response == nil || !response.code.to_i == 200
          check_failed url
        else
          check_success url
        end
      end

      def check_failed url
        @count -= 1
        sleep @pause

        if @count > 0
          wait_for_HTTPOK url
        else
          @logger.error "Server: #{url} failed health check"
          raise 'Server failed to start'
        end
      end

      def check_success url
        if @successes > 0 
          @logger.debug "Server: #{url} passed health check, #{@successes} checks to go..."

          @successes -= 1
          sleep @pause
          wait_for_HTTPOK url
        else
          @logger.debug "Server: #{url} healthy"
        end
      end

    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
minke-1.13.6 lib/minke/docker/health_check.rb