feat: add health check service to gRPC server
All checks were successful
CI / test (push) Successful in 3s
CI / build-ha-gateway (push) Successful in 52s
CI / build-discord-bot (push) Successful in 45s

This commit is contained in:
Nik Afiq 2026-04-06 21:17:21 +09:00
parent 5f0ae449b2
commit 7a0b0f1540

View File

@ -12,6 +12,8 @@ import (
"github.com/joho/godotenv"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
grpc_health_v1 "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
@ -79,12 +81,15 @@ func main() {
grpc.ChainUnaryInterceptor(loggingUnaryInterceptor),
grpc.ChainStreamInterceptor(loggingStreamInterceptor),
)
healthSrv := health.NewServer()
healthSrv.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// 7. Register services.
hav1.RegisterEntityServiceServer(srv, grpcadapter.NewEntityGRPC(entityApp))
hav1.RegisterLightServiceServer(srv, grpcadapter.NewLightGRPC(lightApp))
hav1.RegisterSwitchServiceServer(srv, grpcadapter.NewSwitchGRPC(switchApp))
hav1.RegisterEventServiceServer(srv, &grpcadapter.EventGRPC{})
grpc_health_v1.RegisterHealthServer(srv, healthSrv)
reflection.Register(srv)
// 8. Start listener.
@ -107,6 +112,7 @@ func main() {
slog.Info("shutting down")
// 11. Graceful stop, then flush telemetry.
healthSrv.SetServingStatus("", grpc_health_v1.HealthCheckResponse_NOT_SERVING)
srv.GracefulStop()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)