Example usage for com.amazonaws.services.ecs.model ContainerInstance getRemainingResources

List of usage examples for com.amazonaws.services.ecs.model ContainerInstance getRemainingResources

Introduction

In this page you can find the example usage for com.amazonaws.services.ecs.model ContainerInstance getRemainingResources.

Prototype


public java.util.List<Resource> getRemainingResources() 

Source Link

Document

For CPU and memory resource types, this parameter describes the remaining CPU and memory that has not already been allocated to tasks and is therefore available for new tasks.

Usage

From source file:com.cloudbees.jenkins.plugins.amazonecs.ECSService.java

License:Open Source License

void waitForSufficientClusterResources(Date timeout, ECSTaskTemplate template, String clusterArn)
        throws InterruptedException, AbortException {
    AmazonECSClient client = getAmazonECSClient();

    boolean hasEnoughResources = false;
    WHILE: do {//from w w  w . jav  a 2s .  c  o m
        ListContainerInstancesResult listContainerInstances = client
                .listContainerInstances(new ListContainerInstancesRequest().withCluster(clusterArn));
        DescribeContainerInstancesResult containerInstancesDesc = client
                .describeContainerInstances(new DescribeContainerInstancesRequest()
                        .withContainerInstances(listContainerInstances.getContainerInstanceArns())
                        .withCluster(clusterArn));
        LOGGER.log(Level.INFO, "Found {0} instances", containerInstancesDesc.getContainerInstances().size());
        for (ContainerInstance instance : containerInstancesDesc.getContainerInstances()) {
            LOGGER.log(Level.INFO, "Resources found in instance {1}: {0}",
                    new Object[] { instance.getRemainingResources(), instance.getContainerInstanceArn() });
            Resource memoryResource = null;
            Resource cpuResource = null;
            for (Resource resource : instance.getRemainingResources()) {
                if ("MEMORY".equals(resource.getName())) {
                    memoryResource = resource;
                } else if ("CPU".equals(resource.getName())) {
                    cpuResource = resource;
                }
            }

            LOGGER.log(Level.INFO, "Instance {0} has {1}mb of free memory. {2}mb are required",
                    new Object[] { instance.getContainerInstanceArn(), memoryResource.getIntegerValue(),
                            template.getMemory() });
            LOGGER.log(Level.INFO, "Instance {0} has {1} units of free cpu. {2} units are required",
                    new Object[] { instance.getContainerInstanceArn(), cpuResource.getIntegerValue(),
                            template.getCpu() });
            if (memoryResource.getIntegerValue() >= template.getMemory()
                    && cpuResource.getIntegerValue() >= template.getCpu()) {
                hasEnoughResources = true;
                break WHILE;
            }
        }

        // sleep 10s and check memory again
        Thread.sleep(10000);
    } while (!hasEnoughResources && timeout.after(new Date()));

    if (!hasEnoughResources) {
        final String msg = MessageFormat.format(
                "Timeout while waiting for sufficient resources: {0} cpu units, {1}mb free memory",
                template.getCpu(), template.getMemory());
        LOGGER.log(Level.WARNING, msg);
        throw new AbortException(msg);
    }
}