Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #10271 - Introduce jetty-pid.xml #10272

Merged
merged 23 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
adca81d
Issue #10271 - new start.jar --pid-file=<name> arg
joakime Aug 8, 2023
6133dd0
Fixing license header
joakime Aug 8, 2023
a0dbc2a
Changes from review
joakime Aug 8, 2023
8e7e10f
Changes from review
joakime Aug 8, 2023
22b527c
Merge remote-tracking branch 'origin/jetty-10.0.x' into fix/10.0.x/st…
joakime Aug 8, 2023
778bbb9
Changes from review
joakime Aug 8, 2023
4c91aed
More cleanup
joakime Aug 9, 2023
3b8f159
Ensure pidfile is truncated on write (so only 1 pid is present)
joakime Aug 9, 2023
394db41
Javadoc
joakime Aug 9, 2023
3712349
Changes from review
joakime Aug 14, 2023
029a537
Merge remote-tracking branch 'origin/jetty-10.0.x' into fix/10.0.x/st…
joakime Aug 14, 2023
443c9c9
Use ShutdownThread, not ShutdownMonitor.
joakime Aug 15, 2023
0323557
Commenting out the property in the ini-template
joakime Aug 21, 2023
b3368ae
Merge remote-tracking branch 'origin/jetty-10.0.x' into fix/10.0.x/st…
joakime Aug 21, 2023
565f55a
Fixing license header
joakime Aug 21, 2023
724dbb9
Changes from review
joakime Aug 22, 2023
f4258ec
Add DEBUG logging to show actual filename used in PidFile and StateLi…
joakime Aug 22, 2023
a8f9f6d
Deregister PidFile on stop
joakime Aug 22, 2023
453fa9a
Simplify PidFile and add distro tests for pid module
joakime Aug 23, 2023
e851ca7
Better PidFile management
joakime Aug 23, 2023
d88d617
state.mod depends on server as it configures the server.
joakime Aug 24, 2023
33f6418
Updates from review
joakime Aug 24, 2023
804c8e0
Merge remote-tracking branch 'origin/jetty-10.0.x' into fix/10.0.x/st…
joakime Aug 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions jetty-home/src/main/resources/bin/jetty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,10 @@ case "$ACTION" in
# 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\""
echo ${RUN_ARGS[*]} --pid-file=\"$JETTY_PID\" start-log-file=\"$JETTY_START_LOG\" | xargs ${JAVA} > /dev/null &
"
else
echo ${RUN_ARGS[*]} | xargs ${JAVA} > /dev/null &
disown $!
echo $! > "$JETTY_PID"
echo ${RUN_ARGS[*]} | xargs ${JAVA} --pid-file="$JETTY_PID" > /dev/null &
fi

fi
Expand Down
15 changes: 15 additions & 0 deletions jetty-home/src/main/resources/etc/jetty-pid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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.PidFileLifeCycleListener">
<Arg><Property name="jetty.pid.file" default="./jetty.pid"/></Arg>
</New>
</Arg>
</Call>
</Configure>
13 changes: 13 additions & 0 deletions jetty-start/src/main/java/org/eclipse/jetty/start/StartArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public class StartArgs

private boolean help = false;
private boolean stopCommand = false;
private String pidFile = null;
private List<String> listModules = null;
private List<String> showModules = null;
private boolean listClasspath = false;
Expand Down Expand Up @@ -919,6 +920,11 @@ public String getModuleGraphFilename()
return moduleGraphFilename;
}

public String getPidFile()
{
return pidFile;
}

public Props getProperties()
{
return properties;
Expand Down Expand Up @@ -1169,6 +1175,13 @@ public void parse(final String rawarg, String source)
return;
}

if (arg.startsWith("--pid-file="))
{
setProperty("jetty.pid.file", pidFile, source);
xmlRefs.add("etc/jetty-pid.xml");
return;
}

if (arg.startsWith("--download="))
{
addFile(null, Props.getValue(arg));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// ========================================================================
// 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.component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PidFileLifeCycleListener implements LifeCycle.Listener
{
private static final Logger LOG = LoggerFactory.getLogger(PidFileLifeCycleListener.class);

private final Path pidFile;

public PidFileLifeCycleListener(String filename)
{
pidFile = Paths.get(filename);
}

@Override
public void lifeCycleStarting(LifeCycle event)
{
long pid = ProcessHandle.current().pid();
try
{
Files.writeString(pidFile, Long.toString(pid), StandardCharsets.UTF_8);
}
catch (IOException e)
{
LOG.warn("Unable to create pidFile: {}", pidFile, e);
}
}

@Override
public void lifeCycleFailure(LifeCycle event, Throwable cause)
{
removePid();
}

@Override
public void lifeCycleStopped(LifeCycle event)
{
removePid();
}

private void removePid()
{
try
{
Files.deleteIfExists(pidFile);
}
catch (IOException e)
{
LOG.warn("Unable to remove pidFile: {}", pidFile, e);
}
}
}