Skip to content

Commit

Permalink
Dev UI fix error in log on Hot Reload when properties change
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed May 31, 2023
1 parent 9aaa021 commit bb026b7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static io.quarkus.vertx.http.deployment.devmode.console.ConfigEditorProcessor.isSetByDevServices;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -13,8 +15,12 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ConfigDescriptionBuildItem;
import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devui.deployment.InternalPageBuildItem;
import io.quarkus.devui.runtime.config.ConfigJsonRPCService;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.devui.spi.page.Page;
import io.quarkus.vertx.http.deployment.devmode.console.ConfigEditorProcessor;
import io.quarkus.vertx.http.runtime.devmode.ConfigDescription;

/**
Expand Down Expand Up @@ -46,6 +52,16 @@ InternalPageBuildItem createConfigurationPages(List<ConfigDescriptionBuildItem>
return configurationPages;
}

@BuildStep(onlyIf = IsDevelopment.class)
JsonRPCProvidersBuildItem registerJsonRpcService() {
DevConsoleManager.register("config-update-property", map -> {
Map<String, String> values = Collections.singletonMap(map.get("name"), map.get("value"));
ConfigEditorProcessor.updateConfig(values, false);
return null;
});
return new JsonRPCProvidersBuildItem("devui-configuration", ConfigJsonRPCService.class);
}

private List<ConfigDescription> getAllConfig(List<ConfigDescriptionBuildItem> configDescriptionBuildItems,
Optional<DevServicesLauncherConfigResultBuildItem> devServicesLauncherConfig) {
List<ConfigDescription> configDescriptions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import io.quarkus.devconsole.runtime.spi.DevConsolePostHandler;
import io.quarkus.devconsole.spi.DevConsoleRouteBuildItem;
import io.quarkus.devconsole.spi.DevConsoleRuntimeTemplateInfoBuildItem;
import io.quarkus.devui.runtime.config.ConfigJsonRPCService;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.vertx.http.runtime.devmode.ConfigDescription;
import io.quarkus.vertx.http.runtime.devmode.ConfigDescriptionsManager;
import io.quarkus.vertx.http.runtime.devmode.ConfigDescriptionsRecorder;
Expand Down Expand Up @@ -154,16 +152,6 @@ void handleRequests(BuildProducer<DevConsoleRouteBuildItem> devConsoleRouteProdu
}));
}

@BuildStep(onlyIf = IsDevelopment.class)
JsonRPCProvidersBuildItem registerJsonRpcService() {
DevConsoleManager.register("config-update-property", map -> {
Map<String, String> values = Collections.singletonMap(map.get("name"), map.get("value"));
updateConfig(values);
return null;
});
return new JsonRPCProvidersBuildItem("devui-configuration", ConfigJsonRPCService.class);
}

private Map<String, String> filterAndApplyProfile(Map<String, String> autoconfig, List<String> configFilter,
String profile) {
return autoconfig.entrySet().stream()
Expand Down Expand Up @@ -221,6 +209,10 @@ static byte[] getConfig() {
}

public static void updateConfig(Map<String, String> values) {
updateConfig(values, true);
}

public static void updateConfig(Map<String, String> values, boolean preventKill) {
if (values != null && !values.isEmpty()) {
try {
Path configPath = getConfigPath();
Expand Down Expand Up @@ -255,14 +247,19 @@ public static void updateConfig(Map<String, String> values) {
writer.newLine();
}
}
preventKill();
if (preventKill)
preventKill();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}

static void setConfig(String value) {
setConfig(value, true);
}

static void setConfig(String value, boolean preventKill) {
try {
Path configPath = getConfigPath();
try (BufferedWriter writer = Files.newBufferedWriter(configPath)) {
Expand All @@ -272,7 +269,8 @@ static void setConfig(String value) {
writer.write(value);
}
}
preventKill();
if (preventKill)
preventKill();
} catch (Throwable t) {
throw new RuntimeException(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ export class JsonRpc {

if (messageType === MessageType.Void.toString()) { // Void response, typically used on initial subscription
// Do nothing
} else if (messageType === MessageType.HotReload.toString()){
connectionState.hotreload(JsonRpc.serverUri);
connectionState.connected(JsonRpc.serverUri)
} else if (messageType === MessageType.Response.toString()) { // Normal Request-Response
} else if (messageType === MessageType.HotReload.toString() || messageType === MessageType.Response.toString()) {
if (messageType === MessageType.HotReload.toString()){
connectionState.hotreload(JsonRpc.serverUri);
connectionState.connected(JsonRpc.serverUri);
}
if (JsonRpc.promiseQueue.has(response.id)) {
var saved = JsonRpc.promiseQueue.get(response.id);
var promise = saved.promise;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.devui.runtime.comms;

/**
* Allows JSON RPC methods to response with more finer grade message types
*
* @param <T> The type of the response object
*/
public class JsonRpcMessage<T> {
private T response;
private MessageType messageType;

public JsonRpcMessage() {
}

public JsonRpcMessage(T response, MessageType messageType) {
this.response = response;
this.messageType = messageType;
}

public T getResponse() {
return response;
}

public void setResponse(T response) {
this.response = response;
}

public MessageType getMessageType() {
return messageType;
}

public void setMessageType(MessageType messageType) {
this.messageType = messageType;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.devui.runtime.comms;

import static io.quarkus.devui.runtime.jsonrpc.JsonRpcKeys.MessageType;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -199,8 +197,14 @@ private void route(JsonRpcRequest jsonRpcRequest, ServerWebSocket s) {
}
uni.subscribe()
.with(item -> {
codec.writeResponse(s, jsonRpcRequest.getId(), item,
MessageType.Response);
if (JsonRpcMessage.class.isAssignableFrom(item.getClass())) {
JsonRpcMessage jsonRpcMessage = (JsonRpcMessage) item;
codec.writeResponse(s, jsonRpcRequest.getId(), jsonRpcMessage.getResponse(),
jsonRpcMessage.getMessageType());
} else {
codec.writeResponse(s, jsonRpcRequest.getId(), item,
MessageType.Response);
}
}, failure -> {
codec.writeErrorResponse(s, jsonRpcRequest.getId(), jsonRpcMethodName, failure);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.devui.runtime.comms;

public enum MessageType {
Void,
Response,
SubscriptionMessage,
HotReload
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.devui.runtime.comms.JsonRpcMessage;
import io.quarkus.devui.runtime.comms.MessageType;
import io.vertx.core.json.JsonObject;

@ApplicationScoped
public class ConfigJsonRPCService {

public boolean updateProperty(String name, String value) {
public JsonRpcMessage<Boolean> updateProperty(String name, String value) {
DevConsoleManager.invoke("config-update-property", Map.of("name", name, "value", value));
return true;
return new JsonRpcMessage(true, MessageType.HotReload);
}

public JsonObject getAllValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static io.quarkus.devui.runtime.jsonrpc.JsonRpcKeys.INTERNAL_ERROR;
import static io.quarkus.devui.runtime.jsonrpc.JsonRpcKeys.METHOD_NOT_FOUND;
import static io.quarkus.devui.runtime.jsonrpc.JsonRpcKeys.MessageType;

import io.quarkus.devui.runtime.comms.MessageType;
import io.quarkus.devui.runtime.jsonrpc.json.JsonMapper;
import io.vertx.core.http.ServerWebSocket;
import io.vertx.core.json.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,4 @@ public interface JsonRpcKeys {
public static final int INVALID_PARAMS = -32602; // Invalid params. Invalid method parameter(s).
public static final int INTERNAL_ERROR = -32603; // Internal error. Internal JSON-RPC error.

public static enum MessageType {
Void,
Response,
SubscriptionMessage,
HotReload
}
}

0 comments on commit bb026b7

Please sign in to comment.