Search in sources :

Example 1 with HandoutRequestBody

use of bio.terra.buffer.model.HandoutRequestBody in project terra-workspace-manager by DataBiosphere.

the class PullProjectFromPoolStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) {
    try {
        String resourceId = flightContext.getWorkingMap().get(RBS_RESOURCE_ID, String.class);
        logger.info("Preparing to query Buffer Service for resource with ID: " + resourceId);
        HandoutRequestBody body = new HandoutRequestBody();
        body.setHandoutRequestId(resourceId);
        ResourceInfo info = bufferService.handoutResource(body);
        String projectId = info.getCloudResourceUid().getGoogleProjectUid().getProjectId();
        logger.info("Buffer Service returned project with id: " + projectId);
        flightContext.getWorkingMap().put(GCP_PROJECT_ID, projectId);
        return StepResult.getStepResultSuccess();
    } catch (BufferServiceAPIException e) {
        // must retry. Retrying TOO_MANY_REQUESTS gives the service time to recover from load.
        if (e.getStatusCode() == HttpStatus.NOT_FOUND || e.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
        }
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
    } catch (BufferServiceAuthorizationException e) {
        // If authorization fails, there is no recovering
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
    }
}
Also used : ResourceInfo(bio.terra.buffer.model.ResourceInfo) BufferServiceAPIException(bio.terra.workspace.service.buffer.exception.BufferServiceAPIException) HandoutRequestBody(bio.terra.buffer.model.HandoutRequestBody) BufferServiceAuthorizationException(bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException) StepResult(bio.terra.stairway.StepResult)

Example 2 with HandoutRequestBody

use of bio.terra.buffer.model.HandoutRequestBody in project terra-resource-buffer by DataBiosphere.

the class BufferServiceUtils method retryHandout.

/**
 * Retries Handout resource API if resource is no resource is available.
 */
public static String retryHandout(BufferApi bufferApi, String handoutRequestId) throws InterruptedException, ApiException {
    int numAttempts = 1;
    int maxNumAttempts = 5;
    while (numAttempts <= maxNumAttempts) {
        try {
            return bufferApi.handoutResource(new HandoutRequestBody().handoutRequestId(handoutRequestId), POOL_ID).getCloudResourceUid().getGoogleProjectUid().getProjectId();
        } catch (ApiException e) {
            // Only retry when resource is not available (404).
            if (e.getCode() != 404) {
                throw e;
            }
            logger.info("No resource available, retrying... Attempts so far: {}", numAttempts, e);
        }
        ++numAttempts;
        TimeUnit.SECONDS.sleep(5);
    }
    throw new InterruptedException("Exceeds maximum number of retries.");
}
Also used : HandoutRequestBody(bio.terra.buffer.model.HandoutRequestBody) ApiException(bio.terra.buffer.client.ApiException)

Aggregations

HandoutRequestBody (bio.terra.buffer.model.HandoutRequestBody)2 ApiException (bio.terra.buffer.client.ApiException)1 ResourceInfo (bio.terra.buffer.model.ResourceInfo)1 StepResult (bio.terra.stairway.StepResult)1 BufferServiceAPIException (bio.terra.workspace.service.buffer.exception.BufferServiceAPIException)1 BufferServiceAuthorizationException (bio.terra.workspace.service.buffer.exception.BufferServiceAuthorizationException)1