use of com.netflix.spinnaker.halyard.core.DaemonResponse in project halyard by spinnaker.
the class DaemonTaskHandler method reduceChildren.
public static <U, T> DaemonResponse<U> reduceChildren(U base, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) {
DaemonTask task = getTask();
if (task != null) {
U responseBody = base;
ProblemSet problemSet = new ProblemSet();
DaemonResponse<U> response = new DaemonResponse<>(responseBody, problemSet);
return (DaemonResponse) task.getChildren().stream().reduce(response, (o, t) -> {
DaemonResponse<U> collector = (DaemonResponse<U>) o;
DaemonTask child = (DaemonTask) t;
DaemonResponse<T> childResponse;
try {
childResponse = task.reapChild(child);
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted("Interrupted during reap", e);
}
DaemonTask.State state = child.getState();
if (!state.isTerminal()) {
throw new IllegalStateException("Child task " + child + " reaped but non-terminal.");
}
switch(state) {
case FAILED:
throw new HalException(childResponse.getProblemSet().getProblems());
case INTERRUPTED:
task.interrupt();
throw new DaemonTaskInterrupted(child.getFatalError());
case TIMED_OUT:
task.timeout();
throw new DaemonTaskInterrupted("Child task timed out");
case SUCCEEDED:
break;
default:
throw new IllegalStateException("Unknown terminal state " + state);
}
collector.getProblemSet().addAll(childResponse.getProblemSet());
collector.setResponseBody(accumulator.apply(collector.getResponseBody(), childResponse.getResponseBody()));
return collector;
}, (Object o1, Object o2) -> {
DaemonResponse<U> r1 = (DaemonResponse<U>) o1;
DaemonResponse<U> r2 = (DaemonResponse<U>) o2;
r1.setResponseBody(combiner.apply(r1.getResponseBody(), r2.getResponseBody()));
r1.getProblemSet().addAll(r2.getProblemSet());
return r1;
});
} else {
throw new IllegalStateException("You must be running a DaemonTask to reduce child tasks");
}
}
Aggregations