To allow a gRPC-node server to support health checks, use the HealthImplementation class.
- Define a
statusMap where keys are service names and values are statuses (e.g., 'SERVING', 'NOT_SERVING'). - Use an empty string
'' as a key to represent the status of the entire server. - Instantiate
HealthImplementation with your map. - Call
healthImpl.addToServer(server) to attach the service to your existing server. - Use
healthImpl.setStatus(serviceName, status) to dynamically update the status of specific services.
// Import package
import { HealthImplementation, ServingStatusMap } from 'grpc-health-check';
// Define service status map. Key is the service name, value is the corresponding status.
// By convention, the empty string '' key represents that status of the entire server.
const statusMap = {
'ServiceFoo': 'SERVING',
'ServiceBar': 'NOT_SERVING',
'': 'NOT_SERVING',
};
// Construct the service implementation
const healthImpl = new HealthImplementation(statusMap);
healthImpl.addToServer(server);
// When ServiceBar comes up
healthImpl.setStatus('serviceBar', 'SERVING');