Class ProcessUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longstatic final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckCommand(String[] checkCmd, int... errorValue) Checks to see if the command can be run.static booleancheckCommand(String checkCmd, int... errorValue) Checks to see if the command can be run.static booleancheckCommandWithTimeout(String[] checkCmd, long timeoutMillis, int... errorValue) Same ascheckCommand(String[], int...), but with a caller-specified timeout instead of the 5000Lms default.static StringescapeCommandLine(String arg) This should correctly put double-quotes around an argument if ProcessBuilder doesn't seem to work (as it doesn't on paths with spaces on Windows)static FileProcessResultexecute(ProcessBuilder pb, long requestedTimeoutMillis, int maxStdoutBuffer, int maxStdErrBuffer) This writes stdout and stderr to the FileProcessResult.static FileProcessResultexecute(ProcessBuilder pb, long requestedTimeoutMillis, Path stdoutRedirect, int maxStdErrBuffer) This redirects stdout to stdoutRedirect path.static FileProcessResultexecute(ProcessBuilder pb, ParseContext context, long requestedTimeoutMillis, int maxStdoutBuffer, int maxStdErrBuffer) Same asexecute(ProcessBuilder, long, int, int), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())(seeParseTimeout.budgetFor(long)) so no single call can outlast the task's total timeout regardless of its own configuration.static FileProcessResultexecute(ProcessBuilder pb, ParseContext context, long requestedTimeoutMillis, Path stdoutRedirect, int maxStdErrBuffer) Same asexecute(ProcessBuilder, long, Path, int), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())and checkpoints while waiting -- seeexecute(ProcessBuilder, ParseContext, long, int, int).static Stringstatic booleanwaitForWithHeartbeat(Process p, ParseContext context, long timeoutMillis) Waits for the process to exit, likeProcess.waitFor(long, TimeUnit), but polls in 1000L ms increments and checkpointscontext'sParseTimeoutafter each increment that doesn't complete -- this is what lets a bounded external call run longer than the progress timeout without tripping the stall detector: the wait itself is progress.
-
Field Details
-
HEARTBEAT_INTERVAL_MILLIS
public static final long HEARTBEAT_INTERVAL_MILLIS- See Also:
-
DEFAULT_CHECK_COMMAND_TIMEOUT_MILLIS
public static final long DEFAULT_CHECK_COMMAND_TIMEOUT_MILLIS- See Also:
-
-
Constructor Details
-
ProcessUtils
public ProcessUtils()
-
-
Method Details
-
escapeCommandLine
This should correctly put double-quotes around an argument if ProcessBuilder doesn't seem to work (as it doesn't on paths with spaces on Windows)- Parameters:
arg-- Returns:
-
unescapeCommandLine
-
execute
public static FileProcessResult execute(ProcessBuilder pb, long requestedTimeoutMillis, int maxStdoutBuffer, int maxStdErrBuffer) throws IOException This writes stdout and stderr to the FileProcessResult.Equivalent to
execute(ProcessBuilder, ParseContext, long, int, int)with a null context:requestedTimeoutMillisis granted unclipped, no checkpointing.- Parameters:
pb-requestedTimeoutMillis-maxStdoutBuffer-maxStdErrBuffer-- Returns:
- Throws:
IOException
-
execute
public static FileProcessResult execute(ProcessBuilder pb, ParseContext context, long requestedTimeoutMillis, int maxStdoutBuffer, int maxStdErrBuffer) throws IOException Same asexecute(ProcessBuilder, long, int, int), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())(seeParseTimeout.budgetFor(long)) so no single call can outlast the task's total timeout regardless of its own configuration. The granted budget and original request are both recorded on the result (seeFileProcessResult.getRequestedTimeoutMillis(),FileProcessResult.isClippedByRemaining()).While waiting, checkpoints the
ParseTimeoutincontext(if any) every 1000L ms, so a bounded external call can run longer than the progress (stall-detection) timeout without looking like a hang -- the wait itself is progress. A nullcontextbehaves like the four-argument overload.- Parameters:
pb-context- may be nullrequestedTimeoutMillis- the timeout the caller's own configuration asks formaxStdoutBuffer-maxStdErrBuffer-- Returns:
- Throws:
IOException
-
execute
public static FileProcessResult execute(ProcessBuilder pb, long requestedTimeoutMillis, Path stdoutRedirect, int maxStdErrBuffer) throws IOException This redirects stdout to stdoutRedirect path.Equivalent to
execute(ProcessBuilder, ParseContext, long, Path, int)with a null context, i.e.requestedTimeoutMillisis granted unclipped and the wait does not checkpoint any task's progress timeout.- Parameters:
pb-requestedTimeoutMillis-stdoutRedirect-maxStdErrBuffer-- Returns:
- Throws:
IOException
-
execute
public static FileProcessResult execute(ProcessBuilder pb, ParseContext context, long requestedTimeoutMillis, Path stdoutRedirect, int maxStdErrBuffer) throws IOException Same asexecute(ProcessBuilder, long, Path, int), but bounds the wait tomin(requestedTimeoutMillis, ParseTimeout.remainingMillis())and checkpoints while waiting -- seeexecute(ProcessBuilder, ParseContext, long, int, int).- Parameters:
pb-context- may be nullrequestedTimeoutMillis- the timeout the caller's own configuration asks forstdoutRedirect-maxStdErrBuffer-- Returns:
- Throws:
IOException
-
checkCommand
Checks to see if the command can be run. Typically used with something like "myapp --version" to check to see if "myapp" is installed and on the path.Equivalent to
checkCommandWithTimeout(String[], long, int...)withDEFAULT_CHECK_COMMAND_TIMEOUT_MILLIS.- Parameters:
checkCmd- The check command to runerrorValue- What is considered an error value? Default is 127 (command not found).- Returns:
- true if the command ran successfully (exit code not in errorValue list)
-
checkCommand
Checks to see if the command can be run. Typically used with something likenew String[]{"myapp", "--version"}to check to see if "myapp" is installed and on the path.Equivalent to
checkCommandWithTimeout(String[], long, int...)withDEFAULT_CHECK_COMMAND_TIMEOUT_MILLIS.- Parameters:
checkCmd- The check command to runerrorValue- What is considered an error value? Default is 127 (command not found).- Returns:
- true if the command ran successfully (exit code not in errorValue list)
-
checkCommandWithTimeout
public static boolean checkCommandWithTimeout(String[] checkCmd, long timeoutMillis, int... errorValue) Same ascheckCommand(String[], int...), but with a caller-specified timeout instead of the 5000Lms default.Deliberately a distinct method name, not a same-named overload:
checkCommand(cmd, 500)would silently resolve tocheckCommand(String[], int...)witherrorValue={500}at the default timeout -- Java prefers the varargs-only overload over wideningintto this method'slongparameter, with no compile error to catch the mistake.- Parameters:
timeoutMillis- how long to wait for the command to exiterrorValue- What is considered an error value? Default is 127 (command not found).- Returns:
- true if the command ran successfully (exit code not in errorValue list)
-
waitForWithHeartbeat
public static boolean waitForWithHeartbeat(Process p, ParseContext context, long timeoutMillis) throws InterruptedException Waits for the process to exit, likeProcess.waitFor(long, TimeUnit), but polls in 1000L ms increments and checkpointscontext'sParseTimeoutafter each increment that doesn't complete -- this is what lets a bounded external call run longer than the progress timeout without tripping the stall detector: the wait itself is progress.Public so callers managing their own
Process(not going throughexecute(ProcessBuilder, ParseContext, long, int, int)) can still checkpoint while waiting.- Parameters:
context- may be null, in which case no checkpoint is recordedtimeoutMillis- total wait time in ms; zero or negative checks once without waiting- Returns:
- true if the process exited before the timeout elapsed
- Throws:
InterruptedException
-