From eb7f91a81971d71aee6e14bef79e11df5be5015c Mon Sep 17 00:00:00 2001 From: Thorsten Vitt Date: Tue, 4 Oct 2016 11:08:47 +0200 Subject: [PATCH] More debugging for phantomjs, cf. #17 --- pom.xml | 4 + .../gen/DiplomaticConversion.java | 73 +++++++++++++------ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 1e687d7..3f7c148 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,8 @@ utf-8 beta.faustedition.net:/var/www/dev 10 + false + true faust-dev @@ -261,6 +263,8 @@ -Dphantomjs.binary=${phantomjs.binary} -Dfaust.diplo.allowedFailures=${faust.diplo.allowedFailures} + -Dfaust.diplo.server=${faust.diplo.server} + -Dfaust.diplo.debug=${faust.diplo.debug} net.faustedition.gen.DiplomaticConversion diff --git a/src/main/java/net/faustedition/gen/DiplomaticConversion.java b/src/main/java/net/faustedition/gen/DiplomaticConversion.java index ac4f798..5a8a135 100644 --- a/src/main/java/net/faustedition/gen/DiplomaticConversion.java +++ b/src/main/java/net/faustedition/gen/DiplomaticConversion.java @@ -16,6 +16,7 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.Optional; +import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; @@ -25,6 +26,7 @@ import javax.xml.transform.TransformerException; import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.mycila.xmltool.XMLDoc; import com.mycila.xmltool.XMLDocumentException; @@ -41,6 +43,12 @@ public class DiplomaticConversion { public static Path target = Paths.get("target"); private static String serverURL; + private static boolean debugPhantomJS; + + private static boolean onlyWebServer; + + private static ImmutableList baseCmdLine; + public static class TranscriptPage { public final Document document; private final String page; @@ -93,8 +101,11 @@ public String toString() { public boolean buildSVGs() { logger.fine("Converting " + this); final ArrayList arguments = Lists.newArrayList( - System.getProperty("phantomjs.binary", "/usr/local/bin/phantomjs"), "rendersvgs.js", - serverURL, getJsonPath().toString(), + System.getProperty("phantomjs.binary", "/usr/local/bin/phantomjs"), + debugPhantomJS? "--debug=true" : "", + "rendersvgs.js", + serverURL, + getJsonPath().toString(), target.resolve("www").resolve("transcript").resolve("diplomatic").resolve(getPagePath("svg")).toString()); final Optional imageLinkPath = getImageLinkPath(); if (imageLinkPath.isPresent()) { @@ -105,6 +116,8 @@ serverURL, getJsonPath().toString(), } try { + if (debugPhantomJS) + logger.fine(() -> String.join(" ", arguments)); final Process renderProcess = new ProcessBuilder(arguments).redirectErrorStream(true).start(); final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new BufferedInputStream(renderProcess.getInputStream()))); @@ -153,34 +166,50 @@ public Stream transcripts() { } public static void main(final String[] args) throws IOException { + Properties properties = System.getProperties(); + onlyWebServer = Boolean.valueOf((String) properties.getOrDefault("faust.diplo.server", "false")); + debugPhantomJS = Boolean.valueOf((String) properties.getOrDefault("faust.diplo.debug", "false")); final SimpleWebServer webServer = new SimpleWebServer("localhost", 0, new File("svg_rendering/page"), true); webServer.start(60, true); try { serverURL = new URL("http", "localhost", webServer.getListeningPort(), "/transcript-generation.html").toString(); logger.info(MessageFormat.format("Web server runs on {0}", serverURL)); + baseCmdLine = ImmutableList.of( + System.getProperty("phantomjs.binary", "/usr/local/bin/phantomjs"), + debugPhantomJS? "--debug=true" : "", + "rendersvgs.js", + serverURL); + logger.info(() -> "PhantomJS command line: " + String.join(" ", baseCmdLine) + " [ ]"); - - - final Object[] failedConversions = getDocuments() - .flatMap(document -> document.transcripts()) - .parallel() - .map(page -> page.writeTranscriptJson()) - .filter(page -> page.buildSVGs()) - .filter(page -> page.buildSVGs()) - .filter(page -> page.buildSVGs()) - .toArray(); - - if (failedConversions.length > 0) { - logger.log(Level.SEVERE, MessageFormat.format("Conversion of the following {0} pages failed:\n {1}", - failedConversions.length, Joiner.on("\n ").join(failedConversions))); - int allowedFailures = Integer.parseUnsignedInt(System.getProperty("faust.diplo.allowedFailures", "0")); - if (failedConversions.length > allowedFailures) { - logger.log(Level.SEVERE, MessageFormat.format("These are more than the {0} tolerated failures.", allowedFailures)); - System.exit(1); + if (onlyWebServer) { + logger.info("Hit Ctrl+C to interrupt"); + while (true) + Thread.sleep(60^000); } else { - logger.log(Level.INFO, MessageFormat.format("Up to {0} failures are tolerated.", allowedFailures)); + + final Object[] failedConversions = getDocuments() + .flatMap(document -> document.transcripts()) + .parallel() + .map(page -> page.writeTranscriptJson()) + .filter(page -> page.buildSVGs()) + .filter(page -> page.buildSVGs()) + .filter(page -> page.buildSVGs()) + .toArray(); + + if (failedConversions.length > 0) { + logger.log(Level.SEVERE, MessageFormat.format("Conversion of the following {0} pages failed:\n {1}", + failedConversions.length, Joiner.on("\n ").join(failedConversions))); + int allowedFailures = Integer.parseUnsignedInt(System.getProperty("faust.diplo.allowedFailures", "0")); + if (failedConversions.length > allowedFailures) { + logger.log(Level.SEVERE, MessageFormat.format("These are more than the {0} tolerated failures.", allowedFailures)); + System.exit(1); + } else { + logger.log(Level.INFO, MessageFormat.format("Up to {0} failures are tolerated.", allowedFailures)); + } + } } - } + } catch (InterruptedException e) { + logger.log(Level.INFO, "Interrupted.", e); } finally { webServer.stop(); }