Commit 9820962a authored by Simon Rettberg's avatar Simon Rettberg
Browse files

Switch to semaphore for signalling the mainloop to check for work

parent fd3c703e
Showing with 24 additions and 28 deletions
+24 -28
......@@ -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();
}
......
......@@ -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 );
......
......@@ -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<String, AbstractTask> 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<AbstractTask> 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 ) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment