summaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java')
-rw-r--r--api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java65
1 files changed, 43 insertions, 22 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 cc837ba..a1c81e8 100644
--- a/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java
+++ b/api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java
@@ -21,19 +21,19 @@ public abstract class AbstractTask implements Runnable
* The id of the task instance.
*/
@Expose
- private String id = null;
+ private volatile String id = null;
/**
* Parent task. This task won't be started as long as the parent task currently
* waiting for execution or is being executed. Otherwise this task is available for execution.
* Note that MAX_INSTANCES is still being taken into account. Set to null to ignore.
*/
@Expose
- private String parentTask = null;
+ private volatile String parentTask = null;
/**
* If the parent task failed to execute, don't run this task and fail immediately.
*/
@Expose
- private boolean failOnParentFail = true;
+ private volatile boolean failOnParentFail = true;
/*
* Variables we're working with - these should never be set from incoming (json) data
@@ -57,11 +57,16 @@ public abstract class AbstractTask implements Runnable
/**
* Status of Task
*/
- private TaskStatus status = TaskStatus.ts_waiting;
+ private volatile TaskStatus status = TaskStatus.ts_waiting;
/**
* Reference to parent task
*/
- private AbstractTask parent = null;
+ private volatile AbstractTask parent = null;
+ /**
+ * Reference to "owner" so we can tell it to check for more
+ * work when we're done.
+ */
+ private volatile FinishCallback finishCallback = null;
/**
* Default constructor which should not be overridden
@@ -79,12 +84,24 @@ public abstract class AbstractTask implements Runnable
* Initialize the task; method used by the {@link Taskmanager}.
* Put your own initialization code in initTask()
*/
- public final boolean init( AbstractTask parent )
+ public final boolean init( AbstractTask parent, FinishCallback finishCallback )
{
if ( this.initDone ) {
LOG.fatal( "init() called twice on " + this.getClass().getSimpleName() );
System.exit( 1 );
}
+ if ( this.parentTask != null && this.parentTask.isEmpty() )
+ this.parentTask = null;
+ if ( this.parentTask != null && parent == null ) {
+ if ( this.failOnParentFail ) {
+ this.status = new TaskStatus( StatusCode.PARENT_FAILED, this.id );
+ return false;
+ }
+ this.parentTask = null;
+ }
+ if ( this.parentTask == null && parent != null )
+ parent = null;
+ this.finishCallback = finishCallback;
this.parent = parent;
this.status = new TaskStatus( StatusCode.TASK_WAITING, this.id );
this.initDone = true;
@@ -116,6 +133,19 @@ public abstract class AbstractTask implements Runnable
*/
protected abstract boolean execute();
+ /**
+ * This is called when a client requests the status of this task. In case you
+ * want to return complex structures like Lists, which are not thread safe, you
+ * might want to keep that list outside the status class you return, and only
+ * create a copy of it for the status class in this function.
+ * If you only return more or less atomic data, you don't need to override
+ * this function
+ */
+ protected void updateStatus()
+ {
+
+ }
+
/*
* Final methods
*/
@@ -126,7 +156,7 @@ public abstract class AbstractTask implements Runnable
* @return id of parent task, null if no parent set
*
*/
- public String getParentTaskId()
+ public final String getParentTaskId()
{
return this.parentTask;
}
@@ -151,7 +181,7 @@ public abstract class AbstractTask implements Runnable
*/
public final TaskStatus getStatus()
{
- if ( this.initDone && this.parentTask != null ) {
+ if ( this.initDone && this.parent != null ) {
final StatusCode parentStatus = parent.getStatusCode();
switch ( parentStatus ) {
case DUPLICATE_ID:
@@ -160,8 +190,10 @@ public abstract class AbstractTask implements Runnable
case NO_SUCH_CONSTRUCTOR:
case PARENT_FAILED:
case TASK_ERROR:
- if ( this.failOnParentFail )
+ if ( this.failOnParentFail ) {
this.status.statusCode = StatusCode.PARENT_FAILED;
+ LOG.debug( "Parent " + this.parentTask + " of " + this.id + " failed." );
+ }
this.parentTask = null;
break;
default:
@@ -267,18 +299,7 @@ public abstract class AbstractTask implements Runnable
} else {
this.status.statusCode = StatusCode.TASK_ERROR;
}
- }
-
- /**
- * This is called when a client requests the status of this task. In case you
- * want to return complex structures like Lists, which are not thread safe, you
- * might want to keep that list outside the status class you return, and only
- * create a copy of it for the status class in this function.
- * If you only return more or less atomic data, you don't need to override
- * this function
- */
- protected void updateStatus()
- {
-
+ if ( this.finishCallback != null )
+ this.finishCallback.taskFinished();
}
}