Skip to content

Commit

Permalink
fixed server ping of startFuseki
Browse files Browse the repository at this point in the history
  • Loading branch information
melaasar committed Apr 21, 2022
1 parent e00446d commit 4b240ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
subprojects {
group = 'io.opencaesar.owl'
version = '1.0.3'
version = '1.0.4'

ext.versions = [
owl: '5.1.17',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public abstract class StartFusekiTask extends DefaultTask {
@Input
public abstract Property<Boolean> getWebUI();

@Optional
@Input
public abstract Property<Integer> getMaxPings();

@Optional
@Input
public abstract Property<Boolean> getDebug();
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion owl-fuseki/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
43 changes: 25 additions & 18 deletions owl-fuseki/src/main/java/io/opencaesar/owl/fuseki/FusekiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4b240ec

Please sign in to comment.