Skip to content

Commit c929a86

Browse files
authored
fix: handle is_healthy() AttributeError when not initialized (closes #298) (#322)
ObserverService.is_healthy() threw AttributeError when called before initialize() because self._config and self._vikingdb were None. - Add None guards to vikingdb and vlm properties returning unhealthy ComponentStatus instead of crashing - Add early return False in is_healthy() when dependencies not ready - Add 4 tests covering the uninitialized state Co-authored-by: r266-tech <r266-tech@users.noreply.github.com>
1 parent 9948390 commit c929a86

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

openviking/service/debug_service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def set_dependencies(
7373
self._vikingdb = vikingdb
7474
self._config = config
7575

76+
@property
77+
def _dependencies_ready(self) -> bool:
78+
"""Check if both vikingdb and config dependencies are set."""
79+
return self._vikingdb is not None and self._config is not None
80+
7681
@property
7782
def queue(self) -> ComponentStatus:
7883
"""Get queue status."""
@@ -87,6 +92,13 @@ def queue(self) -> ComponentStatus:
8792
@property
8893
def vikingdb(self) -> ComponentStatus:
8994
"""Get VikingDB status."""
95+
if self._vikingdb is None:
96+
return ComponentStatus(
97+
name="vikingdb",
98+
is_healthy=False,
99+
has_errors=True,
100+
status="Not initialized",
101+
)
90102
observer = VikingDBObserver(self._vikingdb)
91103
return ComponentStatus(
92104
name="vikingdb",
@@ -98,6 +110,13 @@ def vikingdb(self) -> ComponentStatus:
98110
@property
99111
def vlm(self) -> ComponentStatus:
100112
"""Get VLM status."""
113+
if self._config is None:
114+
return ComponentStatus(
115+
name="vlm",
116+
is_healthy=False,
117+
has_errors=True,
118+
status="Not initialized",
119+
)
101120
observer = VLMObserver(self._config.vlm.get_vlm_instance())
102121
return ComponentStatus(
103122
name="vlm",
@@ -143,6 +162,8 @@ def system(self) -> SystemStatus:
143162

144163
def is_healthy(self) -> bool:
145164
"""Quick health check."""
165+
if not self._dependencies_ready:
166+
return False
146167
return self.system.is_healthy
147168

148169

tests/misc/test_debug_service.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,38 @@ def test_is_healthy_returns_true(
284284
status = service.system
285285
assert all(c.is_healthy for name, c in status.components.items() if name != "transaction")
286286

287+
def test_is_healthy_without_dependencies(self):
288+
"""Test is_healthy returns False (not raises) when dependencies not set."""
289+
service = ObserverService()
290+
assert service.is_healthy() is False
291+
292+
def test_vikingdb_property_without_dependency(self):
293+
"""Test vikingdb property returns unhealthy ComponentStatus when vikingdb is None."""
294+
service = ObserverService()
295+
status = service.vikingdb
296+
assert isinstance(status, ComponentStatus)
297+
assert status.name == "vikingdb"
298+
assert status.is_healthy is False
299+
assert status.has_errors is True
300+
assert status.status == "Not initialized"
301+
302+
def test_vlm_property_without_dependency(self):
303+
"""Test vlm property returns unhealthy ComponentStatus when config is None."""
304+
service = ObserverService()
305+
status = service.vlm
306+
assert isinstance(status, ComponentStatus)
307+
assert status.name == "vlm"
308+
assert status.is_healthy is False
309+
assert status.has_errors is True
310+
assert status.status == "Not initialized"
311+
312+
def test_system_property_without_dependencies(self):
313+
"""Test system property returns unhealthy SystemStatus when dependencies not set."""
314+
service = ObserverService()
315+
status = service.system
316+
assert isinstance(status, SystemStatus)
317+
assert status.is_healthy is False
318+
287319
@patch("openviking.service.debug_service.get_queue_manager")
288320
@patch("openviking.service.debug_service.QueueObserver")
289321
@patch("openviking.service.debug_service.VikingDBObserver")

0 commit comments

Comments
 (0)