X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.maps.server%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fmaps%2Fserver%2FTileserverMapnik.java;fp=org.simantics.maps.server%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fmaps%2Fserver%2FTileserverMapnik.java;h=dbe525725dff6d6bc3fa9ef456b3ab8a023011bd;hb=7ab15b0d6830bf2f60116c435b22766139ba8f37;hp=b047626a53253c82561992c71f2a39855460f889;hpb=fab0b23a6127c7d7081556d59dcf1080d043dd90;p=simantics%2Fdistrict.git diff --git a/org.simantics.maps.server/src/org/simantics/district/maps/server/TileserverMapnik.java b/org.simantics.maps.server/src/org/simantics/district/maps/server/TileserverMapnik.java index b047626a..dbe52572 100644 --- a/org.simantics.maps.server/src/org/simantics/district/maps/server/TileserverMapnik.java +++ b/org.simantics.maps.server/src/org/simantics/district/maps/server/TileserverMapnik.java @@ -7,11 +7,13 @@ import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Stream; @@ -54,7 +56,7 @@ public class TileserverMapnik { return running.get(); } - public void start() throws Exception { + public void start(Optional listener) throws Exception { // check if existing server is left hanging if (Files.exists(getPid())) { String pid = new String(Files.readAllBytes(getPid())); @@ -63,10 +65,10 @@ public class TileserverMapnik { } // check that npm dependencies are satisfied - if (checkAndInstall(null)) { - checkAndInstall(ADDITIONAL_DEPENDENCIES[0]); - checkAndInstall(ADDITIONAL_DEPENDENCIES[1]); - } +// if (checkAndInstall(null, listener)) { +// checkAndInstall(ADDITIONAL_DEPENDENCIES[0], listener); +// checkAndInstall(ADDITIONAL_DEPENDENCIES[1], listener); +// } checkConfigJson(); checkTm2Styles(); @@ -74,8 +76,9 @@ public class TileserverMapnik { if (process != null && process.isAlive()) return; - StartedProcess startedProcess = new ProcessExecutor().directory(serverRoot.resolve("tileserver-mapnik").toFile()).destroyOnExit().environment(getEnv()) - .command(NodeJS.executable().toString(), getTessera().toString(), "-c", getConfigJson().toString(), "-p", Integer.toString(MapsServerPreferences.defaultPort())) + Path tileliveTesseraWin = serverRoot.resolve("dist").resolve("node.exe").normalize().toAbsolutePath(); + StartedProcess startedProcess = new ProcessExecutor().directory(serverRoot.resolve("dist").toFile()).destroyOnExit().environment(getEnv()) + .command(tileliveTesseraWin.toString(), "-c", getConfigJson().toString(), "-p", Integer.toString(MapsServerPreferences.defaultPort())) .redirectOutput(new Slf4jDebugOutputStream(LOGGER) { @Override @@ -89,8 +92,10 @@ public class TileserverMapnik { Process nativeProcess = startedProcess.getProcess(); process = Processes.newStandardProcess(nativeProcess); int pid = PidUtil.getPid(nativeProcess); + LOGGER.info("Writing pid-file to {} with pid={}", getPid(), pid); Files.write(getPid(), (pid + "").getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); running.set(true); + listener.ifPresent(TileserverStartListener::started); } private Map getEnv() { @@ -119,43 +124,53 @@ public class TileserverMapnik { return serverRoot.resolve("pid"); } - public void restart() throws Exception { + public void restart(Optional listener) throws Exception { stop(); - start(); - } - - private boolean checkIfInstalled(String module) throws Exception { - String tileserverMapnik = tileserverMapnikRoot().toString(); - int retVal; - try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { - if (module == null) { - retVal = NodeJS.npm(output, "--prefix", tileserverMapnik, "list"); - } else { - retVal = NodeJS.npm(output, "--prefix", tileserverMapnik, "list", module); - } - String outputString = new String(output.toByteArray(), StandardCharsets.UTF_8); - } - return retVal == 0; - } - - private boolean install(String module) throws Exception { - String tileserverMapnik = tileserverMapnikRoot().toString(); - int retVal; - if (module == null) - retVal = NodeJS.npm(null, "--prefix", tileserverMapnik, "install", "--save"); - else - retVal = NodeJS.npm(null, "--prefix", tileserverMapnik, "install", module, "--save"); - if (retVal != 0) - LOGGER.warn("Could not install module " + module == null ? "package.json" : module + "! " + retVal); - return retVal == 0; + start(listener); } - private boolean checkAndInstall(String module) throws Exception { - boolean installed = checkIfInstalled(module); - if (!installed) - install(module); - return !installed; - } +// private boolean checkIfInstalled(String module) throws Exception { +// String tileserverMapnik = tileserverMapnikRoot().toString(); +// int retVal; +// try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { +// if (module == null) { +// retVal = NodeJS.npm(output, "--prefix", tileserverMapnik, "list"); +// } else { +// retVal = NodeJS.npm(output, "--prefix", tileserverMapnik, "list", module); +// } +// String outputString = new String(output.toByteArray(), StandardCharsets.UTF_8); +// } +// return retVal == 0; +// } +// +// private int install(String module, Optional listener) throws Exception { +// String tileserverMapnik = tileserverMapnikRoot().toString(); +// int retVal; +// if (module == null) { +// listener.ifPresent(l -> l.installing("Installing tileserver-mapnik")); +// retVal = NodeJS.npm(null, "--prefix", tileserverMapnik, "install", "--save"); +// } else { +// retVal = NodeJS.npm(null, "--prefix", tileserverMapnik, "install", module, "--save"); +// } +// if (retVal != 0) +// LOGGER.warn("Could not install module " + module == null ? "package.json" : module + "! " + retVal); +// return retVal; +// } +// +// private boolean checkAndInstall(String module, Optional listener) throws Exception { +// LOGGER.info("Installing module {}", String.valueOf(module)); +// boolean installed = checkIfInstalled(module); +// if (!installed) { +// int installSuccessfull = install(module, listener); +// if (installSuccessfull != 0) { +// LOGGER.warn("Installation of module {} failed!", String.valueOf(module)); +// listener.ifPresent(l -> l.installationFailed(module, installSuccessfull)); +// } +// } else { +// LOGGER.info("Module {} was already installed", String.valueOf(module)); +// } +// return !installed; +// } private Path tileserverMapnikRoot() { @@ -167,7 +182,7 @@ public class TileserverMapnik { } private Path getICU() { - return serverRoot.resolve("tileserver-mapnik/node_modules/tilelive-vector/node_modules/mapnik/lib/binding/node-v46-win32-x64/share/icu").toAbsolutePath(); + return serverRoot.resolve("dist/share/icu").toAbsolutePath(); } private Path getTessera() { @@ -269,4 +284,12 @@ public class TileserverMapnik { return results; } + public static interface TileserverStartListener { + + void installing(String module); + + void installationFailed(String module, int returnValue); + + void started(); + } }