use of bio.terra.workspace.service.job.exception.InvalidResultStateException in project terra-workspace-manager by DataBiosphere.
the class JobService method mapFlightStateToApiJobReport.
public ApiJobReport mapFlightStateToApiJobReport(FlightState flightState) {
FlightMap inputParameters = flightState.getInputParameters();
String description = inputParameters.get(JobMapKeys.DESCRIPTION.getKeyName(), String.class);
FlightStatus flightStatus = flightState.getFlightStatus();
String submittedDate = flightState.getSubmitted().toString();
ApiJobReport.StatusEnum jobStatus = getJobStatus(flightStatus);
String completedDate = null;
HttpStatus statusCode = HttpStatus.ACCEPTED;
if (jobStatus != StatusEnum.RUNNING) {
// If the job is completed, the JobReport should include a result code indicating success or
// failure. For failed jobs, this code is the error code. For successful jobs, this is the
// code specified by the flight if present, or a default of 200 if not.
completedDate = flightState.getCompleted().get().toString();
switch(jobStatus) {
case FAILED:
int errorCode = flightState.getException().map(e -> ErrorReportUtils.buildApiErrorReport(e).getStatusCode()).orElseThrow(() -> new InvalidResultStateException("Flight failed with no exception reported"));
statusCode = HttpStatus.valueOf(errorCode);
break;
case SUCCEEDED:
FlightMap resultMap = getResultMap(flightState);
statusCode = resultMap.get(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.class);
if (statusCode == null) {
statusCode = HttpStatus.OK;
}
break;
default:
throw new IllegalStateException("Cannot get status code of flight in unknown state " + jobStatus);
}
}
ApiJobReport jobReport = new ApiJobReport().id(flightState.getFlightId()).description(description).status(jobStatus).statusCode(statusCode.value()).submitted(submittedDate).completed(completedDate).resultURL(resultUrlFromFlightState(flightState));
return jobReport;
}
use of bio.terra.workspace.service.job.exception.InvalidResultStateException in project terra-workspace-manager by DataBiosphere.
the class JobService method retrieveJobResultWorker.
private <T> JobResultOrException<T> retrieveJobResultWorker(String jobId, Class<T> resultClass) throws StairwayException, InterruptedException {
FlightState flightState = stairwayComponent.get().getFlightState(jobId);
FlightMap resultMap = flightState.getResultMap().orElse(null);
if (resultMap == null) {
throw new InvalidResultStateException("No result map returned from flight");
}
switch(flightState.getFlightStatus()) {
case FATAL:
case ERROR:
if (flightState.getException().isPresent()) {
Exception exception = flightState.getException().get();
if (exception instanceof RuntimeException) {
return new JobResultOrException<T>().exception((RuntimeException) exception);
} else {
return new JobResultOrException<T>().exception(new JobResponseException("wrap non-runtime exception", exception));
}
}
throw new InvalidResultStateException("Failed operation with no exception reported");
case SUCCESS:
return new JobResultOrException<T>().result(resultMap.get(JobMapKeys.RESPONSE.getKeyName(), resultClass));
case RUNNING:
throw new JobNotCompleteException("Attempt to retrieve job result before job is complete; job id: " + flightState.getFlightId());
default:
throw new InvalidResultStateException("Impossible case reached");
}
}
Aggregations