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>.

some
  • Loading branch information
c4710n committed Jun 26, 2024
1 parent 77ba19f commit afc14ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 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
23 changes: 22 additions & 1 deletion lib/docker_compizo/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ defmodule DockerCompizo.CLI do
parser: :integer,
required: false,
default: Keyword.fetch!(defaultOptions, :no_healthcheck_timeout)
],
wait_after_healthy: [
value_name: "SECONDS",
short: "-l",
long: "--wait-after-healthy",
help: """
Time in seconds to wait after new container is ready.
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.
""",
parser: :integer,
required: false,
default: Keyword.fetch!(defaultOptions, :wait_after_healthy)
]
]
)
Expand All @@ -70,7 +90,8 @@ defmodule DockerCompizo.CLI do
%{
compose_file: _,
healthcheck_timeout: _,
no_healthcheck_timeout: _
no_healthcheck_timeout: _,
wait_after_healthy: _
} = opts
} = Optimus.parse!(optimus, argv)

Expand Down
3 changes: 2 additions & 1 deletion lib/docker_compizo/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule DockerCompizo.Default do
[
compose_file: Path.expand("compose.yaml", File.cwd!()),
healthcheck_timeout: 60,
no_healthcheck_timeout: 10
no_healthcheck_timeout: 10,
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 afc14ab

Please sign in to comment.