Skip to content

Commit

Permalink
Improves health check action to use a scheduled task to check readine…
Browse files Browse the repository at this point in the history
…ss periodically. Use config to init HealthObserver.

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Sep 6, 2024
1 parent 4727a6c commit 21f97f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions examples/webserver/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health-checks</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.scheduling</groupId>
<artifactId>helidon-scheduling</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void main(String[] args) {
// create a health check to verify gRPC endpoint
ObserveFeature observe = ObserveFeature.builder()
.addObserver(HealthObserver.builder()
.details(true)
.config(serverConfig.get("features.observe.observers.health"))
.addCheck(new StringServiceHealthCheck(serverConfig))
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@
*/
package io.helidon.examples.webserver.grpc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import io.helidon.common.configurable.Resource;
import io.helidon.common.tls.Tls;
import io.helidon.config.Config;
import io.helidon.health.HealthCheck;
import io.helidon.health.HealthCheckResponse;
import io.helidon.health.HealthCheckType;
import io.helidon.scheduling.Scheduling;
import io.helidon.webclient.api.WebClient;
import io.helidon.webclient.grpc.GrpcClient;

class StringServiceHealthCheck implements HealthCheck {

private final WebClient webClient;
private CountDownLatch latch;
private volatile boolean readiness = true;

StringServiceHealthCheck(Config config) {
// set up client to access gRPC service
Tls clientTls = Tls.builder()
.trust(trust -> trust
.keystore(store -> store
Expand All @@ -53,26 +60,44 @@ public HealthCheckType type() {
return HealthCheckType.READINESS;
}

@Override
public HealthCheckResponse call() {
if (latch == null) {
latch = new CountDownLatch(1);
Scheduling.fixedRate() // task to check for readiness
.delay(1)
.initialDelay(0)
.timeUnit(TimeUnit.MINUTES)
.task(i -> checkReadiness())
.build();
}
try {
boolean check = latch.await(5, TimeUnit.SECONDS);
return HealthCheckResponse.builder()
.status(check && readiness)
.get();
} catch (Exception e) {
return HealthCheckResponse.builder()
.status(false)
.get();
}
}

/**
* Self-invocation to verify gRPC endpoint is available and ready.
*
* @return health check response
*/
@Override
public HealthCheckResponse call() {
private void checkReadiness() {
try {
GrpcClient grpcClient = webClient.client(GrpcClient.PROTOCOL);
StringServiceGrpc.StringServiceBlockingStub service =
StringServiceGrpc.newBlockingStub(grpcClient.channel());
Strings.StringMessage res = service.upper(
Strings.StringMessage.newBuilder().setText("hello").build());
return HealthCheckResponse.builder()
.status(res.getText().equals("HELLO"))
.get();
readiness = res.getText().equals("HELLO");
} catch (Exception e) {
return HealthCheckResponse.builder()
.status(false)
.get();
readiness = false;
} finally {
latch.countDown();
}
}
}

0 comments on commit 21f97f7

Please sign in to comment.