Switch to ruby-based retry
Retry connection errors with backoff
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
|
class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
|
||||||
|
MAX_ATTEMPTS = 5
|
||||||
|
|
||||||
default_command :perform
|
default_command :perform
|
||||||
|
|
||||||
desc "perform", "Health check current app version"
|
desc "perform", "Health check current app version"
|
||||||
@@ -8,18 +10,31 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base
|
|||||||
execute *MRSK.healthcheck.run
|
execute *MRSK.healthcheck.run
|
||||||
|
|
||||||
target = "Health check against #{MRSK.config.healthcheck["path"]}"
|
target = "Health check against #{MRSK.config.healthcheck["path"]}"
|
||||||
|
attempt = 1
|
||||||
|
|
||||||
if capture_with_info(*MRSK.healthcheck.curl) == "200"
|
begin
|
||||||
info "#{target} succeeded with 200 OK!"
|
status = capture_with_info(*MRSK.healthcheck.curl)
|
||||||
else
|
|
||||||
# Catches 1xx, 2xx, 3xx
|
if status == "200"
|
||||||
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
|
info "#{target} succeeded with 200 OK!"
|
||||||
|
else
|
||||||
|
raise "#{target} failed with status #{status}"
|
||||||
|
end
|
||||||
|
rescue SSHKit::Command::Failed
|
||||||
|
if attempt <= MAX_ATTEMPTS
|
||||||
|
info "#{target} not ready yet, retrying in #{attempt}s..."
|
||||||
|
sleep attempt
|
||||||
|
attempt += 1
|
||||||
|
|
||||||
|
retry
|
||||||
|
else
|
||||||
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
rescue SSHKit::Command::Failed => e
|
rescue SSHKit::Command::Failed => e
|
||||||
error capture_with_info(*MRSK.healthcheck.logs)
|
error capture_with_info(*MRSK.healthcheck.logs)
|
||||||
|
|
||||||
if e.message =~ /curl/
|
if e.message =~ /curl/
|
||||||
# Catches 4xx, 5xx
|
|
||||||
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
|
raise SSHKit::Command::Failed, "#{target} failed to return 200 OK!"
|
||||||
else
|
else
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ class Mrsk::Commands::Healthcheck < Mrsk::Commands::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def curl
|
def curl
|
||||||
[ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", "--retry-max-time", 10, "--retry", 5, health_url ]
|
[ :curl, "--silent", "--output", "/dev/null", "--write-out", "'%{http_code}'", "--max-time", "2", health_url ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def logs
|
def logs
|
||||||
pipe container_id, xargs(docker(:logs, "2>&1"))
|
pipe container_id, xargs(docker(:logs, "--tail", 50, "2>&1"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop
|
def stop
|
||||||
|
|||||||
Reference in New Issue
Block a user