use crate::grpc::state::GrpcState;
use tasker_shared::proto::v1::{
self as proto, health_service_server::HealthService as HealthServiceTrait,
};
use tonic::{Request, Response, Status};
use tracing::debug;
#[derive(Debug)]
pub struct HealthServiceImpl {
state: GrpcState,
}
impl HealthServiceImpl {
pub fn new(state: GrpcState) -> Self {
Self { state }
}
}
#[tonic::async_trait]
impl HealthServiceTrait for HealthServiceImpl {
async fn check_health(
&self,
_request: Request<proto::HealthRequest>,
) -> Result<Response<proto::HealthResponse>, Status> {
debug!("gRPC basic health check");
let response = self.state.services.health_service.basic_health();
Ok(Response::new(proto::HealthResponse::from(&response)))
}
async fn check_liveness(
&self,
_request: Request<proto::LivenessRequest>,
) -> Result<Response<proto::HealthResponse>, Status> {
debug!("gRPC liveness check");
let response = self.state.services.health_service.liveness().await;
Ok(Response::new(proto::HealthResponse::from(&response)))
}
async fn check_readiness(
&self,
_request: Request<proto::ReadinessRequest>,
) -> Result<Response<proto::ReadinessResponse>, Status> {
debug!("gRPC readiness check");
let result = self.state.services.health_service.readiness().await;
match result {
Ok(response) => Ok(Response::new(proto::ReadinessResponse::from(&response))),
Err(_) => {
Err(Status::unavailable("Service is not ready"))
}
}
}
async fn check_detailed_health(
&self,
_request: Request<proto::DetailedHealthRequest>,
) -> Result<Response<proto::DetailedHealthResponse>, Status> {
debug!("gRPC detailed health check");
let response = self.state.services.health_service.detailed_health().await;
Ok(Response::new(proto::DetailedHealthResponse::from(
&response,
)))
}
}