Issue #7344 - Fix stop in process (#7639)

* Issue #7344 Fix stop goal for non-forked usage
This commit is contained in:
Jan Bartel 2022-02-24 16:30:44 +01:00 committed by GitHub
parent ec5f05fc10
commit 34c21ff413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 8 deletions

View File

@ -129,21 +129,32 @@ public class JettyStopMojo extends AbstractWebAppMojo
//wait for pid to stop
getLog().info("Waiting " + stopWait + " seconds for jetty " + pid + " to stop");
Optional<ProcessHandle> optional = ProcessHandle.of(pid);
final long remotePid = pid.longValue();
optional.ifPresentOrElse(p ->
{
try
{
send(stopKey + "\r\n" + command + "\r\n", 0);
CompletableFuture<ProcessHandle> future = p.onExit();
if (p.isAlive())
//if running in the same process, just send the stop
//command and wait for the response
if (ProcessHandle.current().pid() == remotePid)
{
p = future.get(stopWait, TimeUnit.SECONDS);
send(stopKey + "\r\n" + command + "\r\n", stopWait);
}
if (p.isAlive())
getLog().info("Couldn't verify server process stop");
else
getLog().info("Server process stopped");
{
//running forked, so wait for the process
send(stopKey + "\r\n" + command + "\r\n", 0);
CompletableFuture<ProcessHandle> future = p.onExit();
if (p.isAlive())
{
p = future.get(stopWait, TimeUnit.SECONDS);
}
if (p.isAlive())
getLog().info("Couldn't verify server process stop");
else
getLog().info("Server process stopped");
}
}
catch (ConnectException e)
{

View File

@ -22,6 +22,7 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.maven.plugin.AbstractWebAppMojo.DeploymentMode;
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.hamcrest.Matchers;
@ -209,6 +210,30 @@ public class TestJettyStopMojo
log.assertContains("Server reports itself as stopped");
}
@Test
public void testStopSameProcess() throws Exception
{
//test that if we need to stop a jetty in the same process as us
//we will wait for it to exit
String stopKey = "foo";
long thisPid = ProcessHandle.current().pid();
MockShutdownMonitorRunnable runnable = new MockShutdownMonitorRunnable();
runnable.setPidResponse(Long.toString(thisPid));
MockShutdownMonitor monitor = new MockShutdownMonitor(stopKey, runnable);
monitor.start();
TestLog log = new TestLog();
JettyStopMojo mojo = new JettyStopMojo();
mojo.stopWait = 5;
mojo.stopKey = stopKey;
mojo.stopPort = monitor.getPort();
mojo.setLog(log);
mojo.execute();
log.assertContains("Waiting 5 seconds for jetty " + Long.toString(thisPid) + " to stop");
}
@Test
public void testStopWait() throws Exception
{