Skip to content

Commit b7e3f6f

Browse files
bhimrazypre-commit-ci[bot]aniketmaurya
authored
Feat: add custom health check logic (#430)
* feat: add health check method to LitAPI class * add: check custom LitAPI health status before returning server readiness * test: add custom health check method and corresponding tests * added feedback * docs: update health method documentation for clarity * fix: improve health check logic in tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/test_simple.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aniket Maurya <theaniketmaurya@gmail.com>
1 parent 38b3559 commit b7e3f6f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/litserve/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,15 @@ def has_active_requests(self) -> bool:
143143

144144
def has_capacity(self) -> bool:
145145
raise NotImplementedError("has_capacity is not implemented")
146+
147+
def health(self) -> bool:
148+
"""Check the additional health status of the API.
149+
150+
This method is used in the /health endpoint of the server to determine the health status.
151+
Users can extend this method to include additional health checks specific to their application.
152+
153+
Returns:
154+
bool: True if the API is healthy, False otherwise.
155+
156+
"""
157+
return True

src/litserve/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ async def health(request: Request) -> Response:
406406
if not workers_ready:
407407
workers_ready = all(v == WorkerSetupStatus.READY for v in self.workers_setup_status.values())
408408

409-
if workers_ready:
409+
lit_api_health_status = self.lit_api.health()
410+
if workers_ready and lit_api_health_status:
410411
return Response(content="ok", status_code=200)
411412

412413
return Response(content="not ready", status_code=503)

tests/test_simple.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ def setup(self, device):
5353
time.sleep(2)
5454

5555

56+
class SlowSetupWithCustomHealthLitAPI(SimpleLitAPI):
57+
def setup(self, device):
58+
self.model = lambda x: x**2
59+
time.sleep(2)
60+
61+
def health(self) -> bool:
62+
# Health check passes after 5 seconds from the first time it is called.
63+
if not hasattr(self, "_start_time"):
64+
self._start_time = time.time()
65+
return time.time() - self._start_time >= 5
66+
67+
5668
@pytest.mark.parametrize("use_zmq", [True, False])
5769
def test_workers_health(use_zmq):
5870
server = LitServer(
@@ -103,6 +115,38 @@ def test_workers_health_custom_path(use_zmq):
103115
assert response.text == "ok"
104116

105117

118+
@pytest.mark.parametrize("use_zmq", [True, False])
119+
def test_workers_health_with_custom_health_method(use_zmq):
120+
server = LitServer(
121+
SlowSetupWithCustomHealthLitAPI(),
122+
accelerator="cpu",
123+
devices=1,
124+
timeout=5,
125+
workers_per_device=2,
126+
fast_queue=use_zmq,
127+
)
128+
129+
with wrap_litserve_start(server) as server, TestClient(server.app) as client:
130+
response = client.get("/health")
131+
assert response.status_code == 503
132+
assert response.text == "not ready"
133+
134+
time.sleep(1)
135+
response = client.get("/health")
136+
assert response.status_code == 503
137+
assert response.text == "not ready"
138+
139+
time.sleep(1)
140+
response = client.get("/health")
141+
assert response.status_code == 503
142+
assert response.text == "not ready"
143+
144+
time.sleep(4)
145+
response = client.get("/health")
146+
assert response.status_code == 200
147+
assert response.text == "ok"
148+
149+
106150
def make_load_request(server, outputs):
107151
with TestClient(server.app) as client:
108152
for i in range(100):

0 commit comments

Comments
 (0)