From 4b240ec001cefe9756c214ac4a31d4ca3e1d22ad Mon Sep 17 00:00:00 2001 From: Maged Elaasar Date: Thu, 21 Apr 2022 13:16:44 -0700 Subject: [PATCH] fixed server ping of startFuseki --- build.gradle | 2 +- .../owl/fuseki/StartFusekiTask.java | 13 +++++- owl-fuseki/README.md | 4 +- .../io/opencaesar/owl/fuseki/FusekiApp.java | 43 +++++++++++-------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index edba711f..906f8020 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ subprojects { group = 'io.opencaesar.owl' - version = '1.0.3' + version = '1.0.4' ext.versions = [ owl: '5.1.17', diff --git a/owl-fuseki-gradle/src/main/java/io/opencaesar/owl/fuseki/StartFusekiTask.java b/owl-fuseki-gradle/src/main/java/io/opencaesar/owl/fuseki/StartFusekiTask.java index 72ce4b8f..c0274d4d 100644 --- a/owl-fuseki-gradle/src/main/java/io/opencaesar/owl/fuseki/StartFusekiTask.java +++ b/owl-fuseki-gradle/src/main/java/io/opencaesar/owl/fuseki/StartFusekiTask.java @@ -30,6 +30,10 @@ public abstract class StartFusekiTask extends DefaultTask { @Input public abstract Property getWebUI(); + @Optional + @Input + public abstract Property getMaxPings(); + @Optional @Input public abstract Property getDebug(); @@ -66,8 +70,13 @@ public void run() { args.add("-o"); args.add(getOutputFolderPath().get().getAsFile().getAbsolutePath()); } - if (getWebUI().isPresent() && getWebUI().get()) { - args.add("-ui"); + if (getWebUI().isPresent()) { + if (getWebUI().get()) + args.add("-ui"); + } + if (getMaxPings().isPresent()) { + args.add("-p"); + args.add(getMaxPings().get().toString()); } if (getDebug().isPresent() && getDebug().get()) { args.add("-d"); diff --git a/owl-fuseki/README.md b/owl-fuseki/README.md index 763497ba..328b74c1 100644 --- a/owl-fuseki/README.md +++ b/owl-fuseki/README.md @@ -19,6 +19,7 @@ Args: --configurationPath | -g path/to/.fuseki.ttl [Required] --outputFolderPath | -i path/to/output/folder [Required] --webui | -ui [Optional] +--max-pings | -p [Optional] ``` ## Run as Gradle Task @@ -38,7 +39,8 @@ buildscript { task startFuseki(type: io.opencaesar.owl.fuseki.StartFusekiTask) { configurationPath = file('path/to/.fuseki.ttl') outputFolderPath = file('path/to/output/folder') // with webui, there must be a 'webapp' subfolder for the Fuseki UI - webui = true + webui = true // optional + maxPings = 10 // optional } task stopFuseki(type: io.opencaesar.owl.fuseki.StopFusekiTask) { outputFolderPath = file('path/to/output/folder') diff --git a/owl-fuseki/src/main/java/io/opencaesar/owl/fuseki/FusekiApp.java b/owl-fuseki/src/main/java/io/opencaesar/owl/fuseki/FusekiApp.java index 112f5a75..71478dee 100644 --- a/owl-fuseki/src/main/java/io/opencaesar/owl/fuseki/FusekiApp.java +++ b/owl-fuseki/src/main/java/io/opencaesar/owl/fuseki/FusekiApp.java @@ -70,17 +70,24 @@ enum Command { order = 4) private boolean webui; + @Parameter( + names = {"--max-pings", "-p"}, + description = "Maximum number (10 by default) of pings to the server before giving up", + help = true, + order = 5) + private int maxPings = 10; + @Parameter( names = {"--debug", "-d"}, description = "Shows debug logging statements", - order = 5) + order = 6) private boolean debug; @Parameter( names = {"--help", "-h"}, description = "Displays summary of options", help = true, - order = 6) + order = 7) private boolean help; private final static Logger LOGGER = Logger.getLogger(FusekiApp.class); @@ -128,9 +135,9 @@ private void run(FusekiApp app) throws Exception { } } LOGGER.info("fusekiWarPomURL="+fusekiWarPomURL); - startFuseki(new File(configurationPath), new File(outputFolderPath), "org.apache.jena.fuseki.cmd.FusekiCmd", app, "--localhost"); + startFuseki(new File(configurationPath), new File(outputFolderPath), app.maxPings, "org.apache.jena.fuseki.cmd.FusekiCmd", app, "--localhost"); } else { - startFuseki(new File(configurationPath), new File(outputFolderPath), "org.apache.jena.fuseki.main.cmds.FusekiMainCmd", app); + startFuseki(new File(configurationPath), new File(outputFolderPath), app.maxPings, "org.apache.jena.fuseki.main.cmds.FusekiMainCmd", app); } } else { stopFuseki(new File(outputFolderPath)); @@ -177,13 +184,14 @@ public static void unzip(InputStream source, File target) throws IOException { * @param fusekiDir Path to an output directory that, if it exists, will be cleaned, and that will have: * - fuseki.log the combination of standard output and error. * - fuseki.pid the ID of the fuseki process. + * @param maxPings The maximum number of pings to try before giving up * @param clazz Qualified name of the Fuseki server application (with or without Web UI) * @param app Fuseki application * @param argv Additional arguments * @throws IOException if the 'fuseki.pid' file could not be written to * @throws URISyntaxException If there is a problem retrieving the location of the fuseki jar. */ - public static void startFuseki(File config, File fusekiDir, String clazz, FusekiApp app, String... argv) throws IOException, URISyntaxException { + public static void startFuseki(File config, File fusekiDir, int maxPings, String clazz, FusekiApp app, String... argv) throws IOException, URISyntaxException { Path output = fusekiDir.toPath(); File pidFile = output.resolve(PID_FILENAME).toFile(); File logFile = output.resolve(LOG_FILENAME).toFile(); @@ -230,25 +238,29 @@ public static void startFuseki(File config, File fusekiDir, String clazz, Fuseki // Check that the newly started server is alive if (!p.isAlive()) { - throw new RuntimeException("Fuseki server has failed to start and returned exit code: " + p.exitValue() + ". See "+logFile+" for more details."); + throw new RuntimeException("Fuseki server has failed to start with exit code: " + p.exitValue() + ". See "+logFile+" for more details."); } else { // Ping the server a max number of times until it responds - int maxAttempts = 3; - int attempt = 0; - while (++attempt<=maxAttempts) { + int ping = 0; + while (++ping<=maxPings) { if (pingServer()) { - System.out.print("Fuseki server has started with pid="+p.pid()); + System.out.print("Fuseki server has now successfully started with pid="+p.pid()); break; } + try { + Thread.sleep(2000); // wait before the next ping + } catch (InterruptedException e) { + // do nothing + } } // if the server has failed to respond, kill it - if (attempt > maxAttempts) { + if (ping > maxPings) { try { p.destroyForcibly().waitFor(); } catch (InterruptedException e) { // do nothing } - throw new IllegalArgumentException("Fuseki server has failed to respond to ping after "+maxAttempts+" attempts and now been killed"); + throw new IllegalArgumentException("Fuseki server has failed to respond after "+maxPings+" pings and now been killed"); } } @@ -289,14 +301,9 @@ private static boolean pingServer() throws IOException { int responseCode = HttpURLConnection.HTTP_NOT_FOUND; try { con.setRequestMethod("GET"); - con.setConnectTimeout(5000); - con.setReadTimeout(5000); responseCode = con.getResponseCode(); - if (responseCode != HttpURLConnection.HTTP_OK) { - LOGGER.error("Fuseki server has failed to respond to ping (" + responseCode + " - " + con.getResponseMessage() + ")"); - } } catch (Exception e) { - LOGGER.error(e); + LOGGER.error("Fuseki server has not yet responded to ping"); } finally { con.disconnect(); }