From 0d4817ea42face353093d84ea46af38d49ce5512 Mon Sep 17 00:00:00 2001 From: Mark Lassau Date: Tue, 5 Sep 2023 19:14:17 +1000 Subject: [PATCH] Issue 355: Allow an application developer to override the default Executor Timeout value --- .../github/kokorin/jaffree/ffmpeg/FFmpeg.java | 21 +++++++++++++++- .../jaffree/process/ProcessHandler.java | 25 +++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java b/src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java index 0a3f8177..9e2907ba 100644 --- a/src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java +++ b/src/main/java/com/github/kokorin/jaffree/ffmpeg/FFmpeg.java @@ -59,6 +59,7 @@ public class FFmpeg { private LogLevel logLevel = LogLevel.INFO; private String contextName = null; + private Integer executorTimeoutMillis = null; private final Path executable; @@ -387,6 +388,23 @@ public FFmpeg setContextName(final String contextName) { return this; } + /** + * Overrides the default {@link com.github.kokorin.jaffree.process.Executor} timeout. + *

+ * Most normal use cases will easily complete within the default timeout. It is not recommended + * to set an explicit timeout value unless you have actually experienced unwanted timeouts. + * + * @param executorTimeoutMillis the custom executor timeout in milliseconds + * @return this + */ + public FFmpeg setExecutorTimeoutMillis(final int executorTimeoutMillis) { + if (executorTimeoutMillis < 0) { + throw new IllegalArgumentException("Executor timeout cannot be negative"); + } + this.executorTimeoutMillis = executorTimeoutMillis; + return this; + } + /** * Starts synchronous ffmpeg execution. *

@@ -484,7 +502,8 @@ protected ProcessHandler createProcessHandler() { .setStdErrReader(createStdErrReader(outputListener)) .setStdOutReader(createStdOutReader()) .setHelpers(helpers) - .setArguments(buildArguments()); + .setArguments(buildArguments()) + .setExecutorTimeoutMillis(executorTimeoutMillis); } /** diff --git a/src/main/java/com/github/kokorin/jaffree/process/ProcessHandler.java b/src/main/java/com/github/kokorin/jaffree/process/ProcessHandler.java index a7e5b3e3..2e0740ff 100644 --- a/src/main/java/com/github/kokorin/jaffree/process/ProcessHandler.java +++ b/src/main/java/com/github/kokorin/jaffree/process/ProcessHandler.java @@ -49,8 +49,9 @@ public class ProcessHandler { private List helpers = null; private Stopper stopper = null; private List arguments = Collections.emptyList(); + private int executorTimeoutMillis = DEFAULT_EXECUTOR_TIMEOUT_MILLIS; - private static final int EXECUTOR_TIMEOUT_MILLIS = 10_000; + private static final int DEFAULT_EXECUTOR_TIMEOUT_MILLIS = 10_000; private static final Logger LOGGER = LoggerFactory.getLogger(ProcessHandler.class); /** @@ -120,6 +121,24 @@ public synchronized ProcessHandler setArguments(final List arguments) return this; } + /** + * Overrides the default Executor timeout. + *

+ * A null value is interpreted as "use default timeout". + * + * @param executorTimeoutMillis the new Executor timeout in milliseconds + * @return this + */ + public ProcessHandler setExecutorTimeoutMillis(final Integer executorTimeoutMillis) { + if (executorTimeoutMillis != null) { + if (executorTimeoutMillis < 0) { + throw new IllegalArgumentException("Executor timeout cannot be negative"); + } + this.executorTimeoutMillis = executorTimeoutMillis; + } + return this; + } + /** * Executes a program. *

@@ -180,7 +199,9 @@ protected T interactWithProcess(final Process process) { status = process.waitFor(); LOGGER.info("Process has finished with status: {}", status); - waitForExecutorToStop(executor, EXECUTOR_TIMEOUT_MILLIS); + if (executorTimeoutMillis > 0) { + waitForExecutorToStop(executor, executorTimeoutMillis); + } } catch (InterruptedException e) { LOGGER.warn("Process has been interrupted"); if (stopper != null) {