You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/12/14 17:33:30 UTC
(commons-exec) branch master updated: Add Watchdog.Watchdog(Duration).
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git
The following commit(s) were added to refs/heads/master by this push:
new 6eda6204 Add Watchdog.Watchdog(Duration).
6eda6204 is described below
commit 6eda62041ab41635ba5700396516b82256d9574c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Dec 14 12:33:26 2023 -0500
Add Watchdog.Watchdog(Duration).
- Add ExecuteWatchdog.ExecuteWatchdog(Duration)
- Deprecate ExecuteWatchdog.ExecuteWatchdog(long)
- Deprecate Watchdog.Watchdog(long)
---
src/changes/changes.xml | 4 ++
.../org/apache/commons/exec/ExecuteWatchdog.java | 50 ++++++++++++++--------
.../java/org/apache/commons/exec/Watchdog.java | 21 +++++++++
3 files changed, 58 insertions(+), 17 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 66642f28..f52554ae 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -29,6 +29,8 @@
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ShutdownHookProcessDestroyer.isEmpty().</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add DefaultExecuteResultHandler.waitFor(Duration).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add Watchdog.Watchdog(Duration).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add ExecuteWatchdog.ExecuteWatchdog(Duration).</action>
<!-- FIX -->
<action issue="EXEC-105" type="fix" date="2023-07-16" due-to="Dimitrios Efthymiou">
Fix code snippet in tutorial page.
@@ -50,6 +52,8 @@
</action>
<!-- REMOVE -->
<action dev="ggregory" type="remove" due-to="Gary Gregory">Deprecate DefaultExecuteResultHandler.waitFor(long).</action>
+ <action dev="ggregory" type="remove" due-to="Gary Gregory">Deprecate ExecuteWatchdog.ExecuteWatchdog(long).</action>
+ <action dev="ggregory" type="remove" due-to="Gary Gregory">Deprecate Watchdog.Watchdog(long).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory, Dependabot">
Bump github actions #52.
diff --git a/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java b/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java
index 9de4e84d..9792b872 100644
--- a/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java
+++ b/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java
@@ -17,6 +17,7 @@
package org.apache.commons.exec;
+import java.time.Duration;
import java.util.Objects;
import org.apache.commons.exec.util.DebugUtils;
@@ -49,6 +50,9 @@ public class ExecuteWatchdog implements TimeoutObserver {
/** The marker for an infinite timeout */
public static final long INFINITE_TIMEOUT = -1;
+ /** The marker for an infinite timeout */
+ public static final Duration INFINITE_TIMEOUT_DURATION = Duration.ofMillis(INFINITE_TIMEOUT);
+
/** The process to execute and watch for duration. */
private Process process;
@@ -73,12 +77,13 @@ public class ExecuteWatchdog implements TimeoutObserver {
/**
* Creates a new watchdog with a given timeout.
*
- * @param timeout the timeout for the process in milliseconds. It must be greater than 0 or 'INFINITE_TIMEOUT'
+ * @param timeout the timeout for the process in milliseconds. It must be greater than 0 or {@code INFINITE_TIMEOUT_DURATION}.
+ * @since 1.4.0
*/
- public ExecuteWatchdog(final long timeout) {
+ public ExecuteWatchdog(final Duration timeout) {
this.killedProcess = false;
this.watch = false;
- this.hasWatchdog = timeout != INFINITE_TIMEOUT;
+ this.hasWatchdog = !INFINITE_TIMEOUT_DURATION.equals(timeout);
this.processStarted = false;
if (this.hasWatchdog) {
this.watchdog = new Watchdog(timeout);
@@ -88,6 +93,17 @@ public class ExecuteWatchdog implements TimeoutObserver {
}
}
+ /**
+ * Creates a new watchdog with a given timeout.
+ *
+ * @param timeoutMillis the timeout for the process in milliseconds. It must be greater than 0 or {@code INFINITE_TIMEOUT}.
+ * @deprecated Use {@link #ExecuteWatchdog(Duration)}.
+ */
+ @Deprecated
+ public ExecuteWatchdog(final long timeoutMillis) {
+ this(Duration.ofMillis(timeoutMillis));
+ }
+
/**
* This method will rethrow the exception that was possibly caught during the run of the process. It will only remains valid once the process has been
* terminated either by 'error', timeout or manual intervention. Information will be discarded once a new process is ran.
@@ -113,8 +129,8 @@ public class ExecuteWatchdog implements TimeoutObserver {
*/
public synchronized void destroyProcess() {
ensureStarted();
- this.timeoutOccured(null);
- this.stop();
+ timeoutOccured(null);
+ stop();
}
/**
@@ -124,7 +140,7 @@ public class ExecuteWatchdog implements TimeoutObserver {
private void ensureStarted() {
while (!processStarted && caught == null) {
try {
- this.wait();
+ wait();
} catch (final InterruptedException e) {
throw new IllegalStateException(e.getMessage(), e);
}
@@ -138,9 +154,9 @@ public class ExecuteWatchdog implements TimeoutObserver {
*
*/
public synchronized void failedToStart(final Exception e) {
- this.processStarted = true;
- this.caught = e;
- this.notifyAll();
+ processStarted = true;
+ caught = e;
+ notifyAll();
}
/**
@@ -174,16 +190,16 @@ public class ExecuteWatchdog implements TimeoutObserver {
*/
public synchronized void start(final Process processToMonitor) {
Objects.requireNonNull(processToMonitor, "processToMonitor");
- if (this.process != null) {
+ if (process != null) {
throw new IllegalStateException("Already running.");
}
- this.caught = null;
- this.killedProcess = false;
- this.watch = true;
- this.process = processToMonitor;
- this.processStarted = true;
- this.notifyAll();
- if (this.hasWatchdog) {
+ caught = null;
+ killedProcess = false;
+ watch = true;
+ process = processToMonitor;
+ processStarted = true;
+ notifyAll();
+ if (hasWatchdog) {
watchdog.start();
}
}
diff --git a/src/main/java/org/apache/commons/exec/Watchdog.java b/src/main/java/org/apache/commons/exec/Watchdog.java
index dc892307..95f4e289 100644
--- a/src/main/java/org/apache/commons/exec/Watchdog.java
+++ b/src/main/java/org/apache/commons/exec/Watchdog.java
@@ -17,6 +17,7 @@
package org.apache.commons.exec;
+import java.time.Duration;
import java.util.Enumeration;
import java.util.Vector;
@@ -33,6 +34,26 @@ public class Watchdog implements Runnable {
private boolean stopped;
+ /**
+ * Constructs a new instance.
+ *
+ * @param timeout the timeout duration.
+ * @since 1.4.0
+ */
+ public Watchdog(final Duration timeout) {
+ if (timeout.isNegative() || Duration.ZERO.equals(timeout)) {
+ throw new IllegalArgumentException("timeout must not be less than 1.");
+ }
+ this.timeoutMillis = timeout.toMillis();
+ }
+
+ /**
+ * Constructs a new instance.
+ *
+ * @param timeoutMillis the timeout duration.
+ * @deprecated Use {@link #Watchdog(Duration)}.
+ */
+ @Deprecated
public Watchdog(final long timeoutMillis) {
if (timeoutMillis < 1) {
throw new IllegalArgumentException("timeout must not be less than 1.");