diff --git a/devservices/commands/start.py b/devservices/commands/start.py index a05ad48..caa10f4 100644 --- a/devservices/commands/start.py +++ b/devservices/commands/start.py @@ -36,11 +36,15 @@ def start(args: Namespace) -> None: with Status(f"Starting {service.name}", f"{service.name} started") as status: try: run_docker_compose_command( - service, "up", mode_dependencies, ["-d"], force_update_dependencies=True + service, + "up", + mode_dependencies, + ["-d"], + force_update_dependencies=True, ) except DockerComposeError as dce: status.print(f"Failed to start {service.name}: {dce.stderr}") exit(1) # TODO: We should factor in healthchecks here before marking service as running state = State() - state.add_started_service(service.name) + state.add_started_service(service.name, mode_to_start) diff --git a/devservices/utils/state.py b/devservices/utils/state.py index 1632f94..978ffa4 100644 --- a/devservices/utils/state.py +++ b/devservices/utils/state.py @@ -28,22 +28,23 @@ def initialize_database(self) -> None: """ CREATE TABLE IF NOT EXISTS started_services ( service_name TEXT PRIMARY KEY, + mode TEXT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """ ) self.conn.commit() - def add_started_service(self, service_name: str) -> None: + def add_started_service(self, service_name: str, mode: str) -> None: cursor = self.conn.cursor() started_services = self.get_started_services() if service_name in started_services: return cursor.execute( """ - INSERT INTO started_services (service_name) VALUES (?) + INSERT INTO started_services (service_name, mode) VALUES (?, ?) """, - (service_name,), + (service_name, mode), ) self.conn.commit() @@ -65,3 +66,16 @@ def get_started_services(self) -> list[str]: """ ) return [row[0] for row in cursor.fetchall()] + + def get_mode_for_service(self, service_name: str) -> str | None: + cursor = self.conn.cursor() + cursor.execute( + """ + SELECT mode FROM started_services WHERE service_name = ? + """, + (service_name,), + ) + result = cursor.fetchone() + if result is None: + return None + return str(result[0]) diff --git a/tests/utils/test_state.py b/tests/utils/test_state.py index c3a129e..195033e 100644 --- a/tests/utils/test_state.py +++ b/tests/utils/test_state.py @@ -15,15 +15,17 @@ def test_state_simple(tmp_path: Path) -> None: def test_state_add_started_service(tmp_path: Path) -> None: with mock.patch("devservices.utils.state.DB_FILE", str(tmp_path / "state")): state = State() - state.add_started_service("example-service") + state.add_started_service("example-service", "default") assert state.get_started_services() == ["example-service"] + assert state.get_mode_for_service("example-service") == "default" def test_state_remove_started_service(tmp_path: Path) -> None: with mock.patch("devservices.utils.state.DB_FILE", str(tmp_path / "state")): state = State() - state.add_started_service("example-service") + state.add_started_service("example-service", "default") assert state.get_started_services() == ["example-service"] + assert state.get_mode_for_service("example-service") == "default" state.remove_started_service("example-service") assert state.get_started_services() == [] @@ -38,7 +40,15 @@ def test_state_remove_unknown_service(tmp_path: Path) -> None: def test_start_service_twice(tmp_path: Path) -> None: with mock.patch("devservices.utils.state.DB_FILE", str(tmp_path / "state")): state = State() - state.add_started_service("example-service") + state.add_started_service("example-service", "default") assert state.get_started_services() == ["example-service"] - state.add_started_service("example-service") + assert state.get_mode_for_service("example-service") == "default" + state.add_started_service("example-service", "default") assert state.get_started_services() == ["example-service"] + assert state.get_mode_for_service("example-service") == "default" + + +def test_get_mode_for_nonexistent_service(tmp_path: Path) -> None: + with mock.patch("devservices.utils.state.DB_FILE", str(tmp_path / "state")): + state = State() + assert state.get_mode_for_service("unknown-service") is None