Skip to content

Commit 0641b61

Browse files
authored
Merge pull request #5 from code0-tech/3-implement-health-service
Implement health service and proper shutdown
2 parents 20c68d5 + b710ffc commit 0641b61

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

main.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import concurrent.futures
22
import os
3+
import signal
34
import sys
45
import time
56

@@ -24,6 +25,7 @@
2425
import tucana.generated.velorum.info_pb2_grpc as info_pb2_grpc
2526
import tucana.generated.velorum.info_pb2 as info_pb2
2627
from grpc_reflection.v1alpha import reflection
28+
from grpc_health.v1 import health, health_pb2, health_pb2_grpc
2729

2830
from src.endpoint.info.info_endpoint import InfoService
2931
from src.endpoint.generation.generate_endpoint import GenerateService
@@ -37,6 +39,12 @@
3739
generate_pb2_grpc.add_GenerateServiceServicer_to_server(GenerateService(), server)
3840
info_pb2_grpc.add_InfoServiceServicer_to_server(InfoService(), server)
3941

42+
43+
health_servicer = health.HealthServicer()
44+
health_servicer.set('liveness', health_pb2.HealthCheckResponse.SERVING)
45+
health_servicer.set('readiness', health_pb2.HealthCheckResponse.SERVING)
46+
health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
47+
4048
port = os.getenv("HOST", "0.0.0.0") + ":" + os.getenv("PORT", "50051")
4149
server.add_insecure_port(port)
4250
log.success(f"Velorum listening on {port}") # type: ignore[attr-defined]
@@ -50,9 +58,13 @@
5058

5159
server.start()
5260

53-
try:
54-
while True:
55-
time.sleep(86400)
56-
except KeyboardInterrupt:
61+
62+
def shutdown(signum, frame):
5763
log.info("Shutting down gracefully...")
58-
server.stop(0)
64+
health_servicer.enter_graceful_shutdown()
65+
server.stop(5).wait()
66+
67+
signal.signal(signal.SIGTERM, shutdown)
68+
signal.signal(signal.SIGINT, shutdown)
69+
70+
server.wait_for_termination()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires-python = ">=3.10,<3.13"
77
dependencies = [
88
"apscheduler>=3.11.2",
99
"grpcio>=1.80.0",
10+
"grpcio-health-checking>=1.80.0",
1011
"grpcio-reflection>=1.80.0",
1112
"instructor>=1.15.1",
1213
"PyJWT>=2.4.0",

src/interceptor/auth_interceptor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
class AuthInterceptor(grpc.ServerInterceptor):
1414

1515
def intercept_service(self, continuation, handler_call_details):
16+
method = handler_call_details.method or ''
17+
if method.startswith('/grpc.health.v1.Health/'):
18+
return continuation(handler_call_details)
19+
1620
metadata = dict(handler_call_details.invocation_metadata or [])
1721
auth_token = metadata.get('authorization', '')
1822

uv.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)