Search in sources :

Example 1 with DaemonResponse

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");
    }
}
Also used : Slf4j(lombok.extern.slf4j.Slf4j) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) BiFunction(java.util.function.BiFunction) DaemonResponse(com.netflix.spinnaker.halyard.core.DaemonResponse) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet) DaemonLocalJobExecutor(com.netflix.spinnaker.halyard.core.job.v1.DaemonLocalJobExecutor) Supplier(java.util.function.Supplier) BinaryOperator(java.util.function.BinaryOperator) DaemonResponse(com.netflix.spinnaker.halyard.core.DaemonResponse) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ProblemSet(com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet)

Aggregations

DaemonResponse (com.netflix.spinnaker.halyard.core.DaemonResponse)1 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)1 DaemonLocalJobExecutor (com.netflix.spinnaker.halyard.core.job.v1.DaemonLocalJobExecutor)1 JobExecutor (com.netflix.spinnaker.halyard.core.job.v1.JobExecutor)1 ProblemSet (com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet)1 BiFunction (java.util.function.BiFunction)1 BinaryOperator (java.util.function.BinaryOperator)1 Supplier (java.util.function.Supplier)1 Slf4j (lombok.extern.slf4j.Slf4j)1