From 9820962a9a0fb0fa9a49ddf49ee551ac41a708ae Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Feb 2015 16:28:58 +0100 Subject: Switch to semaphore for signalling the mainloop to check for work --- .../org/openslx/taskmanager/api/AbstractTask.java | 1 + .../openslx/taskmanager/api/SystemCommandTask.java | 27 ++++++++++++++-------- .../org/openslx/taskmanager/main/Taskmanager.java | 24 +++++-------------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java b/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java index a1c81e8..271a8bd 100644 --- a/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java +++ b/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java @@ -299,6 +299,7 @@ public abstract class AbstractTask implements Runnable } else { this.status.statusCode = StatusCode.TASK_ERROR; } + LOG.debug( "Finished task " + this.getClass().getSimpleName() + ": " + this.status.statusCode.toString() + " (" + this.id + ")" ); if ( this.finishCallback != null ) this.finishCallback.taskFinished(); } 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 ea71ea5..ed47336 100644 --- a/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java +++ b/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java @@ -14,9 +14,8 @@ public abstract class SystemCommandTask extends AbstractTask private static final Logger log = Logger.getLogger( SystemCommandTask.class ); private String[] command = null; - - private Process process = null; + private Process process = null; @Override protected final boolean execute() @@ -50,7 +49,7 @@ public abstract class SystemCommandTask extends AbstractTask processStdOut( line ); } } - } catch ( IOException e ) { + } catch ( Exception e ) { } } } ); @@ -67,7 +66,7 @@ public abstract class SystemCommandTask extends AbstractTask processStdErr( line ); } } - } catch ( IOException e ) { + } catch ( Exception e ) { } } } ); @@ -76,9 +75,17 @@ public abstract class SystemCommandTask extends AbstractTask stderr.start(); // Wait for everything - stdout.join(); - stderr.join(); process.waitFor(); + try { + process.getErrorStream().close(); + } catch ( Throwable t ) { + } + try { + process.getOutputStream().close(); + } catch ( Throwable t ) { + } + stdout.join( 2000 ); + stderr.join( 2000 ); return processEnded( process.exitValue() ); @@ -98,14 +105,14 @@ public abstract class SystemCommandTask extends AbstractTask process.destroy(); } } - + /** * Write data to the process's stdin. * * @param data stuff to write * @return success or failure mapped to a boolean in a really complicated way */ - protected final boolean toStdIn(byte[] data) + protected final boolean toStdIn( byte[] data ) { try { process.getOutputStream().write( data ); @@ -122,7 +129,7 @@ public abstract class SystemCommandTask extends AbstractTask * @param text stuff to write * @return success or failure mapped to a boolean in a really complicated way */ - protected final boolean toStdIn(String text) + protected final boolean toStdIn( String text ) { return toStdIn( text.getBytes( StandardCharsets.UTF_8 ) ); } @@ -147,7 +154,7 @@ public abstract class SystemCommandTask extends AbstractTask * Called when the process has finished running * * @param exitCode the process' exit code - * @return + * @return */ protected abstract boolean processEnded( int exitCode ); diff --git a/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java b/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java index f9b7752..1325dca 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java +++ b/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java @@ -5,11 +5,9 @@ import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; import org.apache.log4j.Logger; import org.openslx.taskmanager.Global; @@ -46,8 +44,7 @@ public class Taskmanager implements FinishCallback, Runnable */ private final Map instances = new ConcurrentHashMap<>(); - private final Lock workLock = new ReentrantLock(); - private final Condition doCheckForWork = workLock.newCondition(); + private final Semaphore doCheckForWork = new Semaphore( 0 ); /* * Static methods @@ -142,12 +139,7 @@ public class Taskmanager implements FinishCallback, Runnable */ protected void checkForWork() { - workLock.lock(); - try { - doCheckForWork.signalAll(); - } finally { - workLock.unlock(); - } + doCheckForWork.release(); } @Override @@ -161,12 +153,8 @@ public class Taskmanager implements FinishCallback, Runnable { try { while ( !Global.doShutdown ) { - workLock.lock(); - try { - doCheckForWork.await( 1, TimeUnit.MINUTES ); - } finally { - workLock.unlock(); - } + doCheckForWork.tryAcquire( 1, TimeUnit.MINUTES ); + doCheckForWork.drainPermits(); try { for ( Iterator it = instances.values().iterator(); it.hasNext(); ) { AbstractTask task = it.next(); @@ -176,8 +164,8 @@ public class Taskmanager implements FinishCallback, Runnable continue; } if ( task.canStart() ) { - threadPool.execute( task ); log.debug( "Started Task " + task.getClass().getSimpleName() + " (" + task.getId() + ")" ); + threadPool.execute( task ); } } } catch ( RejectedExecutionException e ) { -- cgit v1.2.3-55-g7522