Skip to content

Commit

Permalink
issue #101 - fail fast if after starting an office process its pid ca…
Browse files Browse the repository at this point in the history
…nnot be found
  • Loading branch information
[email protected] committed Jan 1, 2012
1 parent 6bf6527 commit e43ef41
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//
package org.artofsolving.jodconverter.office;

import static org.artofsolving.jodconverter.process.ProcessManager.PID_NOT_FOUND;
import static org.artofsolving.jodconverter.process.ProcessManager.PID_UNKNOWN;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -63,7 +63,7 @@ public void start() throws IOException {
public void start(boolean restart) throws IOException {
ProcessQuery processQuery = new ProcessQuery("soffice.bin", unoUrl.getAcceptString());
long existingPid = processManager.findPid(processQuery);
if (existingPid != PID_UNKNOWN) {
if (!(existingPid == PID_NOT_FOUND || existingPid == PID_UNKNOWN)) {
throw new IllegalStateException(String.format("a process with acceptString '%s' is already running; pid %d",
unoUrl.getAcceptString(), existingPid));
}
Expand Down Expand Up @@ -92,6 +92,10 @@ public void start(boolean restart) throws IOException {
logger.info(String.format("starting process with acceptString '%s' and profileDir '%s'", unoUrl, instanceProfileDir));
process = processBuilder.start();
pid = processManager.findPid(processQuery);
if (pid == PID_NOT_FOUND) {
throw new IllegalStateException(String.format("process with acceptString '%s' started but its pid could not be found",
unoUrl.getAcceptString()));
}
logger.info("started process" + (pid != PID_UNKNOWN ? "; pid = " + pid : ""));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public long findPid(ProcessQuery query) throws IOException {
}
}
}
return PID_UNKNOWN;
return PID_NOT_FOUND;
}

public void kill(Process process, long pid) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

public interface ProcessManager {

public static final long PID_NOT_FOUND = -2;
public static final long PID_UNKNOWN = -1;

void kill(Process process, long pid) throws IOException;

/**
* @param query
* @return the pid or {@link #PID_UNKNOWN}
* @return the pid if found, {@link #PID_NOT_FOUND} if not,
* or {@link #PID_UNKNOWN} if this implementation is unable to find out
* @throws IOException
*/
long findPid(ProcessQuery query) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public long findPid(ProcessQuery query) throws IOException {
return pids[i];
}
}
return PID_UNKNOWN;
return PID_NOT_FOUND;
} catch (SigarException sigarException) {
throw new IOException("findPid failed", sigarException);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void linuxProcessManager() throws Exception {
ProcessQuery query = new ProcessQuery("sleep", "5s");

long pid = processManager.findPid(query);
assertFalse(pid == ProcessManager.PID_UNKNOWN);
assertFalse(pid == ProcessManager.PID_NOT_FOUND);
Integer javaPid = (Integer) ReflectionUtils.getPrivateField(process, "pid");
assertEquals(pid, javaPid.longValue());

processManager.kill(process, pid);
assertEquals(processManager.findPid(query), ProcessManager.PID_UNKNOWN);
assertEquals(processManager.findPid(query), ProcessManager.PID_NOT_FOUND);
}

public void sigarProcessManager() throws Exception {
Expand All @@ -53,14 +53,14 @@ public void sigarProcessManager() throws Exception {
ProcessQuery query = new ProcessQuery("sleep", "5s");

long pid = processManager.findPid(query);
assertFalse(pid == ProcessManager.PID_UNKNOWN);
assertFalse(pid == ProcessManager.PID_NOT_FOUND);
if (PlatformUtils.isLinux()) {
Integer javaPid = (Integer) ReflectionUtils.getPrivateField(process, "pid");
assertEquals(pid, javaPid.longValue());
}

processManager.kill(process, pid);
assertEquals(processManager.findPid(query), ProcessManager.PID_UNKNOWN);
assertEquals(processManager.findPid(query), ProcessManager.PID_NOT_FOUND);
}

}

0 comments on commit e43ef41

Please sign in to comment.