Configurable Healthcheck Expose Port

This commit is contained in:
Krzysztof Adamski
2023-09-05 10:53:32 +02:00
parent 9363b6a464
commit bbcc90e4d1
3 changed files with 9 additions and 5 deletions

View File

@@ -1,5 +1,4 @@
class Kamal::Commands::Healthcheck < Kamal::Commands::Base class Kamal::Commands::Healthcheck < Kamal::Commands::Base
EXPOSED_PORT = 3999
def run def run
web = config.role(:web) web = config.role(:web)
@@ -7,7 +6,7 @@ class Kamal::Commands::Healthcheck < Kamal::Commands::Base
docker :run, docker :run,
"--detach", "--detach",
"--name", container_name_with_version, "--name", container_name_with_version,
"--publish", "#{EXPOSED_PORT}:#{config.healthcheck["port"]}", "--publish", "#{exposed_port}:#{config.healthcheck["port"]}",
"--label", "service=#{container_name}", "--label", "service=#{container_name}",
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"", "-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
*web.env_args, *web.env_args,
@@ -52,6 +51,10 @@ class Kamal::Commands::Healthcheck < Kamal::Commands::Base
end end
def health_url def health_url
"http://localhost:#{EXPOSED_PORT}#{config.healthcheck["path"]}" "http://localhost:#{exposed_port}#{config.healthcheck["path"]}"
end
def exposed_port
config.healthcheck["exposed_port"]
end end
end end

View File

@@ -145,7 +145,7 @@ class Kamal::Configuration
def healthcheck def healthcheck
{ "path" => "/up", "port" => 3000, "max_attempts" => 7 }.merge(raw_config.healthcheck || {}) { "path" => "/up", "port" => 3000, "max_attempts" => 7, "exposed_port" => 3999 }.merge(raw_config.healthcheck || {})
end end
def readiness_delay def readiness_delay

View File

@@ -40,8 +40,9 @@ class CommandsHealthcheckTest < ActiveSupport::TestCase
test "run with custom options" do test "run with custom options" do
@config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere" } } } @config[:servers] = { "web" => { "hosts" => [ "1.1.1.1" ], "options" => { "mount" => "somewhere" } } }
@config[:healthcheck] = { "exposed_port" => 4999 }
assert_equal \ assert_equal \
"docker run --detach --name healthcheck-app-123 --publish 3999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123", "docker run --detach --name healthcheck-app-123 --publish 4999:3000 --label service=healthcheck-app -e KAMAL_CONTAINER_NAME=\"healthcheck-app\" --health-cmd \"curl -f http://localhost:3000/up || exit 1\" --health-interval \"1s\" --mount \"somewhere\" dhh/app:123",
new_command.run.join(" ") new_command.run.join(" ")
end end