Search in sources :

Example 1 with ExecutionTimeoutException

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;
    }
}
Also used : HashMap(java.util.HashMap) ExecutionTimeoutException(org.codelibs.fess.crawler.exception.ExecutionTimeoutException) IOException(java.io.IOException) ExtractException(org.codelibs.fess.crawler.exception.ExtractException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExecutionTimeoutException(org.codelibs.fess.crawler.exception.ExecutionTimeoutException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException)

Example 2 with ExecutionTimeoutException

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) {
    }
}
Also used : ExecutionTimeoutException(org.codelibs.fess.crawler.exception.ExecutionTimeoutException) ExtractData(org.codelibs.fess.crawler.entity.ExtractData) HashMap(java.util.HashMap) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

HashMap (java.util.HashMap)2 ExecutionTimeoutException (org.codelibs.fess.crawler.exception.ExecutionTimeoutException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ExtractData (org.codelibs.fess.crawler.entity.ExtractData)1 CrawlerSystemException (org.codelibs.fess.crawler.exception.CrawlerSystemException)1 ExtractException (org.codelibs.fess.crawler.exception.ExtractException)1