private synchronized boolean addProvisionedSlave(DockerSlaveTemplate template) throws Exception {
  final DockerContainerLifecycle dockerCreateContainer=template.getDockerContainerLifecycle();
  String dockerImageName=dockerCreateContainer.getImage();
  int templateCapacity=template.getMaxCapacity();
  int estimatedTotalSlaves=countCurrentDockerSlaves(null);
  int estimatedAmiSlaves=countCurrentDockerSlaves(template);
synchronized (provisionedImages) {
    int currentProvisioning=0;
    if (provisionedImages.containsKey(template)) {
      currentProvisioning=provisionedImages.get(template);
    }
    for (    int amiCount : provisionedImages.values()) {
      estimatedTotalSlaves+=amiCount;
    }
    estimatedAmiSlaves+=currentProvisioning;
    if (estimatedTotalSlaves >= getContainerCap()) {
      LOG.info("Not Provisioning '{}'; Server '{}' full with '{}' container(s)",dockerImageName,name,getContainerCap());
      return false;
    }
    if (templateCapacity != 0 && estimatedAmiSlaves >= templateCapacity) {
      LOG.info("Not Provisioning '{}'. Instance limit of '{}' reached on server '{}'",dockerImageName,templateCapacity,name);
      return false;
    }
    LOG.info("Provisioning '{}' number '{}' on '{}'; Total containers: '{}'",dockerImageName,estimatedAmiSlaves,name,estimatedTotalSlaves);
    provisionedImages.put(template,currentProvisioning + 1);
    return true;
  }
}

private synchronized boolean addProvisionedSlave ( DockerSlaveTemplate template ) throws Exception { final DockerContainerLifecycle dockerCreateContainer = template . getDockerContainerLifecycle ( ) ; String dockerImageName = dockerCreateContainer . getImage ( ) ; int templateCapacity = template . getMaxCapacity ( ) ; int estimatedTotalSlaves = countCurrentDockerSlaves ( null ) ; int estimatedAmiSlaves = countCurrentDockerSlaves ( template ) ; synchronized ( provisionedImages ) { int currentProvisioning = 0 ; if ( provisionedImages . containsKey ( template ) ) { currentProvisioning = provisionedImages . get ( template ) ; } for ( int amiCount : provisionedImages . values ( ) ) { estimatedTotalSlaves += amiCount ; } estimatedAmiSlaves += currentProvisioning ; if ( estimatedTotalSlaves >= getContainerCap ( ) ) { LOG . info ( "Not Provisioning '{}'; Server '{}' full with '{}' container(s)" , dockerImageName , name , getContainerCap ( ) ) ; return false ; } if ( templateCapacity != 0 && estimatedAmiSlaves >= templateCapacity ) { LOG . info ( "Not Provisioning '{}'. Instance limit of '{}' reached on server '{}'" , dockerImageName , templateCapacity , name ) ; return false ; } LOG . info ( "Provisioning '{}' number '{}' on '{}'; Total containers: '{}'" , dockerImageName , estimatedAmiSlaves , name , estimatedTotalSlaves ) ; provisionedImages . put ( template , currentProvisioning + 1 ) ; return true ; } }
check not too many already running .
