Skip to content

Commit

Permalink
add mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertdeng123 committed Nov 1, 2024
1 parent d0639df commit cb7f573
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
8 changes: 6 additions & 2 deletions devservices/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 17 additions & 3 deletions devservices/utils/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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])
18 changes: 14 additions & 4 deletions tests/utils/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() == []

Expand All @@ -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

0 comments on commit cb7f573

Please sign in to comment.