From d35303d51b8ee8bc64fdc17dac70aeb5780b58c0 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Thu, 26 Sep 2024 16:04:26 -0500 Subject: [PATCH] Fixes #4014 - Add support for --write-pid to Piranha Micro (#4018) --- .../piranha/dist/micro/MicroBootstrap.java | 20 ++++++++++++++++ .../cloud/piranha/maven/plugin/StopMojo.java | 23 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/dist/micro/src/main/java/cloud/piranha/dist/micro/MicroBootstrap.java b/dist/micro/src/main/java/cloud/piranha/dist/micro/MicroBootstrap.java index cf556de6b2..2bfd3afb0f 100644 --- a/dist/micro/src/main/java/cloud/piranha/dist/micro/MicroBootstrap.java +++ b/dist/micro/src/main/java/cloud/piranha/dist/micro/MicroBootstrap.java @@ -37,6 +37,10 @@ import org.jboss.shrinkwrap.api.spec.WebArchive; import cloud.piranha.micro.loader.MicroConfiguration; import cloud.piranha.micro.loader.MicroOuterDeployer; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import static java.lang.System.Logger.Level.WARNING; /** * The micro version of Piranha. @@ -129,6 +133,9 @@ public void configure(String[] arguments) { if (arguments[i].equals("--ssl")) { System.setProperty("piranha.http.ssl", "true"); } + if (arguments[i].equals("--write-pid")) { + configuration.setBoolean("writePid", Boolean.TRUE); + } } } @@ -153,6 +160,19 @@ public void run() { outerDeployer = new MicroOuterDeployer(microConfiguration.postConstruct()); outerDeployer.deploy(archive); + + if (configuration.getBoolean("writePid", false)) { + File pidFile = new File("tmp", "piranha.pid"); + if (!pidFile.getParentFile().exists() && !pidFile.getParentFile().mkdirs()) { + LOGGER.log(WARNING, "Unable to create tmp directory for PID file"); + } + try (PrintWriter writer = new PrintWriter(new FileWriter(pidFile))) { + writer.print(ProcessHandle.current().pid()); + writer.flush(); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Unable to write PID file", ioe); + } + } } /** diff --git a/maven/plugin/src/main/java/cloud/piranha/maven/plugin/StopMojo.java b/maven/plugin/src/main/java/cloud/piranha/maven/plugin/StopMojo.java index b659f1524f..4da6dc3fa2 100644 --- a/maven/plugin/src/main/java/cloud/piranha/maven/plugin/StopMojo.java +++ b/maven/plugin/src/main/java/cloud/piranha/maven/plugin/StopMojo.java @@ -55,7 +55,7 @@ public class StopMojo extends AbstractMojo { /** * Stores the skip property. */ - @Parameter(defaultValue= "false", property="piranha.skip") + @Parameter(defaultValue = "false", property = "piranha.skip") private boolean skip; /** @@ -68,6 +68,15 @@ public StopMojo() { public void execute() throws MojoExecutionException { if (!skip) { try { + /* + * Get the PID from the PID file. + */ + String pid = Files.readString((new File( + runtimeDirectory, "tmp/piranha.pid").toPath())); + + /* + * Delete the PID file. + */ if (!Files.deleteIfExists(new File( runtimeDirectory, "tmp/piranha.pid").toPath())) { try { @@ -80,6 +89,18 @@ public void execute() throws MojoExecutionException { System.err.println("Unable to delete PID file"); } } + + if (!pid.trim().equals("")) { + /* + * If the process is still active destroy it forcibly. + */ + ProcessHandle.of(Long.parseLong(pid.trim())).ifPresent(p -> { + if (p.isAlive()) { + System.err.println("Process still alive, destroying forcibly"); + p.destroyForcibly(); + } + }); + } } catch (IOException ioe) { throw new MojoExecutionException(ioe); }