Class: Mrsk::Cli::Healthcheck

Inherits:
Base
  • Object
show all
Defined in:
lib/mrsk/cli/healthcheck.rb

Defined Under Namespace

Classes: HealthcheckError

Constant Summary collapse

MAX_ATTEMPTS =
7

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Mrsk::Cli::Base

Instance Method Details

#performObject



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
48
49
# File 'lib/mrsk/cli/healthcheck.rb', line 9

def perform
  on(MRSK.primary_host) do
    begin
      execute *MRSK.healthcheck.run

      target = "Health check against #{MRSK.config.healthcheck["path"]}"
      attempt = 1

      begin
        status = capture_with_info(*MRSK.healthcheck.curl)

        if status == "200"
          info "#{target} succeeded with 200 OK!"
        else
          raise HealthcheckError, "#{target} failed with status #{status}"
        end
      rescue SSHKit::Command::Failed
        if attempt <= MAX_ATTEMPTS
          info "#{target} failed to respond, retrying in #{attempt}s..."
          sleep attempt
          attempt += 1

          retry
        else
          raise
        end
      end
    rescue SSHKit::Command::Failed, HealthcheckError => e
      error capture_with_info(*MRSK.healthcheck.logs)

      if e.message =~ /curl/
        raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
      else
        raise
      end
    ensure
      execute *MRSK.healthcheck.stop, raise_on_non_zero_exit: false
      execute *MRSK.healthcheck.remove, raise_on_non_zero_exit: false
    end
  end
end