Issue #10271 - new jetty-home `pid` module (#10272)

* Issue #10271 - new jetty-home module `pid`

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2023-08-25 09:12:49 -05:00 committed by GitHub
parent 38cea263cc
commit 900f50f513
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 280 additions and 22 deletions

View File

@ -330,7 +330,7 @@ CYGWIN*) JETTY_STATE="`cygpath -w $JETTY_STATE`";;
esac
JETTY_ARGS=(${JETTY_ARGS[*]} "jetty.state=$JETTY_STATE")
JETTY_ARGS=(${JETTY_ARGS[*]} "jetty.state=$JETTY_STATE" "jetty.pid=$JETTY_PID")
##################################################
# Get the list of config.xml files from jetty.conf
@ -457,6 +457,7 @@ case "$ACTION" in
exit
fi
# Startup from a service file
if [ $UID -eq 0 ] && type start-stop-daemon > /dev/null 2>&1
then
unset CH_USER
@ -482,6 +483,7 @@ case "$ACTION" in
exit 1
fi
# Startup if switching users (not as a service, or from root)
if [ -n "$JETTY_USER" ] && [ `whoami` != "$JETTY_USER" ]
then
unset SU_SHELL
@ -492,16 +494,14 @@ case "$ACTION" in
touch "$JETTY_PID"
chown "$JETTY_USER" "$JETTY_PID"
# FIXME: Broken solution: wordsplitting, pathname expansion, arbitrary command execution, etc.
su - "$JETTY_USER" $SU_SHELL -c "
cd \"$JETTY_BASE\"
echo ${RUN_ARGS[*]} start-log-file=\"$JETTY_START_LOG\" | xargs ${JAVA} > /dev/null &
disown \$!
echo \$! > \"$JETTY_PID\""
disown $(pgrep -P $!)"
else
# Startup if not switching users
echo ${RUN_ARGS[*]} | xargs ${JAVA} > /dev/null &
disown $!
echo $! > "$JETTY_PID"
disown $(pgrep -P $!)
fi
fi
@ -523,6 +523,7 @@ case "$ACTION" in
stop)
echo -n "Stopping Jetty: "
# Stop from a service file
if [ $UID -eq 0 ] && type start-stop-daemon > /dev/null 2>&1; then
start-stop-daemon -K -p"$JETTY_PID" -d"$JETTY_HOME" -a "$JAVA" -s HUP
@ -535,6 +536,7 @@ case "$ACTION" in
sleep 1
done
else
# Stop from a non-service path
if [ ! -f "$JETTY_PID" ] ; then
echo "ERROR: no pid found at $JETTY_PID"
exit 1

View File

@ -1,15 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_3.dtd">
<!-- =============================================================== -->
<!-- Mixin the Start FileNoticeLifeCycleListener -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.util.component.FileNoticeLifeCycleListener">
<Arg><Property name="jetty.state" default="./jetty.state"/></Arg>
</New>
</Arg>
</Call>
</Configure>

View File

@ -8,4 +8,4 @@
# Each line in this file becomes an argument to start.jar
# in addition to those found in the start.ini file
# =======================================================
jetty-started.xml
--module=pid,state

View File

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.server.StateLifeCycleListener">
<Arg><Property name="jetty.state" default="jetty.state"/></Arg>
</New>
</Arg>
</Call>
</Configure>

View File

@ -0,0 +1,17 @@
# DO NOT EDIT THIS FILE - See: https://eclipse.dev/jetty/documentation/
[description]
Creates and updates state file used by jetty.sh
[tags]
start
[depends]
server
[xml]
etc/jetty-state.xml
[ini-template]
## State file path
# jetty.state=${jetty.base}/jetty.state

View File

