// Copyright Epic Games, Inc. All Rights Reserved.
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Health.V1;
using static Grpc.Health.V1.HealthCheckResponse.Types;
namespace HordeServer.Server
{
///
/// Implements the gRPC health checking protocol
/// See https://github.com/grpc/grpc/blob/master/doc/health-checking.md for details
///
public class HealthService : Health.HealthBase
{
readonly LifetimeService _lifetimeService;
///
/// Constructor
///
/// The application lifetime
public HealthService(LifetimeService lifetimeService)
{
_lifetimeService = lifetimeService;
}
///
/// Return server status, primarily intended for load balancers to decide if traffic should be routed to this process
/// The supplied service name is currently ignored.
///
/// For example, the gRPC health check for AWS ALB will pick up and react to the gRPC status code returned.
///
/// Empty placeholder request (for now)
/// Context for the call
/// Return status code 'unavailable' if stopping
public override Task Check(HealthCheckRequest request, ServerCallContext context)
{
ServingStatus status = ServingStatus.Serving;
bool isStopping = _lifetimeService.IsPreStopping || _lifetimeService.IsStopping;
if (isStopping)
{
context.Status = new Status(StatusCode.Unavailable, "Server is stopping");
status = ServingStatus.NotServing;
}
return Task.FromResult(new HealthCheckResponse { Status = status });
}
///
/// Stream the server health status (not implemented)
///
///
///
///
///
public override Task Watch(HealthCheckRequest request, IServerStreamWriter responseStream, ServerCallContext context)
{
return Task.FromException(new RpcException(new Status(StatusCode.Unimplemented, "Watch() not implemented")));
}
}
}