Skip to content

Commit

Permalink
feat: Add --wait-timeout option for docker.compose.up (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taragolis committed Apr 10, 2024
1 parent ac0e53b commit a10959c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ def up(
no_attach_services: Union[List[str], str, None] = ...,
pull: Literal["always", "missing", "never", None] = ...,
stream_logs: Literal[True] = ...,
wait_timeout: Optional[int] = ...,
) -> Iterable[Tuple[str, bytes]]:
...

Expand All @@ -927,6 +928,7 @@ def up(
no_attach_services: Union[List[str], str, None] = ...,
pull: Literal["always", "missing", "never", None] = ...,
stream_logs: Literal[False] = ...,
wait_timeout: Optional[int] = ...,
) -> None:
...

Expand All @@ -951,6 +953,7 @@ def up(
no_attach_services: Union[List[str], str, None] = None,
pull: Literal["always", "missing", "never", None] = None,
stream_logs: bool = False,
wait_timeout: Optional[int] = None,
):
"""Start the containers.
Expand Down Expand Up @@ -995,6 +998,7 @@ def up(
as bytes, you'll need to call `.decode()` if you want the logs as `str`.
See [the streaming guide](https://gabrieldemarmiesse.github.io/python-on-whales/user_guide/docker_run/#stream-the-output) if you are
not familiar with the streaming of logs in Python-on-whales.
wait_timeout: Maximum duration to wait for the project to be running|healthy
"""
if quiet and stream_logs:
raise ValueError(
Expand All @@ -1005,6 +1009,7 @@ def up(
full_cmd.add_flag("--build", build)
full_cmd.add_flag("--detach", detach)
full_cmd.add_flag("--wait", wait)
full_cmd.add_simple_arg("--wait-timeout", wait_timeout)
full_cmd.add_flag("--abort-on-container-exit", abort_on_container_exit)
for service, scale in scales.items():
full_cmd.add_simple_arg("--scale", f"{service}={scale}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ services:
interval: 1s
timeout: 3s
retries: 5

unhealthy_service:
image: busybox:latest
command: sleep infinity
healthcheck:
test: ["CMD-SHELL", "false"]
interval: 1s
timeout: 3s
retries: 5
14 changes: 14 additions & 0 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def test_wait_for_service():
assert container.state.health.status == "healthy"
docker.compose.down(timeout=1, volumes=True)

# now we use wait with timeout and check that timeout respects
with pytest.raises(DockerException) as exc_info:
list(
docker.compose.up(
["unhealthy_service"],
detach=True,
wait=True,
wait_timeout=1,
stream_logs=True,
)
)
assert "application not healthy after" in exc_info.value.stderr
docker.compose.down(timeout=1, volumes=True)


def test_pause_empty_list_of_services():
docker.compose.up(["my_service", "busybox", "alpine"], detach=True)
Expand Down

0 comments on commit a10959c

Please sign in to comment.