Skip to content

Commit

Permalink
Merge pull request #20354 from michalszynkiewicz/optional-grpc-server…
Browse files Browse the repository at this point in the history
…-start

gRPC: add an option to not start server in dev mode
  • Loading branch information
geoand authored Sep 23, 2021
2 parents 103590b + 542152f commit 6f5f907
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ public class GrpcBuildTimeConfig {
@ConfigItem(name = "metrics.enabled")
public boolean metricsEnabled;

/**
* Configuration gRPC dev mode.
*/
@ConfigItem
public GrpcDevModeConfig devMode;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.grpc.deployment;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class GrpcDevModeConfig {

/**
* Start gRPC server in dev mode even if no gRPC services are implemented.
* By default set to `true` to ease incremental development of new services using dev mode.
*/
@ConfigItem(defaultValue = "true")
public boolean forceServerStart;
}
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,11 @@ private GrpcInterceptors gatherInterceptors(IndexView index) {
@BuildStep
@Record(value = ExecutionTime.RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder, GrpcConfiguration config,
ShutdownContextBuildItem shutdown, List<BindableServiceBuildItem> bindables,
ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder,
GrpcConfiguration config,
GrpcBuildTimeConfig buildTimeConfig,
ShutdownContextBuildItem shutdown,
List<BindableServiceBuildItem> bindables,
LaunchModeBuildItem launchModeBuildItem,
VertxBuildItem vertx) {

Expand All @@ -479,7 +482,8 @@ ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder, GrpcConfigur
}
}

if (!bindables.isEmpty() || LaunchMode.current() == LaunchMode.DEVELOPMENT) {
if (!bindables.isEmpty()
|| (LaunchMode.current() == LaunchMode.DEVELOPMENT && buildTimeConfig.devMode.forceServerStart)) {
recorder.initializeGrpcServer(vertx.getVertx(), config, shutdown, blocking, launchModeBuildItem.getLaunchMode());
return new ServiceStartBuildItem(GRPC_SERVER);
}
Expand Down Expand Up @@ -550,7 +554,7 @@ void configureMetrics(GrpcBuildTimeConfig configuration, Optional<MetricsCapabil

@BuildStep
BeanArchivePredicateBuildItem additionalBeanArchives() {
return new BeanArchivePredicateBuildItem(new Predicate<ApplicationArchive>() {
return new BeanArchivePredicateBuildItem(new Predicate<>() {

@Override
public boolean test(ApplicationArchive archive) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.grpc.server.devmode;

import static io.restassured.RestAssured.when;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.response.Response;

public class DevModeServerStartDisabledTest {
@RegisterExtension
public static final QuarkusDevModeTest config = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(IsUpEndpoint.class)
.add(new StringAsset("quarkus.grpc.dev-mode.force-server-start=false\n"), "application.properties"));

@Test
public void test() {
Response response = when().get("/grpc-status");
response.then().statusCode(204); // 200 is returned when server started, 204 otherwise
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.grpc.server.devmode;

import static io.restassured.RestAssured.when;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.response.Response;

public class DevModeServerStartTest {

@RegisterExtension
static final QuarkusDevModeTest config = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(IsUpEndpoint.class));

@Test
public void test() {
Response response = when().get("/grpc-status");
response.then().statusCode(200); // 200 is returned when server started, 204 otherwise
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.grpc.server.devmode;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import io.quarkus.grpc.runtime.GrpcServerRecorder;

@Path("/grpc-status")
@ApplicationScoped
public class IsUpEndpoint {
@GET
public Response isGrpcUp() {
return GrpcServerRecorder.getVerticleCount() > 0 ? Response.ok().build() : Response.noContent().build();
}

}

0 comments on commit 6f5f907

Please sign in to comment.