@Override
public void execute(Props parentProps, FlowCallback callback)
{
if (parentProps == null) {
parentProps = new Props();
}
synchronized (sync) {
if (this.parentProps == null) {
this.parentProps = parentProps;
}
else if (jobState != Status.COMPLETED && ! this.parentProps.equalsProps(parentProps)) {
throw new IllegalArgumentException(
String.format(
"%s.execute() called with multiple differing parentProps objects. " +
"Call reset() before executing again with a different Props object. this.parentProps[%s], parentProps[%s]",
getClass().getSimpleName(),
this.parentProps,
parentProps
)
);
}
switch (jobState) {
case READY:
jobState = Status.RUNNING;
startTime = new DateTime();
callbacksToCall.add(callback);
break;
case RUNNING:
callbacksToCall.add(callback);
return;
case IGNORED:
jobState = Status.COMPLETED;
case COMPLETED:
case SUCCEEDED:
callback.completed(Status.SUCCEEDED);
return;
case FAILED:
callback.completed(Status.FAILED);
return;
}
}
try {
// Only one thread should ever be able to get to this point because of management of jobState
// Thus, this should only ever get called once before the job finishes (at which point it could be reset)
job = jobManager.loadJob(getName(), parentProps, true);
}
catch (Exception e) {
logger.warn(
String.format("Exception thrown while creating job[%s]", getName()),
e
);
job = null;
}
if (job == null) {
logger.warn(
String.format("Job[%s] doesn't exist, but was supposed to run. Perhaps someone changed the flow?", getName())
);
final List<FlowCallback> callbackList;
synchronized (sync) {
jobState = Status.FAILED;
callbackList = callbacksToCall; // Get the reference before leaving the synchronized
}
callCallbacks(callbackList, jobState);
return;
}
Thread theThread = new Thread(
new Runnable()
{
@Override
public void run()
{
final List<FlowCallback> callbackList;
try {
job.run();
}
catch (Exception e) {
synchronized (sync) {
jobState = Status.FAILED;
returnProps = new Props();
exceptions.put(getName(), e);
callbackList = callbacksToCall; // Get the reference before leaving the synchronized
}
callCallbacks(callbackList, jobState);