Skip to content

Commit

Permalink
Issue #7344 Make plugin wait for forked jetty process to stop
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Jan 12, 2022
1 parent ff10c26 commit 42ce379
Show file tree
Hide file tree
Showing 7 changed files with 713 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -72,38 +75,125 @@ public void execute() throws MojoExecutionException, MojoFailureException

String command = "forcestop";

try (Socket s = new Socket(InetAddress.getByName("127.0.0.1"), stopPort);)
if (stopWait > 0)
{
OutputStream out = s.getOutputStream();
out.write((stopKey + "\r\n" + command + "\r\n").getBytes());
out.flush();

if (stopWait > 0)
//try to get the pid
Long pid = null;
try
{
s.setSoTimeout(stopWait * 1000);
s.getInputStream();
String response = send(stopKey + "\r\n" + "pid" + "\r\n", true, stopWait);
pid = Long.valueOf(response);
}
catch (NumberFormatException e)
{
getLog().info("Server returned bad pid");
}
catch (ConnectException e)
{
//jetty not running, no point continuing
getLog().info("Jetty not running!");
return;
}
catch (Exception e)
{
//jetty running, try to stop it regarless of error
getLog().error(e);
}

getLog().info("Waiting " + stopWait + " seconds for jetty to stop");
LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));
String response;
boolean stopped = false;
while (!stopped && ((response = lin.readLine()) != null))
//send the stop command
try
{
if (pid == null)
{
//no pid, so just wait until jetty reports itself stopped
getLog().info("Waiting " + stopWait + " seconds for jetty to stop");
String response = send(stopKey + "\r\n" + command + "\r\n", true, stopWait);

if ("Stopped".equals(response))
{
stopped = true;
getLog().info("Server reports itself as stopped");
else
getLog().info("Couldn't verify server as stopped, received " + response);
}
else
{
//wait for pid to stop
getLog().info("Waiting " + stopWait + " seconds for jetty " + pid + " to stop");
send(stopKey + "\r\n" + command + "\r\n", false, 0);
Optional<ProcessHandle> optional = ProcessHandle.of(pid);
ProcessHandle handle = optional.orElse(null);
if (handle != null)
{
try
{
CompletableFuture<ProcessHandle> future = handle.onExit();
if (handle.isAlive())
{
handle = future.get(stopWait, TimeUnit.SECONDS);
}

if (handle.isAlive())
getLog().info("Couldn't verify server process stop");
else
getLog().info("Server process stopped");
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
}
catch (ConnectException e)
{
getLog().info("Jetty not running!");
}
catch (Exception e)
{
getLog().error(e);
}
}
catch (ConnectException e)
else
{
getLog().info("Jetty not running!");
//send the stop command but don't wait to verify the stop
getLog().info("Stopping jetty");
try
{
send(stopKey + "\r\n" + command + "\r\n", false, 0);
}
catch (ConnectException e)
{
getLog().info("Jetty not running!");
}
catch (Exception e)
{
getLog().error(e);
}

}
catch (Exception e)
}

private String send(String command, boolean expectResponse, int wait)
throws Exception
{
String response = null;
try (Socket s = new Socket(InetAddress.getByName("127.0.0.1"), stopPort); OutputStream out = s.getOutputStream();)
{
getLog().error(e);
//send the command
out.write(command.getBytes());
out.flush();

if (expectResponse)
{
if (wait > 0)
s.setSoTimeout(wait * 1000);

try (LineNumberReader lin = new LineNumberReader(new InputStreamReader(s.getInputStream()));)
{
//read the response
response = lin.readLine();
}
}
return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.maven.plugin;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import org.eclipse.jetty.toolchain.test.IO;

public class ForkableTesterRunnable extends TesterRunnable
{
@Override
public void run()
{
try
{
while (true)
{
try (Socket socket = serverSocket.accept())
{
LineNumberReader reader = new LineNumberReader(new InputStreamReader(socket.getInputStream()));
String receivedKey = reader.readLine();
if (!key.equals(receivedKey))
{
continue;
}

String cmd = reader.readLine();
OutputStream out = socket.getOutputStream();

if ("stop".equalsIgnoreCase(cmd))
{
out.write((stopResponse + "\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
System.exit(0);
}
else if ("forcestop".equalsIgnoreCase(cmd))
{
out.write((forceStopResponse + "\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
System.exit(0);
}
else if ("stopexit".equalsIgnoreCase(cmd))
{
out.write((stopExitResponse + "\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
System.exit(0);
}
else if ("exit".equalsIgnoreCase(cmd))
{
out.write((exitResponse + "\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
System.exit(0);
}
else if ("status".equalsIgnoreCase(cmd))
{
out.write(("OK\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
}
else if ("pid".equalsIgnoreCase(cmd))
{
out.write((String.valueOf(ProcessHandle.current().pid()) + "\r\n").getBytes(StandardCharsets.UTF_8));
out.flush();
}
}
catch (Throwable x)
{
x.printStackTrace();
}
}
}
catch (Throwable x)
{
x.printStackTrace();
}
finally
{
IO.close(serverSocket);
}
}
}
Loading

0 comments on commit 42ce379

Please # to comment.