Example usage for com.amazonaws.services.ecs AmazonECSClient listContainerInstances

List of usage examples for com.amazonaws.services.ecs AmazonECSClient listContainerInstances

Introduction

In this page you can find the example usage for com.amazonaws.services.ecs AmazonECSClient listContainerInstances.

Prototype

@Override
public ListContainerInstancesResult listContainerInstances(ListContainerInstancesRequest request) 

Source Link

Document

Returns a list of container instances in a specified cluster.

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 {/*  w w w.  j  ava2 s  . 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);
    }
}