Class: Mrsk::Utils::HealthcheckPoller

Inherits:
Object
  • Object
show all
Defined in:
lib/mrsk/utils/healthcheck_poller.rb

Defined Under Namespace

Classes: HealthcheckError

Constant Summary collapse

TRAEFIK_HEALTHY_DELAY =
2

Class Method Summary collapse

Class Method Details

.wait_for_healthy(pause_after_ready: false, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mrsk/utils/healthcheck_poller.rb', line 7

def wait_for_healthy(pause_after_ready: false, &block)
  attempt = 1
  max_attempts = MRSK.config.healthcheck["max_attempts"]

  begin
    case status = block.call
    when "healthy"
      sleep TRAEFIK_HEALTHY_DELAY if pause_after_ready
    when "running" # No health check configured
      sleep MRSK.config.readiness_delay if pause_after_ready
    else
      raise HealthcheckError, "container not ready (#{status})"
    end
  rescue HealthcheckError => 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