@ -0,0 +1,89 @@
//
// ========================================================================
// Copyright (c) 1995 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.server;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jetty.util.component.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;
/**
* A LifeCycle Listener that writes state changes to a file.
* <p>This can be used with the jetty.sh script to wait for successful startup.
*/
public class StateLifeCycleListener implements LifeCycle.Listener
{
private static final Logger LOG = LoggerFactory.getLogger(StateLifeCycleListener.class);
private final Path _filename;
public StateLifeCycleListener(String filename)
{
_filename = Paths.get(filename).toAbsolutePath();
if (LOG.isDebugEnabled())
LOG.debug("State File: {}", _filename);
}
private void writeState(String action, LifeCycle lifecycle)
{
try (Writer out = Files.newBufferedWriter(_filename, UTF_8, WRITE, CREATE, APPEND))
{
out.append(action).append(" ").append(lifecycle.toString()).append("\n");
}
catch (Exception e)
{
LOG.warn("Unable to write state", e);
}
}
@Override
public void lifeCycleStarting(LifeCycle event)
{
writeState("STARTING", event);
}
@Override
public void lifeCycleStarted(LifeCycle event)
{
writeState("STARTED", event);
}
@Override
public void lifeCycleFailure(LifeCycle event, Throwable cause)
{
writeState("FAILED", event);
}
@Override
public void lifeCycleStopping(LifeCycle event)
{
writeState("STOPPING", event);
}
@Override
public void lifeCycleStopped(LifeCycle event)
{
writeState("STOPPED", event);
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure>
<Call class="org.eclipse.jetty.util.PidFile" name="create">
<Arg name="file"><Property name="jetty.pid" default="jetty.pid"/></Arg>
</Call>
</Configure>

View File

@ -0,0 +1,19 @@
# DO NOT EDIT THIS FILE - See: https://eclipse.dev/jetty/documentation/
[description]
Creates the PID file for the Jetty process
[tags]
start
[before]
server
threadpool
jvm
[xml]
etc/jetty-pid.xml
[ini-template]
## PID file path
# jetty.pid=${jetty.base}/jetty.pid

View File

@ -0,0 +1,83 @@
//
// ========================================================================
// Copyright (c) 1995 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.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.util.annotation.Name;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
/**
* Create a PID file for the running process.
*
* <p>
* Will register itself in a {@link Runtime#addShutdownHook(Thread)}
* to cleanup the PID file it created during normal JVM shutdown.
* </p>
*/
public class PidFile extends Thread
{
private static final Logger LOG = LoggerFactory.getLogger(PidFile.class);
private static final Set<Path> activeFiles = ConcurrentHashMap.newKeySet();
public static void create(@Name("file") String filename) throws IOException
{
Path pidFile = Paths.get(filename).toAbsolutePath();
if (activeFiles.add(pidFile))
{
Runtime.getRuntime().addShutdownHook(new PidFile(pidFile));
if (Files.exists(pidFile))
LOG.info("Overwriting existing PID file: {}", pidFile);
// Create the PID file as soon as possible.
long pid = ProcessHandle.current().pid();
Files.writeString(pidFile, Long.toString(pid), UTF_8, CREATE, WRITE, TRUNCATE_EXISTING);
if (LOG.isDebugEnabled())
LOG.debug("PID file: {}", pidFile);
}
}
private final Path pidFile;
private PidFile(Path pidFile)
{
this.pidFile = pidFile;
}
@Override
public void run()
{
try
{
Files.deleteIfExists(pidFile);
}
catch (Throwable t)
{
LOG.info("Unable to remove PID file: {}", pidFile, t);
}
}
}

View File

@ -22,7 +22,9 @@ import org.slf4j.LoggerFactory;
/**
* A LifeCycle Listener that writes state changes to a file.
* <p>This can be used with the jetty.sh script to wait for successful startup.
* @deprecated use {@code org.eclipse.jetty.server.StateLifeCycleListener} instead
*/
@Deprecated
public class FileNoticeLifeCycleListener implements LifeCycle.Listener
{
private static final Logger LOG = LoggerFactory.getLogger(FileNoticeLifeCycleListener.class);

View File

@ -23,6 +23,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
@ -64,6 +65,7 @@ import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@ -107,6 +109,45 @@ public class DistributionTests extends AbstractJettyHomeTest
}
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testPidFile(boolean includeJettyPidConfig) throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
JettyHomeTester distribution = JettyHomeTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();
try (JettyHomeTester.Run run1 = distribution.start("--add-modules=http,pid"))
{
assertTrue(run1.awaitFor(10, TimeUnit.SECONDS));
assertEquals(0, run1.getExitValue());
Path pidfile = run1.getConfig().getJettyBase().resolve("jetty.pid");
int port = distribution.freePort();
String[] args = new String[includeJettyPidConfig ? 2 : 1];
args[0] = "jetty.http.port=" + port;
if (includeJettyPidConfig)
args[1] = "jetty.pid=" + pidfile;
try (JettyHomeTester.Run run2 = distribution.start(args))
{
assertTrue(run2.awaitConsoleLogsFor("Started Server@", 10, TimeUnit.SECONDS));
assertTrue(Files.isRegularFile(pidfile), "PID file should exist");
startHttpClient();
ContentResponse response = client.GET("http://localhost:" + port);
assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus());
}
await().atMost(Duration.ofSeconds(10)).until(() -> !Files.exists(pidfile));
}
}
@Test
public void testQuickStartGenerationAndRun() throws Exception
{