From 99c04872cddb2b9eb537174ed96e49989b2aa162 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 3 Jul 2023 18:15:47 +0200 Subject: Simplify timed wait in SystemCommandTask --- .../openslx/taskmanager/api/SystemCommandTask.java | 29 +++++++++------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'api') diff --git a/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java b/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java index 172d2ed..4754dca 100644 --- a/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java +++ b/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java @@ -7,6 +7,7 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -40,8 +41,8 @@ public abstract class SystemCommandTask extends AbstractTask if ( command == null || command.length == 0 ) { return processEnded( -1 ); } - for (String a : command) { - if (a == null) { + for ( String a : command ) { + if ( a == null ) { log.warn( "An argument from initCommandLine is null: " + Arrays.toString( command ) ); return processEnded( -5 ); } @@ -110,23 +111,15 @@ public abstract class SystemCommandTask extends AbstractTask // Wait for everything int retval = 124; // Default to 124, which is what the timeout util does - if ( this.timeoutSeconds <= 0 ) { - retval = process.waitFor(); - } else { - int togo = timeoutSeconds * 10; - while ( togo-- > 0 ) { - try { + try { + if ( this.timeoutSeconds <= 0 ) { + retval = process.waitFor(); + } else { + if ( process.waitFor( this.timeoutSeconds, TimeUnit.SECONDS ) ) { retval = process.exitValue(); - break; - } catch ( IllegalThreadStateException e1 ) { - // Still running.... - try { - Thread.sleep( 100 ); - } catch ( Exception e2 ) { - // Bummer.... - } } } + } catch ( IllegalThreadStateException e1 ) { } try { stdout.join( 500 ); @@ -155,8 +148,9 @@ public abstract class SystemCommandTask extends AbstractTask Thread.currentThread().interrupt(); return false; } finally { - if ( process != null ) + if ( process != null ) { process.destroy(); + } } } @@ -217,6 +211,7 @@ public abstract class SystemCommandTask extends AbstractTask /** * Override this to modify the environment of the process to be started. + * * @param environment */ protected void initEnvironment( Map environment ) -- cgit v1.2.3-55-g7522