Sha256: 395e522b0a38b83f4aa10924d102b187b625770b95e4456fb3a746555582b94b

Contents?: true

Size: 1.58 KB

Versions: 12

Compression:

Stored size: 1.58 KB

Contents

module Kamal::Cli::Healthcheck::Poller
  extend self

  TRAEFIK_UPDATE_DELAY = 5


  def wait_for_healthy(pause_after_ready: false, &block)
    attempt = 1
    max_attempts = KAMAL.config.healthcheck.max_attempts

    begin
      case status = block.call
      when "healthy"
        sleep TRAEFIK_UPDATE_DELAY if pause_after_ready
      when "running" # No health check configured
        sleep KAMAL.config.readiness_delay if pause_after_ready
      else
        raise Kamal::Cli::Healthcheck::Error, "container not ready (#{status})"
      end
    rescue Kamal::Cli::Healthcheck::Error => e
      if attempt <= max_attempts
        info "#{e.message}, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..."
        sleep attempt
        attempt += 1
        retry
      else
        raise
      end
    end

    info "Container is healthy!"
  end

  def wait_for_unhealthy(pause_after_ready: false, &block)
    attempt = 1
    max_attempts = KAMAL.config.healthcheck.max_attempts

    begin
      case status = block.call
      when "unhealthy"
        sleep TRAEFIK_UPDATE_DELAY if pause_after_ready
      else
        raise Kamal::Cli::Healthcheck::Error, "container not unhealthy (#{status})"
      end
    rescue Kamal::Cli::Healthcheck::Error => e
      if attempt <= max_attempts
        info "#{e.message}, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..."
        sleep attempt
        attempt += 1
        retry
      else
        raise
      end
    end

    info "Container is unhealthy!"
  end

  private
    def info(message)
      SSHKit.config.output.info(message)
    end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
kamal-1.9.2 lib/kamal/cli/healthcheck/poller.rb
kamal-1.9.1 lib/kamal/cli/healthcheck/poller.rb
kamal-1.9.0 lib/kamal/cli/healthcheck/poller.rb
kamal-2.0.0.alpha lib/kamal/cli/healthcheck/poller.rb
kamal-1.8.3 lib/kamal/cli/healthcheck/poller.rb
kamal-1.8.2 lib/kamal/cli/healthcheck/poller.rb
kamal-1.8.1 lib/kamal/cli/healthcheck/poller.rb
kamal-1.8.0 lib/kamal/cli/healthcheck/poller.rb
kamal-1.7.3 lib/kamal/cli/healthcheck/poller.rb
kamal-1.7.2 lib/kamal/cli/healthcheck/poller.rb
kamal-1.7.1 lib/kamal/cli/healthcheck/poller.rb
kamal-1.7.0 lib/kamal/cli/healthcheck/poller.rb