From 97f85b3d276b46361790a4af042354f21da669f4 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Tue, 27 Oct 2020 12:29:26 +1100 Subject: [PATCH] Improve pom.xml changes in remote dev mode Fixes #12497 --- .../vertx/core/runtime/VertxCoreRecorder.java | 6 ++++ .../devmode/HttpRemoteDevClient.java | 6 +++- .../vertx/http/runtime/VertxHttpRecorder.java | 11 ++++-- .../runtime/devmode/RemoteSyncHandler.java | 34 +++++++++++++++---- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/extensions/vertx-core/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java b/extensions/vertx-core/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java index db2d882dcfe36d..307d79263aec41 100644 --- a/extensions/vertx-core/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java +++ b/extensions/vertx-core/runtime/src/main/java/io/quarkus/vertx/core/runtime/VertxCoreRecorder.java @@ -9,6 +9,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -374,6 +375,11 @@ public Integer get() { }; } + public static Supplier recoverFailedStart(VertxConfiguration config) { + return vertx = new VertxSupplier(config, Collections.emptyList()); + + } + static class VertxSupplier implements Supplier { final VertxConfiguration config; final VertxOptionsCustomizer customizer; diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/devmode/HttpRemoteDevClient.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/devmode/HttpRemoteDevClient.java index b8ef26edd92d71..f92719f89bf02b 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/devmode/HttpRemoteDevClient.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/devmode/HttpRemoteDevClient.java @@ -65,6 +65,7 @@ private class Session implements Closeable, Runnable { private final Thread httpThread; private final String url; private final URL devUrl; + private final URL probeUrl; int errorCount; private Session(RemoteDevState initialState, @@ -74,6 +75,7 @@ private Session(RemoteDevState initialState, this.initialConnectFunction = initialConnectFunction; this.changeRequestFunction = changeRequestFunction; devUrl = new URL(HttpRemoteDevClient.this.url + RemoteSyncHandler.DEV); + probeUrl = new URL(HttpRemoteDevClient.this.url + RemoteSyncHandler.PROBE); url = HttpRemoteDevClient.this.url; httpThread = new Thread(this, "Remote dev client thread"); httpThread.start(); @@ -248,7 +250,9 @@ private String waitForRestart(RemoteDevState initialState, } while (System.currentTimeMillis() < timeout) { try { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + HttpURLConnection connection = (HttpURLConnection) probeUrl.openConnection(); + connection.setRequestMethod("POST"); + connection.addRequestProperty(HttpHeaders.CONTENT_TYPE.toString(), RemoteSyncHandler.APPLICATION_QUARKUS); IoUtil.readBytes(connection.getInputStream()); return doConnect(initialState, initialConnectFunction); } catch (IOException e) { diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java index 928dfac9a71f40..7f859ea199a70e 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java @@ -173,7 +173,7 @@ public static void startServerAfterFailedStart() { if (supplier == null) { VertxConfiguration vertxConfiguration = new VertxConfiguration(); ConfigInstantiator.handleObject(vertxConfiguration); - vertx = VertxCoreRecorder.initialize(vertxConfiguration, null); + vertx = VertxCoreRecorder.recoverFailedStart(vertxConfiguration).get(); } else { vertx = supplier.get(); } @@ -187,7 +187,14 @@ public static void startServerAfterFailedStart() { if (hotReplacementHandler != null) { router.route().order(Integer.MIN_VALUE).blockingHandler(hotReplacementHandler); } - rootHandler = router; + Handler root = router; + LiveReloadConfig liveReloadConfig = new LiveReloadConfig(); + ConfigInstantiator.handleObject(liveReloadConfig); + if (liveReloadConfig.password.isPresent() + && hotReplacementContext.getDevModeType() == DevModeType.REMOTE_SERVER_SIDE) { + root = remoteSyncHandler = new RemoteSyncHandler(liveReloadConfig.password.get(), root, hotReplacementContext); + } + rootHandler = root; //we can't really do doServerStart(vertx, buildConfig, config, LaunchMode.DEVELOPMENT, new Supplier() { diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/devmode/RemoteSyncHandler.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/devmode/RemoteSyncHandler.java index 28db2f20e4f6a8..62078c9df00629 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/devmode/RemoteSyncHandler.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/devmode/RemoteSyncHandler.java @@ -14,9 +14,11 @@ import io.netty.handler.codec.http.HttpHeaderNames; import io.quarkus.dev.spi.HotReplacementContext; import io.quarkus.dev.spi.RemoteDevState; -import io.quarkus.runtime.ExecutorRecorder; import io.quarkus.runtime.util.HashUtil; +import io.quarkus.vertx.core.runtime.VertxCoreRecorder; +import io.vertx.core.AsyncResult; import io.vertx.core.Handler; +import io.vertx.core.Promise; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerRequest; @@ -31,6 +33,7 @@ public class RemoteSyncHandler implements Handler { public static final String QUARKUS_SESSION_COUNT = "X-Quarkus-Count"; public static final String CONNECT = "/connect"; public static final String DEV = "/dev"; + public static final String PROBE = "/probe"; //used to check that the server is back up after restart final String password; final Handler next; @@ -77,10 +80,19 @@ public void handle(HttpServerRequest event) { final String type = event.headers().get(HttpHeaderNames.CONTENT_TYPE); if (APPLICATION_QUARKUS.equals(type)) { currentSessionTimeout = time + 60000; - ExecutorRecorder.getCurrent().execute(new Runnable() { + VertxCoreRecorder.getVertx().get().executeBlocking(new Handler>() { @Override - public void run() { - handleRequest(event); + public void handle(Promise promise) { + try { + handleRequest(event); + } finally { + promise.complete(); + } + } + }, false, new Handler>() { + @Override + public void handle(AsyncResult event) { + } }); return; @@ -98,6 +110,8 @@ private void handleRequest(HttpServerRequest event) { handleDev(event); } else if (event.path().equals(CONNECT)) { handleConnect(event); + } else if (event.path().equals(PROBE)) { + event.response().end(); } else { event.response().setStatusCode(404).end(); } @@ -114,9 +128,9 @@ public void handle(Buffer b) { if (checkSession(event, b.getBytes())) { return; } - ExecutorRecorder.getCurrent().execute(new Runnable() { + VertxCoreRecorder.getVertx().get().executeBlocking(new Handler>() { @Override - public void run() { + public void handle(Promise promise) { try { Throwable problem = (Throwable) new ObjectInputStream(new ByteArrayInputStream(b.getBytes())) .readObject(); @@ -144,10 +158,16 @@ public void run() { } catch (Exception e) { log.error("Connect failed", e); event.response().setStatusCode(500).end(); + } finally { + promise.complete(); } } - }); + }, new Handler>() { + @Override + public void handle(AsyncResult event) { + } + }); } }).exceptionHandler(new Handler() { @Override