Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(start): Add threading and condition to wait for containers to be healthy #144

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion devservices/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def start(args: Namespace) -> None:
"up",
mode_dependencies,
remote_dependencies,
options=["-d"],
options=["-d", "--wait"],
)
except DockerComposeError as dce:
status.failure(f"Failed to start {service.name}: {dce.stderr}")
Expand Down
39 changes: 23 additions & 16 deletions devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import concurrent
import logging
import os
import platform
Expand Down Expand Up @@ -222,6 +223,20 @@ def _get_docker_compose_commands_to_run(
return docker_compose_commands


def _run_cmd(cmd: list[str], env: dict[str, str]) -> subprocess.CompletedProcess[str]:
logger = logging.getLogger(LOGGER_NAME)
try:
logger.debug(f"Running command: {' '.join(cmd)}")
return subprocess.run(cmd, check=True, capture_output=True, text=True, env=env)
except subprocess.CalledProcessError as e:
raise DockerComposeError(
command=" ".join(cmd),
returncode=e.returncode,
stdout=e.stdout,
stderr=e.stderr,
) from e


def run_docker_compose_command(
service: Service,
command: str,
Expand Down Expand Up @@ -252,21 +267,13 @@ def run_docker_compose_command(
)

cmd_outputs = []
for cmd in docker_compose_commands:
try:
logger = logging.getLogger(LOGGER_NAME)
logger.debug(f"Running command: {' '.join(cmd)}")
cmd_outputs.append(
subprocess.run(
cmd, check=True, capture_output=True, text=True, env=current_env
)
)
except subprocess.CalledProcessError as e:
raise DockerComposeError(
command=command,
returncode=e.returncode,
stdout=e.stdout,
stderr=e.stderr,
) from e

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(_run_cmd, cmd, current_env)
for cmd in docker_compose_commands
]
for future in concurrent.futures.as_completed(futures):
cmd_outputs.append(future.result())

return cmd_outputs
1 change: 1 addition & 0 deletions tests/commands/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_start_simple(
"clickhouse",
"redis",
"-d",
"--wait",
],
check=True,
capture_output=True,
Expand Down