Sha256: 14b56799fc15fec28574e9dfe2e99f34a12bf7a1c06a40fa1e1d2986bf135379

Contents?: true

Size: 1.46 KB

Versions: 40

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'net/http'
module Orchestration
  class DockerHealthcheck
    def self.execute
      new.execute
    end

    def execute
      return_code = 1

      # rubocop:disable Lint/RescueException
      begin
        response = run
        return_code = 0 if success?(response.code)
        puts message(response.code)
      rescue Exception => e
        puts "[#{__FILE__}] ERROR: #{e.inspect}"
      ensure
        exit return_code
      end
      # rubocop:enable Lint/RescueException
    end

    private

    def run
      client = Net::HTTP.new(
        ENV.fetch('WEB_HOST', 'localhost'),
        ENV.fetch('WEB_PORT', '8080').to_i
      )

      client.read_timeout = ENV.fetch('WEB_HEALTHCHECK_READ_TIMEOUT', '10').to_i
      client.open_timeout = ENV.fetch('WEB_HEALTHCHECK_OPEN_TIMEOUT', '10').to_i

      client.start do |request|
        request.get(ENV.fetch('WEB_HEALTHCHECK_PATH', '/'))
      end
    end

    def success_codes
      ENV.fetch('WEB_HEALTHCHECK_SUCCESS_CODES', '200,201,202,204').split(',')
    end

    def success?(code)
      success_codes.include?(code.to_s)
    end

    def message(code)
      if success?(code)
        outcome = 'SUCCESS ✓ '
        in_or_not = 'IN'
      else
        outcome = 'FAILURE ✘ '
        in_or_not = 'NOT IN'
      end

      accepted = success_codes.join(', ')
      message = "#{in_or_not} [#{accepted}] : #{outcome} [#{__FILE__}]"

      "# HTTP_STATUS(#{code}) #{message}"
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
orchestration-0.7.15 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.16 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.14 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.13 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.15 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.12 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.14 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.11 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.13 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.10 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.9 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.12 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.8 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.11 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.6 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.10 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.5 lib/orchestration/docker_healthcheck.rb
orchestration-0.7.4 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.9 lib/orchestration/docker_healthcheck.rb
orchestration-0.6.8 lib/orchestration/docker_healthcheck.rb