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(threading): Add threading to docker compose run #143

Closed
Closed
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
41 changes: 25 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,22 @@
return docker_compose_commands


def _run_cmd(
cmd: list[str], env: dict[str, str], command: 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(

Check warning on line 234 in devservices/utils/docker_compose.py

View check run for this annotation

Codecov / codecov/patch

devservices/utils/docker_compose.py#L233-L234

Added lines #L233 - L234 were not covered by tests
command=command,
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 +269,13 @@
)

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, command)
for cmd in docker_compose_commands
]
for future in concurrent.futures.as_completed(futures):
cmd_outputs.append(future.result())

return cmd_outputs