Skip to content

Commit

Permalink
feat: add new option - wait_after_healthy
Browse files Browse the repository at this point in the history
This is a port of <wowu/docker-rollout#28>.
  • Loading branch information
c4710n committed Jun 26, 2024
1 parent 77ba19f commit 769d485
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/docker_compizo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule DockerCompizo do

opts = Keyword.merge(Default.options(), opts)
compose_file = Keyword.fetch!(opts, :compose_file)
scale_opts = Keyword.take(opts, [:healthcheck_timeout, :no_healthcheck_timeout])
scale_opts = Keyword.take(opts, [:healthcheck_timeout, :no_healthcheck_timeout, :wait_after_healthy])

context = %Context{
docker_bin: System.find_executable("docker"),
Expand Down Expand Up @@ -98,7 +98,8 @@ defmodule DockerCompizo do
deploy_opts = [
is_healthcheck_supported?: is_healthcheck_supported?(context, service),
healthcheck_timeout: Keyword.fetch!(opts, :healthcheck_timeout),
no_healthcheck_timeout: Keyword.fetch!(opts, :no_healthcheck_timeout)
no_healthcheck_timeout: Keyword.fetch!(opts, :no_healthcheck_timeout),
wait_after_healthy: Keyword.fetch!(opts, :wait_after_healthy)
]

running_services = Compose.list_running_services(context)
Expand Down
11 changes: 10 additions & 1 deletion lib/docker_compizo/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ defmodule DockerCompizo.Default do
[
compose_file: Path.expand("compose.yaml", File.cwd!()),
healthcheck_timeout: 60,
no_healthcheck_timeout: 10
no_healthcheck_timeout: 10,
# Q: Why do we need this option?
# A: When using a reverse proxy, such as Traefik, the reverse proxy could have their
# own logic to detect containers and route traffics to new container. If we kill
# the old containers immediately, it will lead to disruption of service.
#
# Because of that, it's better to add additional timeout to destroy old containers.
# This will give the new containers time to settle down, and a reverse proxy to
# routes traffics to new containers.
wait_after_healthy: 10
]
end
end
6 changes: 5 additions & 1 deletion lib/docker_compizo/strategy/blue_green.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule DockerCompizo.Strategy.BlueGreen do
def deploy(context, service,
is_healthcheck_supported?: is_healthcheck_supported?,
healthcheck_timeout: healthcheck_timeout,
no_healthcheck_timeout: no_healthcheck_timeout
no_healthcheck_timeout: no_healthcheck_timeout,
wait_after_healthy: wait_after_healthy
) do
Helper.report("Deploying '#{service}' service with Blue/Green strategy")

Expand All @@ -30,6 +31,9 @@ defmodule DockerCompizo.Strategy.BlueGreen do
Helper.report("Waiting for new containers to be healthy (timeout: #{healthcheck_timeout} seconds)")

if Container.check_health_util_timeout(context, new_containers, healthcheck_timeout) do
Helper.report("New containers are healthy, wait #{wait_after_healthy} seconds more")
Process.sleep(:timer.seconds(wait_after_healthy))

Helper.report("Cleaning old containers")
Container.destroy(context, old_containers)

Expand Down

0 comments on commit 769d485

Please sign in to comment.