From dd85fa7871e376e0750a60d34b84d42bbc090a86 Mon Sep 17 00:00:00 2001 From: Maurizio Turatti Date: Wed, 20 Nov 2024 20:05:33 +0100 Subject: [PATCH] :bug: Refactor error handling in PolyglotDeployer to catch specific exceptions and improve deployment timeout management --- .../org/restheart/polyglot/PolyglotDeployer.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/polyglot/src/main/java/org/restheart/polyglot/PolyglotDeployer.java b/polyglot/src/main/java/org/restheart/polyglot/PolyglotDeployer.java index f94aeb24f..b18423e7e 100644 --- a/polyglot/src/main/java/org/restheart/polyglot/PolyglotDeployer.java +++ b/polyglot/src/main/java/org/restheart/polyglot/PolyglotDeployer.java @@ -34,6 +34,8 @@ import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import static org.fusesource.jansi.Ansi.Color.GREEN; @@ -64,7 +66,9 @@ import com.google.common.base.Objects; import com.google.common.collect.Lists; +import com.google.gson.JsonIOException; import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import com.mongodb.client.MongoClient; /** @@ -140,7 +144,7 @@ private void deployAll(Path pluginsDirectory) { var nodeServices = findNodeServices(pluginPath); var interceptors = findInterceptors(pluginPath); deploy(services, nodeServices, interceptors); - } catch (Throwable t) { + } catch (IOException | InterruptedException t) { LOGGER.error("Error deploying {}", pluginPath.toAbsolutePath(), t); } } @@ -340,7 +344,7 @@ private List findDeclaredPlugins(Path path, String prop, boolean checkPlug } else { return Lists.newArrayList(); } - } catch (Throwable t) { + } catch (JsonIOException | JsonSyntaxException | IOException t) { LOGGER.error("Error reading {}", packagePath, t); return Lists.newArrayList(); } @@ -401,11 +405,7 @@ private void deployNodeService(Path pluginPath) throws IOException { try { var srvf = NodeService.get(pluginPath, this.mclient, this.config); - while (!srvf.isDone()) { - Thread.sleep(300); - } - - var srv = srvf.get(); + var srv = srvf.get(30, TimeUnit.SECONDS); var record = new PluginRecord, ? extends ServiceResponse>>(srv.name(), "description", srv.secured(), true, srv.getClass().getName(), srv, new HashMap<>()); @@ -414,7 +414,7 @@ var record = new PluginRecord, ? extends Ser DEPLOYEES.put(pluginPath.toAbsolutePath(), srv); LOGGER.info(ansi().fg(GREEN).a("Service '{}' deployed at URI '{}' with description: '{}'. Secured: {}. Uri match policy: {}").reset().toString(), srv.name(), srv.uri(), srv.getDescription(), srv.secured(), srv.matchPolicy()); - } catch (IOException | InterruptedException | ExecutionException ex) { + } catch (IOException | InterruptedException | ExecutionException | TimeoutException ex) { LOGGER.error("Error deploying node service {}", pluginPath, ex); Thread.currentThread().interrupt(); executor.shutdownNow();