Search in sources :

Example 1 with InvalidResultStateException

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;
}
Also used : ApiJobReport(bio.terra.workspace.generated.model.ApiJobReport) ScheduledFuture(java.util.concurrent.ScheduledFuture) JobNotCompleteException(bio.terra.workspace.service.job.exception.JobNotCompleteException) StatusEnum(bio.terra.workspace.generated.model.ApiJobReport.StatusEnum) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiJobReport(bio.terra.workspace.generated.model.ApiJobReport) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) FlightState(bio.terra.stairway.FlightState) Path(java.nio.file.Path) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) UUID(java.util.UUID) StairwayException(bio.terra.stairway.exception.StairwayException) SamWorkspaceAction(bio.terra.workspace.service.iam.model.SamConstants.SamWorkspaceAction) Executors(java.util.concurrent.Executors) Flight(bio.terra.stairway.Flight) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) List(java.util.List) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightFilter(bio.terra.stairway.FlightFilter) ErrorReportUtils(bio.terra.workspace.common.utils.ErrorReportUtils) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DuplicateFlightIdException(bio.terra.stairway.exception.DuplicateFlightIdException) JobConfiguration(bio.terra.workspace.app.configuration.external.JobConfiguration) JobNotFoundException(bio.terra.workspace.service.job.exception.JobNotFoundException) Callable(java.util.concurrent.Callable) StairwayComponent(bio.terra.common.stairway.StairwayComponent) Stairway(bio.terra.stairway.Stairway) ArrayList(java.util.ArrayList) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport) WorkspaceFlightMapKeys(bio.terra.workspace.service.workspace.flight.WorkspaceFlightMapKeys) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) FlightFilterOp(bio.terra.stairway.FlightFilterOp) Traced(io.opencensus.contrib.spring.aop.Traced) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MdcHook(bio.terra.workspace.common.utils.MdcHook) FlightDebugInfo(bio.terra.stairway.FlightDebugInfo) FlightBeanBag(bio.terra.workspace.common.utils.FlightBeanBag) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException) Logger(org.slf4j.Logger) DuplicateJobIdException(bio.terra.workspace.service.job.exception.DuplicateJobIdException) StairwayDatabaseConfiguration(bio.terra.workspace.app.configuration.external.StairwayDatabaseConfiguration) FlightMap(bio.terra.stairway.FlightMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TracingHook(bio.terra.common.stairway.TracingHook) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) FlightStatus(bio.terra.stairway.FlightStatus) IngressConfiguration(bio.terra.workspace.app.configuration.external.IngressConfiguration) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) StatusEnum(bio.terra.workspace.generated.model.ApiJobReport.StatusEnum) FlightStatus(bio.terra.stairway.FlightStatus) HttpStatus(org.springframework.http.HttpStatus) FlightMap(bio.terra.stairway.FlightMap)

Example 2 with InvalidResultStateException

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");
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) FlightMap(bio.terra.stairway.FlightMap) JobNotCompleteException(bio.terra.workspace.service.job.exception.JobNotCompleteException) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) JobNotCompleteException(bio.terra.workspace.service.job.exception.JobNotCompleteException) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) StairwayException(bio.terra.stairway.exception.StairwayException) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) DuplicateFlightIdException(bio.terra.stairway.exception.DuplicateFlightIdException) JobNotFoundException(bio.terra.workspace.service.job.exception.JobNotFoundException) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException) DuplicateJobIdException(bio.terra.workspace.service.job.exception.DuplicateJobIdException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FlightMap (bio.terra.stairway.FlightMap)2 FlightState (bio.terra.stairway.FlightState)2 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)2 DuplicateFlightIdException (bio.terra.stairway.exception.DuplicateFlightIdException)2 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)2 StairwayException (bio.terra.stairway.exception.StairwayException)2 DuplicateJobIdException (bio.terra.workspace.service.job.exception.DuplicateJobIdException)2 InternalStairwayException (bio.terra.workspace.service.job.exception.InternalStairwayException)2 InvalidResultStateException (bio.terra.workspace.service.job.exception.InvalidResultStateException)2 JobNotCompleteException (bio.terra.workspace.service.job.exception.JobNotCompleteException)2 JobNotFoundException (bio.terra.workspace.service.job.exception.JobNotFoundException)2 JobResponseException (bio.terra.workspace.service.job.exception.JobResponseException)2 ExecutionException (java.util.concurrent.ExecutionException)2 StairwayComponent (bio.terra.common.stairway.StairwayComponent)1 TracingHook (bio.terra.common.stairway.TracingHook)1 Flight (bio.terra.stairway.Flight)1 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)1 FlightFilter (bio.terra.stairway.FlightFilter)1 FlightFilterOp (bio.terra.stairway.FlightFilterOp)1 FlightStatus (bio.terra.stairway.FlightStatus)1