Search in sources :

Example 1 with NotFoundException

use of bio.terra.buffer.common.exception.NotFoundException in project terra-resource-buffer by DataBiosphere.

the class PoolService method handoutResourceTransactionally.

/**
 * Process handout resource in on transaction (anything failure will cause database rollback).
 */
private Resource handoutResourceTransactionally(PoolId poolId, RequestHandoutId requestHandoutId) {
    Optional<Pool> pool = bufferDao.retrievePool(poolId);
    if (pool.isEmpty() || !pool.get().status().equals(PoolStatus.ACTIVE)) {
        throw new BadRequestException(String.format("Invalid pool id: %s.", poolId));
    }
    try {
        // Retry 20 times of 2 seconds each.
        Optional<Resource> resource = executeAndRetry(() -> bufferDao.updateOneReadyResourceToHandedOut(poolId, requestHandoutId), Duration.ofSeconds(2), 20);
        Resource result = resource.orElseThrow(() -> new NotFoundException(String.format("No resource is ready to use at this moment for pool: %s. Please try later", poolId)));
        logger.info("Handed out resource ID {}, Handout ID {}, Pool ID {}", result.cloudResourceUid(), result.requestHandoutId(), poolId);
        return result;
    } catch (InterruptedException | DataAccessException e) {
        throw new InternalServerErrorException(String.format("Failed to update one resource state from READY to HANDED_OUT for pool %s", poolId));
    }
}
Also used : Resource(bio.terra.buffer.common.Resource) BadRequestException(bio.terra.common.exception.BadRequestException) NotFoundException(bio.terra.buffer.common.exception.NotFoundException) InternalServerErrorException(bio.terra.common.exception.InternalServerErrorException) Pool(bio.terra.buffer.common.Pool) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with NotFoundException

use of bio.terra.buffer.common.exception.NotFoundException in project terra-resource-buffer by DataBiosphere.

the class PoolService method getPoolInfo.

/**
 * Gets pool information by given {@link PoolId}.
 */
public PoolInfo getPoolInfo(PoolId poolId) {
    Optional<PoolAndResourceStates> poolAndResourceStates = bufferDao.retrievePoolAndResourceStatesById(poolId);
    if (poolAndResourceStates.isEmpty()) {
        throw new NotFoundException(String.format("Pool %s not found", poolId));
    }
    Pool pool = poolAndResourceStates.get().pool();
    Multiset<ResourceState> resourceStates = poolAndResourceStates.get().resourceStates();
    return new PoolInfo().poolConfig(new PoolConfig().poolId(poolId.toString()).size(pool.size()).resourceConfigName(pool.resourceConfig().getConfigName())).status(bio.terra.buffer.generated.model.PoolStatus.valueOf(pool.status().toString())).putResourceStateCountItem(ResourceState.CREATING.name(), resourceStates.count(ResourceState.CREATING)).putResourceStateCountItem(ResourceState.READY.name(), resourceStates.count(ResourceState.READY)).putResourceStateCountItem(ResourceState.DELETED.name(), resourceStates.count(ResourceState.DELETED)).putResourceStateCountItem(ResourceState.HANDED_OUT.name(), resourceStates.count(ResourceState.HANDED_OUT));
}
Also used : PoolAndResourceStates(bio.terra.buffer.common.PoolAndResourceStates) NotFoundException(bio.terra.buffer.common.exception.NotFoundException) PoolConfig(bio.terra.buffer.generated.model.PoolConfig) PoolConfigLoader.loadPoolConfig(bio.terra.buffer.service.pool.PoolConfigLoader.loadPoolConfig) Pool(bio.terra.buffer.common.Pool) ResourceState(bio.terra.buffer.common.ResourceState) PoolInfo(bio.terra.buffer.generated.model.PoolInfo)

Aggregations

Pool (bio.terra.buffer.common.Pool)2 NotFoundException (bio.terra.buffer.common.exception.NotFoundException)2 PoolAndResourceStates (bio.terra.buffer.common.PoolAndResourceStates)1 Resource (bio.terra.buffer.common.Resource)1 ResourceState (bio.terra.buffer.common.ResourceState)1 PoolConfig (bio.terra.buffer.generated.model.PoolConfig)1 PoolInfo (bio.terra.buffer.generated.model.PoolInfo)1 PoolConfigLoader.loadPoolConfig (bio.terra.buffer.service.pool.PoolConfigLoader.loadPoolConfig)1 BadRequestException (bio.terra.common.exception.BadRequestException)1 InternalServerErrorException (bio.terra.common.exception.InternalServerErrorException)1 DataAccessException (org.springframework.dao.DataAccessException)1