Skip to content

Commit

Permalink
feat(stop): Add more context for what is going on underneath the hood…
Browse files Browse the repository at this point in the history
… for stop (#149)

* add more context for what is going on underneath the hood for stop
  • Loading branch information
hubertdeng123 authored Nov 18, 2024
1 parent e9c2eca commit ecf0cee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 14 additions & 2 deletions devservices/commands/down.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import concurrent.futures
import os
import subprocess
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace
Expand All @@ -13,6 +14,7 @@
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR_KEY
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DOCKER_COMPOSE_COMMAND_LENGTH
from devservices.exceptions import DependencyError
from devservices.exceptions import DockerComposeError
from devservices.utils.console import Console
Expand Down Expand Up @@ -82,7 +84,7 @@ def down(args: Namespace) -> None:
service, remote_dependencies
)
try:
_down(service, remote_dependencies, mode_dependencies)
_down(service, remote_dependencies, mode_dependencies, status)
except DockerComposeError as dce:
capture_exception(dce)
status.failure(f"Failed to stop {service.name}: {dce.stderr}")
Expand All @@ -93,10 +95,20 @@ def down(args: Namespace) -> None:
state.remove_started_service(service.name)


def _bring_down_dependency(
cmd: list[str], current_env: dict[str, str], status: Status
) -> subprocess.CompletedProcess[str]:
# TODO: Get rid of these constants, we need a smarter way to determine the containers being brought down
for dependency in cmd[DOCKER_COMPOSE_COMMAND_LENGTH:]:
status.info(f"Stopping {dependency}")
return run_cmd(cmd, current_env)


def _down(
service: Service,
remote_dependencies: set[InstalledRemoteDependency],
mode_dependencies: list[str],
status: Status,
) -> None:
relative_local_dependency_directory = os.path.relpath(
os.path.join(DEVSERVICES_DEPENDENCIES_CACHE_DIR, DEPENDENCY_CONFIG_VERSION),
Expand Down Expand Up @@ -124,7 +136,7 @@ def _down(

with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(run_cmd, cmd, current_env)
executor.submit(_bring_down_dependency, cmd, current_env, status)
for cmd in docker_compose_commands
]
for future in concurrent.futures.as_completed(futures):
Expand Down
15 changes: 13 additions & 2 deletions tests/commands/test_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
),
)
@mock.patch("devservices.utils.state.State.remove_started_service")
def test_down_simple(
mock_remove_started_service: mock.Mock, mock_run: mock.Mock, tmp_path: Path
def test_up_simple(
mock_remove_started_service: mock.Mock,
mock_run: mock.Mock,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
with mock.patch(
"devservices.commands.down.DEVSERVICES_DEPENDENCIES_CACHE_DIR",
Expand Down Expand Up @@ -91,6 +94,10 @@ def test_down_simple(

mock_remove_started_service.assert_called_with("example-service")

captured = capsys.readouterr()
assert "Stopping clickhouse" in captured.out.strip()
assert "Stopping redis" in captured.out.strip()


@mock.patch("devservices.utils.docker_compose.subprocess.run")
@mock.patch("devservices.utils.state.State.remove_started_service")
Expand Down Expand Up @@ -140,3 +147,7 @@ def test_down_error(
)

mock_remove_started_service.assert_not_called()

captured = capsys.readouterr()
assert "Stopping clickhouse" not in captured.out.strip()
assert "Stopping redis" not in captured.out.strip()

0 comments on commit ecf0cee

Please sign in to comment.