public void run()

in cloud-vmware-server/src/main/java/jetbrains/buildServer/clouds/base/tasks/UpdateInstancesTask.java [61:151]


  public void run() {
    final Map<InstanceStatus, List<String>> instancesByStatus = new HashMap<InstanceStatus, List<String>>();
    try {
      List<T> goodImages = new ArrayList<>();
      myConnector.checkImages(getImages()).forEach((img, errors)->{
        img.updateErrors(errors);
        if (img.getErrorInfo() == null)
          goodImages.add(img);
      });

      final Map<T, Map<String, AbstractInstance>> groupedInstances = myConnector.fetchInstances(goodImages);
      if (LOG.isDebugEnabled()) {
        groupedInstances.forEach((img, instMap) -> {
          LOG.debug(String.format("Instances for [%s]:[%s]", img.getId(), String.join(",", instMap.keySet())));
        });
      }

      for (T image : goodImages) {
        Map<String, AbstractInstance> realInstances = groupedInstances.get(image);
        if (realInstances == null){
          realInstances = Collections.emptyMap();
        }
        for (String realInstanceName : realInstances.keySet()) {
          final G instance = image.findInstanceById(realInstanceName);
          final AbstractInstance realInstance = realInstances.get(realInstanceName);
          if (instance == null) {
            continue;
          }
          final InstanceStatus realInstanceStatus = realInstance.getInstanceStatus();
          if (!instancesByStatus.containsKey(realInstanceStatus)){
            instancesByStatus.put(realInstanceStatus, new ArrayList<String>());
          }
          instancesByStatus.get(realInstanceStatus).add(realInstanceName);

          if ((isStatusPermanent(instance.getStatus()) || isStuck(instance))
                  && isStatusPermanent(realInstanceStatus)
                  && realInstanceStatus != instance.getStatus()) {
            LOG.info(String.format("Updated instance '%s' status to %s based on API information", realInstanceName, realInstanceStatus));
            instance.setStatus(realInstanceStatus);
          }
        }

        final Collection<G> instances = image.getInstances();
        final Set<String> instancesToRemove = new HashSet<>();
        for (final G cloudInstance : instances) {
          try {
            final String instanceName = cloudInstance.getName();
            final AbstractInstance instance = realInstances.get(instanceName);
            if (instance == null) {
              if (cloudInstance.getStatus() != InstanceStatus.SCHEDULED_TO_START && cloudInstance.getStatus() != InstanceStatus.STARTING) {
                instancesToRemove.add(instanceName);
              }
              continue;
            }

            cloudInstance.updateErrors(myConnector.checkInstance(cloudInstance));
            if (instance.getStartDate() != null) {
              cloudInstance.setStartDate(instance.getStartDate());
            }
            if (instance.getIpAddress() != null) {
              cloudInstance.setNetworkIdentify(instance.getIpAddress());
            }
          } catch (Exception ex){
            LOG.warnAndDebugDetails("Error processing VM " + cloudInstance.getName(), ex);
          }
        }
        for (String instanceName : instancesToRemove) {
          AbstractInstance realInstanceNullable = realInstances.get(instanceName);
          final InstanceStatus currentStatus = realInstanceNullable==null ? null : realInstanceNullable.getInstanceStatus();
          if (currentStatus == null) {
            image.removeInstance(instanceName);
          }
        }
        image.detectNewInstances(realInstances);
      }
      myClient.updateErrors();
    } catch (CheckedCloudException e) {
      LOG.warnAndDebugDetails("An error occurred during updateInstanceTask", e);
      if (myRethrowException){
        // for tests
        throw new RuntimeException(e);
      }
    } finally {
      //logging here:
      if (LOG.isDebugEnabled()) {
        for (InstanceStatus instanceStatus : instancesByStatus.keySet()) {
          LOG.debug(String.format("Instances in '%s' status: %s", instanceStatus.getText(), Arrays.toString(instancesByStatus.get(instanceStatus).toArray())));
        }
      }
    }
  }