Search in sources :

Example 1 with InternalStairwayException

use of bio.terra.service.job.exception.InternalStairwayException in project jade-data-repo by DataBiosphere.

the class JobService method initialize.

/**
 * This method is called from StartupInitializer as part of the sequence of migrating databases
 * and recovering any jobs; i.e., Stairway flights. It lives in this class so that JobService encapsulates
 * all Stairway interaction.
 */
public void initialize() {
    try {
        List<String> recordedStairways;
        boolean weMigrated = false;
        try {
            weMigrated = migrate.migrateDatabase();
            // Initialize stairway - only do the stairway migration if we did the data repo migration
            recordedStairways = stairway.initialize(stairwayJdbcConfiguration.getDataSource(), (weMigrated && migrateConfiguration.getDropAllOnStart()), (weMigrated && migrateConfiguration.getUpdateAllOnStart()));
        } finally {
            if (weMigrated) {
                migrate.releaseMigrateLock();
            }
        }
        // Order is important here. There are two concerns we need to handle:
        // 1. We need to avoid a window where a running pod could get onto the Stairway list, but not be
        // on the pod list. That is why we get the recorded list from Stairway *before* we read the Kubernetes
        // pod list.
        // 2. We want to clean up pods that fail, so we start the kubePodListener as early as possible so we
        // detect pods that get deleted. The Stairway recovery method called by kubePodListener works once
        // Stairway initialization is done, so it is safe to start the listener before we have called
        // Stairway recoveryAndStart
        kubeService.startPodListener(stairway);
        // Lookup all of the stairway instances we know about
        Set<String> existingStairways = kubeService.getApiPodList();
        List<String> obsoleteStairways = new LinkedList<>();
        // Any instances that stairway knows about, but we cannot see are obsolete.
        for (String recordedStairway : recordedStairways) {
            if (!existingStairways.contains(recordedStairway)) {
                obsoleteStairways.add(recordedStairway);
            }
        }
        // Recover and start stairway - step 3 of the stairway startup sequence
        stairway.recoverAndStart(obsoleteStairways);
    } catch (StairwayException stairwayEx) {
        throw new InternalStairwayException("Stairway initialization failed", stairwayEx);
    } catch (InterruptedException ex) {
        throw new JobServiceStartupException("Stairway startup process interrupted", ex);
    }
}
Also used : JobServiceStartupException(bio.terra.service.job.exception.JobServiceStartupException) StairwayException(bio.terra.stairway.exception.StairwayException) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException) LinkedList(java.util.LinkedList) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException)

Example 2 with InternalStairwayException

use of bio.terra.service.job.exception.InternalStairwayException in project jade-data-repo by DataBiosphere.

the class JobService method enumerateJobs.

public List<JobModel> enumerateJobs(int offset, int limit, AuthenticatedUserRequest userReq) {
    boolean canListAnyJob = checkUserCanListAnyJob(userReq);
    // if the user has access to all jobs, then fetch everything
    // otherwise, filter the jobs on the user
    List<FlightState> flightStateList;
    try {
        FlightFilter filter = new FlightFilter();
        if (!canListAnyJob) {
            filter.addFilterInputParameter(JobMapKeys.SUBJECT_ID.getKeyName(), FlightFilterOp.EQUAL, userReq.getSubjectId());
        }
        flightStateList = stairway.getFlights(offset, limit, filter);
    } catch (StairwayException stairwayEx) {
        throw new InternalStairwayException(stairwayEx);
    } catch (InterruptedException ex) {
        throw new JobServiceShutdownException("Job service interrupted", ex);
    }
    List<JobModel> jobModelList = new ArrayList<>();
    for (FlightState flightState : flightStateList) {
        JobModel jobModel = mapFlightStateToJobModel(flightState);
        jobModelList.add(jobModel);
    }
    return jobModelList;
}
Also used : FlightState(bio.terra.stairway.FlightState) ArrayList(java.util.ArrayList) FlightFilter(bio.terra.stairway.FlightFilter) StairwayException(bio.terra.stairway.exception.StairwayException) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException) JobModel(bio.terra.model.JobModel) JobServiceShutdownException(bio.terra.service.job.exception.JobServiceShutdownException) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException)

Example 3 with InternalStairwayException

use of bio.terra.service.job.exception.InternalStairwayException in project jade-data-repo by DataBiosphere.

the class JobService method verifyUserAccess.

private void verifyUserAccess(String jobId, AuthenticatedUserRequest userReq) {
    try {
        FlightState flightState = stairway.getFlightState(jobId);
        FlightMap inputParameters = flightState.getInputParameters();
        String flightSubjectId = inputParameters.get(JobMapKeys.SUBJECT_ID.getKeyName(), String.class);
        if (!StringUtils.equals(flightSubjectId, userReq.getSubjectId())) {
            throw new JobUnauthorizedException("Unauthorized");
        }
    } catch (DatabaseOperationException ex) {
        throw new InternalStairwayException("Stairway exception looking up the job", ex);
    } catch (FlightNotFoundException ex) {
        throw new JobNotFoundException("Job not found", ex);
    } catch (InterruptedException ex) {
        throw new JobServiceShutdownException("Job service interrupted", ex);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) JobUnauthorizedException(bio.terra.service.job.exception.JobUnauthorizedException) JobNotFoundException(bio.terra.service.job.exception.JobNotFoundException) FlightMap(bio.terra.stairway.FlightMap) JobServiceShutdownException(bio.terra.service.job.exception.JobServiceShutdownException) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException)

Aggregations

InternalStairwayException (bio.terra.service.job.exception.InternalStairwayException)3 JobServiceShutdownException (bio.terra.service.job.exception.JobServiceShutdownException)2 FlightState (bio.terra.stairway.FlightState)2 StairwayException (bio.terra.stairway.exception.StairwayException)2 JobModel (bio.terra.model.JobModel)1 JobNotFoundException (bio.terra.service.job.exception.JobNotFoundException)1 JobServiceStartupException (bio.terra.service.job.exception.JobServiceStartupException)1 JobUnauthorizedException (bio.terra.service.job.exception.JobUnauthorizedException)1 FlightFilter (bio.terra.stairway.FlightFilter)1 FlightMap (bio.terra.stairway.FlightMap)1 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)1 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1