Module: Kamal::Cli::Healthcheck::Poller

Extended by:
Poller
Included in:
Poller
Defined in:
lib/kamal/cli/healthcheck/poller.rb

Constant Summary collapse

NO_HEALTHCHECK =
Kamal::Commands::Base::NO_HEALTHCHECK

Instance Method Summary collapse

Instance Method Details

#wait_for_healthy(role:, &block) ⇒ Object



6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kamal/cli/healthcheck/poller.rb', line 6

def wait_for_healthy(role:, &block)
  attempt = 1
  timeout_at = Time.now + KAMAL.config.deploy_timeout
  readiness_delay = role.readiness_delay

  begin
    status = block.call

    if unchecked?(status)
      ensure_no_healthcheck_drift(role, status)

      if docker_state(status) == "running"
        announce_missing_gate(role, readiness_delay)

        # Wait for the readiness delay and confirm it is still running
        if readiness_delay > 0
          sleep readiness_delay
          status = block.call
          ensure_no_healthcheck_drift(role, status)
        end
      end
    end

    unless acceptable?(status)
      raise Kamal::Cli::Healthcheck::Error, "container not ready after #{KAMAL.config.deploy_timeout} seconds (#{status})"
    end
  rescue Kamal::Cli::Healthcheck::Error => e
    time_left = timeout_at - Time.now
    if time_left > 0
      sleep_for = [ attempt, time_left ].min
      elapsed = KAMAL.config.deploy_timeout - time_left
      info "Container not ready yet, retrying in #{sleep_for.ceil}s (#{elapsed.round}s elapsed, #{time_left.round}s left)"
      sleep sleep_for
      attempt += 1
      retry
    else
      raise
    end
  end

  info "Container is healthy!"
end