use of org.codelibs.fess.crawler.exception.ExecutionTimeoutException in project fess-crawler by codelibs.
the class CommandExtractor method executeCommand.
private void executeCommand(final File inputFile, final File outputFile) {
if (StringUtil.isBlank(command)) {
throw new CrawlerSystemException("command is empty.");
}
final Map<String, String> params = new HashMap<>();
params.put("$INPUT_FILE", inputFile.getAbsolutePath());
params.put("$OUTPUT_FILE", outputFile.getAbsolutePath());
final List<String> cmdList = parseCommand(command, params);
if (logger.isInfoEnabled()) {
logger.info("Command: " + cmdList);
}
final ProcessBuilder pb = new ProcessBuilder(cmdList);
if (workingDirectory != null) {
pb.directory(workingDirectory);
}
if (standardOutput) {
pb.redirectOutput(outputFile);
} else {
pb.redirectErrorStream(true);
}
Process currentProcess = null;
MonitorThread mt = null;
try {
currentProcess = pb.start();
// monitoring
mt = new MonitorThread(currentProcess, executionTimeout);
mt.start();
final InputStreamThread it = new InputStreamThread(currentProcess.getInputStream(), commandOutputEncoding, maxOutputLine);
it.start();
currentProcess.waitFor();
it.join(5000);
if (mt.isTeminated()) {
throw new ExecutionTimeoutException("The command execution is timeout: " + cmdList);
}
final int exitValue = currentProcess.exitValue();
if (logger.isInfoEnabled()) {
if (standardOutput) {
logger.info("Exit Code: " + exitValue);
} else {
logger.info("Exit Code: " + exitValue + " - Process Output:\n" + it.getOutput());
}
}
if (exitValue == 143 && mt.isTeminated()) {
throw new ExecutionTimeoutException("The command execution is timeout: " + cmdList);
}
} catch (final CrawlerSystemException e) {
throw e;
} catch (final InterruptedException e) {
if (mt != null && mt.isTeminated()) {
throw new ExecutionTimeoutException("The command execution is timeout: " + cmdList, e);
}
throw new CrawlerSystemException("Process terminated.", e);
} catch (final Exception e) {
throw new CrawlerSystemException("Process terminated.", e);
} finally {
if (mt != null) {
mt.setFinished(true);
try {
mt.interrupt();
} catch (final Exception e) {
}
}
if (currentProcess != null) {
try {
currentProcess.destroy();
} catch (final Exception e) {
}
}
currentProcess = null;
}
}
use of org.codelibs.fess.crawler.exception.ExecutionTimeoutException in project fess-crawler by codelibs.
the class CommandExtractorTest method test_getText_timeout.
public void test_getText_timeout() throws IOException {
final File scriptFile = createScriptTempFile(3);
final String content = "TEST";
final File contentFile = createContentFile(".txt", content.getBytes());
final CommandExtractor extractor = new CommandExtractor();
extractor.executionTimeout = 1000L;
extractor.command = getCommand(scriptFile);
final Map<String, String> params = new HashMap<String, String>();
try {
final ExtractData data = extractor.getText(new FileInputStream(contentFile), params);
fail(data.toString());
} catch (final ExecutionTimeoutException e) {
}
}
Aggregations