Skip to content

Commit

Permalink
Fixes #4014 - Add support for --write-pid to Piranha Micro (#4018)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Sep 26, 2024
1 parent 7f1fa21 commit b073eba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion test/micro/helloworld/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<configuration>
<distribution>${piranha.distribution}</distribution>
<httpPort>${httpPort}</httpPort>
<startTimeout>30</startTimeout>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit b073eba

Please sign in to comment.