I have a Quarkus app with health checking enabled running in a container on port 9000. I can make a health request to that app from another Quarkus app (call it app2) as shown below. It returns the health json. All good.
I am using docker-java to create and run the container on port 9000 and I want to know as soon as it is ready so I can start accessing it via Quarkus reactive rest client from app2. So, I want to poll it every second until I get the health check back and status UP (and some other "withData"). If, after 10 seconds I don't get the health json (and/or status is not "UP"), it should fail and return a "timeout" message.
What is the best approach for a Uni pipeline to do that kind of polling?
My proxy:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonObject;
@Path("/q/health")
@ClientHeaderParam(name = "Content-Type", value = "application/json") @Produces(MediaType.APPLICATION_JSON)
public interface OrgHealthProxy {
@GET
Uni<JsonObject> GETAsUni();
}
The single call that works as long as the container is up and running:
return orgHealthProxy.GETAsUni()
.onItem()
.transform(orgHealthJsonObject -> {
Log.info("getHealthDataFromOrgContainer Received response =" + orgHealthJsonObject.toString());
// eg {"status":"UP","checks":[....]}
// Get the overall status string UP DOWN
String status = orgHealthJsonObject.getString("status");
if (status.equals("UP")) {
// Maybe modify the JsonObject ...
// Then return the JsonObject
return orgHealthJsonObject;
} else {
String errMsg = "ERROR: the container is not ready. Status=" + status;
throw new Error(errMsg);
}
})
.onFailure().recoverWithItem(.... // Try call again somehow?);
So, how do I extend the Uni pipeline above to handle the polling and cope with failures like container not running yet (maybe died on docker run, or just too early), health checks not finished yet, etc?
Thanks, Murray
This can be done with Mutiny like this:
return orgHealthProxy.GETAsUni()
.onItem().invoke(s -> System.out.println("Trying " + s))
.onFailure().invoke(throwable -> System.out.println("Failed " + throwable))
.onFailure().retry()
.withBackOff(Duration.ofSeconds(1))
.atMost(9);
atMost(9)
retries means 10 together invocations with the original one.