Skip to content

Commit

Permalink
Issue #7344 - Fix stop in process (#7639)
Browse files Browse the repository at this point in the history
* Issue #7344 Fix stop goal for non-forked usage
  • Loading branch information
janbartel committed Feb 24, 2022
1 parent ec5f05f commit 34c21ff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,32 @@ public void execute() throws MojoExecutionException, MojoFailureException
//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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -209,6 +210,30 @@ public void testStopWaitBadPid() throws Exception
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
{
Expand Down

0 comments on commit 34c21ff

Please sign in to comment.