Skip to content

Commit

Permalink
Merge pull request #4549 from cescoffier/features/health-check-blocking
Browse files Browse the repository at this point in the history
Call the health check method on the worker pool
  • Loading branch information
kenfinnigan authored Oct 14, 2019
2 parents 085338d + c375339 commit 235ed78
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.quarkus.smallrye.health.runtime.SmallRyeLivenessHandler;
import io.quarkus.smallrye.health.runtime.SmallRyeReadinessHandler;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HandlerType;
import io.smallrye.health.SmallRyeHealthReporter;

class SmallRyeHealthProcessor {
Expand Down Expand Up @@ -91,11 +92,13 @@ void build(SmallRyeHealthRecorder recorder, RecorderContext recorderContext,
feature.produce(new FeatureBuildItem(FeatureBuildItem.SMALLRYE_HEALTH));

// Register the health handler
routes.produce(new RouteBuildItem(health.rootPath, new SmallRyeHealthHandler()));
routes.produce(new RouteBuildItem(health.rootPath, new SmallRyeHealthHandler(), HandlerType.BLOCKING));
routes.produce(
new RouteBuildItem(health.rootPath + health.livenessPath, new SmallRyeLivenessHandler()));
new RouteBuildItem(health.rootPath + health.livenessPath, new SmallRyeLivenessHandler(),
HandlerType.BLOCKING));
routes.produce(
new RouteBuildItem(health.rootPath + health.readinessPath, new SmallRyeReadinessHandler()));
new RouteBuildItem(health.rootPath + health.readinessPath, new SmallRyeReadinessHandler(),
HandlerType.BLOCKING));

// Make ArC discover the beans marked with the @Health qualifier
beanDefiningAnnotation.produce(new BeanDefiningAnnotationBuildItem(HEALTH));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.quarkus.smallrye.health.test;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;

import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.Readiness;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class DispatchedThreadTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(LivenessHealthCheckCapturingThread.class, ReadinessHealthCheckCapturingThread.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void check() {
RestAssured.when().get("/health/live").then()
.body("status", is("UP"),
"checks.status", contains("UP"),
"checks.name", contains("my-liveness-check"),
"checks.data.thread[0]", stringContainsInOrder("worker"),
"checks.data.thread[0]", not(stringContainsInOrder("loop")),
"checks.data.request[0]", is(true));

RestAssured.when().get("/health/ready").then()
.body("status", is("UP"),
"checks.status", contains("UP"),
"checks.name", contains("my-readiness-check"),
"checks.data.thread[0]", stringContainsInOrder("worker"),
"checks.data.thread[0]", not(stringContainsInOrder("loop")),
"checks.data.request[0]", is(true));
}

@ApplicationScoped
@Liveness
public static class LivenessHealthCheckCapturingThread implements HealthCheck {
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.named("my-liveness-check")
.up()
.withData("thread", Thread.currentThread().getName())
.withData("request", Arc.container().requestContext().isActive())
.build();
}
}

@ApplicationScoped
@Readiness
public static class ReadinessHealthCheckCapturingThread implements HealthCheck {
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.named("my-readiness-check")
.up()
.withData("thread", Thread.currentThread().getName())
.withData("request", Arc.container().requestContext().isActive())
.build();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.smallrye.health.runtime;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;

public class RequestScopeHelper {

/**
* Activates the request scope is not yet activated.
*
* @return {@code true} if activated by this method, {@code false} if already activated.
*/
static boolean activeRequestScope() {
ManagedContext context = Arc.container().requestContext();
if (!context.isActive()) {
context.activate();
return true;
}
return false;
}

private RequestScopeHelper() {
// avoid direct instantiation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.enterprise.inject.spi.CDI;

import io.quarkus.arc.Arc;
import io.smallrye.health.SmallRyeHealth;
import io.smallrye.health.SmallRyeHealthReporter;
import io.vertx.core.Handler;
Expand All @@ -17,17 +18,24 @@ public class SmallRyeHealthHandler implements Handler<RoutingContext> {

@Override
public void handle(RoutingContext event) {
boolean activated = RequestScopeHelper.activeRequestScope();

SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getHealth();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getHealth();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
} finally {
if (activated) {
Arc.container().requestContext().terminate();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.enterprise.inject.spi.CDI;

import io.quarkus.arc.Arc;
import io.smallrye.health.SmallRyeHealth;
import io.smallrye.health.SmallRyeHealthReporter;
import io.vertx.core.Handler;
Expand All @@ -33,17 +34,24 @@ public class SmallRyeLivenessHandler implements Handler<RoutingContext> {

@Override
public void handle(RoutingContext event) {

SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getLiveness();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
boolean activated = RequestScopeHelper.activeRequestScope();

try {
SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getLiveness();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
} finally {
if (activated) {
Arc.container().requestContext().terminate();
}
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.enterprise.inject.spi.CDI;

import io.quarkus.arc.Arc;
import io.smallrye.health.SmallRyeHealth;
import io.smallrye.health.SmallRyeHealthReporter;
import io.vertx.core.Handler;
Expand All @@ -32,17 +33,24 @@ public class SmallRyeReadinessHandler implements Handler<RoutingContext> {

@Override
public void handle(RoutingContext event) {

SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getReadiness();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
boolean activated = RequestScopeHelper.activeRequestScope();

try {
SmallRyeHealthReporter reporter = CDI.current().select(SmallRyeHealthReporter.class).get();
SmallRyeHealth health = reporter.getReadiness();
HttpServerResponse resp = event.response();
if (health.isDown()) {
resp.setStatusCode(503);
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
} finally {
if (activated) {
Arc.container().requestContext().terminate();
}
}
resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

reporter.reportHealth(outputStream, health);
resp.end(Buffer.buffer(outputStream.toByteArray()));
}
}

0 comments on commit 235ed78

Please sign in to comment.