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

fix for TIKA-1570 contributed by michaelwda #324

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class TikaServerCli {
public static final String DEFAULT_HOST = "localhost";
public static final Set<String> LOG_LEVELS = new HashSet<>(Arrays.asList("debug", "info"));
private static final Logger LOG = LoggerFactory.getLogger(TikaServerCli.class);
static TikaServerWatchDog watchDog = null;

private static final String FILE_URL_WARNING =
"WARNING: You have chosen to run tika-server with fileUrl enabled.\n"+
Expand Down Expand Up @@ -124,6 +125,13 @@ private static Options getOptions() {
return options;
}

private static Options getStopOptions()
{
Options options = new Options();
options.addOption("preventSystemExit", false, "Prevent the stop method from calling system.exit, which will terminate the JVM. This is useful for integration tests.");
return options;
}

public static void main(String[] args) {
LOG.info("Starting {} server", new Tika());
try {
Expand All @@ -135,6 +143,28 @@ public static void main(String[] args) {
}
}

public static void stop(String [] args) {
// process service stop function
if(watchDog != null)
{
watchDog.close();
}
try{
Options options = getStopOptions();
CommandLineParser cliParser = new GnuParser();
CommandLine line = cliParser.parse(options, args);
if (line.hasOption("preventSystemExit")) {
return;
}
}
catch (org.apache.commons.cli.ParseException e){
e.printStackTrace();
LOG.error("Can't parse stop arguments: ", e);
System.exit(-1);
}
System.exit(0);
}

private static void execute(String[] args) throws Exception {
Options options = getOptions();

Expand All @@ -145,7 +175,7 @@ private static void execute(String[] args) throws Exception {
//and they won't be needed in legacy.
CommandLine line = cliParser.parse(options, stripChildArgs(args));
if (line.hasOption("spawnChild")) {
TikaServerWatchDog watchDog = new TikaServerWatchDog();
watchDog = new TikaServerWatchDog();
watchDog.execute(args, configureServerTimeouts(line));
} else {
if (! line.hasOption("child")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public void execute(String[] args, ServerTimeouts serverTimeouts) throws Excepti
}
}

public void close()
{
setChildStatus(CHILD_STATUS.SHUTTING_DOWN);
LOG.debug("about to shutdown");
if (childProcess != null) {
LOG.info("about to shutdown process");
childProcess.close();
}
}

private void startPingTimer(ServerTimeouts serverTimeouts) {
//if the child thread is in stop-the-world mode, and isn't
//reading the ping, this thread checks to make sure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,42 @@ public void run() {
}
}

@Test
public void testSystemExitViaStopMethod() throws Exception {

Thread serverThread = new Thread() {
@Override
public void run() {
TikaServerCli.main(
new String[]{
"-spawnChild",
"-p", INTEGRATION_TEST_PORT,
"-tmpFilePrefix", "tika-server-systemexitstopmethod"

});
}
//Add custom implementation of the destroy method
//This method was never implemented in the super class, and gives us an easy way to invoke our stop command.
//We pass in the preventSystemExit option to stop the call to System.Exit, which would terminate the JVM and cause a test failure.
@Override
public void destroy() {
TikaServerCli.stop(new String []{"-preventSystemExit"});
}
};
serverThread.start();
awaitServerStartup();
serverThread.destroy();

//give some time for the server to crash/kill itself
Thread.sleep(2000);

try {
testBaseline();
} finally {
serverThread.interrupt();
}
}

@Test
public void testTimeoutOk() throws Exception {
//test that there's enough time for this file.
Expand Down