org.infrastructurebuilder.util.ProcessExecutionResultBag Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ibexecutor Show documentation
Show all versions of ibexecutor Show documentation
Standard interfaces for running applications as a heavyweight process
/**
* Copyright © 2019 admin ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.infrastructurebuilder.util;
import static java.time.Duration.between;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Future;
import org.infrastructurebuilder.util.artifacts.JSONBuilder;
import org.infrastructurebuilder.util.artifacts.JSONOutputEnabled;
import org.json.JSONArray;
import org.json.JSONObject;
import org.zeroturnaround.exec.ProcessResult;
public interface ProcessExecutionResultBag extends JSONOutputEnabled {
public static final String EXECUTION_IDS = "executed-ids";
@Deprecated
public static final String INCOMPLETE_FUTURE_IDS = "incomplete-futures-ids";
public static final String RESULTS = "results";
List getExecutedIds();
Map getExecutions();
/**
* This will be removed before 1.0.0
* @return
*/
@Deprecated
Map> getRunningFutures();
default Set getIncompleteFuturesIds() {
return getRunningFutures().keySet();
}
default Optional getDuration() {
return getStart().map(start -> between(start, getEnd().orElseThrow(() -> new ProcessException("No end time"))));
}
default Optional getEnd() {
return getExecutions().values().stream().map(ProcessExecutionResult::getEndTime).max(Instant::compareTo);
}
default List getErrors() {
return getExecutions().values().stream()
.filter(ProcessExecutionResult::isError)
.map(ProcessExecutionResult::getId)
.collect(toList());
}
default Optional getExecution(final String id) {
return ofNullable(getExecutions().get(requireNonNull(id)));
}
default Map> getExecutionEnvironment() {
return getExecutions().entrySet().stream()
.collect(toMap(k -> k.getKey(), v -> v.getValue().getExecutionEnvironment()));
}
default Map getResults() {
return getExecutions().values().stream().collect(toMap(ProcessExecutionResult::getId, identity()));
}
default Optional getStart() {
return getExecutions().values().stream().map(ProcessExecutionResult::getStartTime).min(Instant::compareTo);
}
default List getStdErr() {
final Map> l = getStdErrs();
return getExecutedIds().stream().map(l::get).flatMap(List::stream).collect(toList());
}
default Map> getStdErrs() {
return getExecutions().values().stream()
.collect(toMap(ProcessExecutionResult::getId, ProcessExecutionResult::getStdErr));
}
default List getStdOut() {
return getExecutedIds().stream().map(key -> getStdOuts().get(key)).flatMap(List::stream).collect(toList());
}
default Map> getStdOuts() {
return getExecutions().values().stream()
.collect(toMap(ProcessExecutionResult::getId, ProcessExecutionResult::getStdOut));
}
@Override
default JSONObject asJSON() {
return JSONBuilder.newInstance()
.addJSONArray(EXECUTION_IDS, new JSONArray(getExecutedIds()))
.addJSONArray(INCOMPLETE_FUTURE_IDS, new JSONArray(getIncompleteFuturesIds()))
.addJSONArray(RESULTS, new JSONArray(getExecutions().values().stream().map(v -> v.asJSON()).collect(toList())))
.asJSON();
}
}