From 3969f56fa62eecab4e8f8fb9a2bed3457348ec84 Mon Sep 17 00:00:00 2001 From: Arturo Ojeda Date: Sun, 9 Apr 2023 12:07:27 -0600 Subject: [PATCH] Improved: configurable max_attempts for healthcheck --- lib/mrsk/cli/healthcheck.rb | 5 ++--- lib/mrsk/configuration.rb | 2 +- test/configuration_test.rb | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/mrsk/cli/healthcheck.rb b/lib/mrsk/cli/healthcheck.rb index 94950373..5e9f42dc 100644 --- a/lib/mrsk/cli/healthcheck.rb +++ b/lib/mrsk/cli/healthcheck.rb @@ -1,5 +1,4 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base - DEFAULT_MAX_ATTEMPTS = 7 class HealthcheckError < StandardError; end @@ -13,7 +12,7 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base target = "Health check against #{MRSK.config.healthcheck["path"]}" attempt = 1 - max_attempts = MRSK.config.healthcheck["max_attempts"] || DEFAULT_MAX_ATTEMPTS + max_attempts = MRSK.config.healthcheck["max_attempts"] begin status = capture_with_info(*MRSK.healthcheck.curl) @@ -25,7 +24,7 @@ class Mrsk::Cli::Healthcheck < Mrsk::Cli::Base end rescue SSHKit::Command::Failed if attempt <= max_attempts - info "#{target} failed to respond, retrying in #{attempt}s..." + info "#{target} failed to respond, retrying in #{attempt}s (attempt #{attempt}/#{max_attempts})..." sleep attempt attempt += 1 diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index d6cf09f1..3e58a85a 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -156,7 +156,7 @@ class Mrsk::Configuration end def healthcheck - { "path" => "/up", "port" => 3000 }.merge(raw_config.healthcheck || {}) + { "path" => "/up", "port" => 3000, "max_attempts" => 7 }.merge(raw_config.healthcheck || {}) end def readiness_delay diff --git a/test/configuration_test.rb b/test/configuration_test.rb index ec65686d..2697673b 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -249,6 +249,6 @@ class ConfigurationTest < ActiveSupport::TestCase end test "to_h" do - assert_equal({ :roles=>["web"], :hosts=>["1.1.1.1", "1.1.1.2"], :primary_host=>"1.1.1.1", :version=>"missing", :repository=>"dhh/app", :absolute_image=>"dhh/app:missing", :service_with_version=>"app-missing", :env_args=>["-e", "REDIS_URL=\"redis://x/y\""], :ssh_options=>{:user=>"root", :auth_methods=>["publickey"]}, :volume_args=>["--volume", "/local/path:/container/path"], :logging=>["--log-opt", "max-size=\"10m\""], :healthcheck=>{"path"=>"/up", "port"=>3000 }}, @config.to_h) + assert_equal({ :roles=>["web"], :hosts=>["1.1.1.1", "1.1.1.2"], :primary_host=>"1.1.1.1", :version=>"missing", :repository=>"dhh/app", :absolute_image=>"dhh/app:missing", :service_with_version=>"app-missing", :env_args=>["-e", "REDIS_URL=\"redis://x/y\""], :ssh_options=>{:user=>"root", :auth_methods=>["publickey"]}, :volume_args=>["--volume", "/local/path:/container/path"], :logging=>["--log-opt", "max-size=\"10m\""], :healthcheck=>{"path"=>"/up", "port"=>3000, "max_attempts" => 7 }}, @config.to_h) end end