public void run()

in google-cloud-server-base/src/main/java/jetbrains/buildServer/clouds/base/tasks/UpdateInstancesTask.java [63:159]


  public void run() {
    final Map<InstanceStatus, List<String>> instancesByStatus = new HashMap<>();
    try {
      List<T> goodImages = new ArrayList<>();
      final Collection<T> images = getImages();
      for (final T image : images) {
        image.updateErrors(myConnector.checkImage(image));
        if (image.getErrorInfo() != null) {
          if (!image.isTimedOut()) {
            image.updateErrors((TypedCloudErrorInfo[]) null);
          }
          continue;
        }
        goodImages.add(image);
      }

      final Map<T, Map<String, AbstractInstance>> groupedInstances = myConnector.fetchInstances(goodImages);
      for (Map.Entry<T, Map<String, AbstractInstance>> entry : groupedInstances.entrySet()) {
        LOG.debug(String.format("Instances for [%s]:[%s]", entry.getKey().getId(), StringUtil.join(",", entry.getValue().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<>());
          }
          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();
        List<TypedCloudErrorInfo[]> instanceInsertionErrs = new ArrayList<>(instances.size());
        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) {
                instanceInsertionErrs.add(myConnector.checkStartOperation(cloudInstance));
                image.removeInstance(cloudInstance.getInstanceId());
              }
              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.debug("Error processing VM " + cloudInstance.getName() + ": " + ex);
          }
        }

        TypedCloudErrorInfo[] encounteredErrors = instanceInsertionErrs.stream()
                .flatMap(Arrays::stream)
                .toArray(TypedCloudErrorInfo[]::new);

        if (encounteredErrors.length != 0) {
          image.timeout(TIMEOUT);
          image.updateErrors(encounteredErrors);
        }

        image.detectNewInstances(realInstances);
      }
      myClient.updateErrors();
    } catch (Exception ex) {
      if (myRethrowException) {
        // for tests
        throw new RuntimeException(ex);
      }
      LOG.warn(ex.toString(), ex);
    } finally {
      //logging here:
      for (InstanceStatus instanceStatus : instancesByStatus.keySet()) {
        LOG.debug(String.format("Instances in '%s' status: %s", instanceStatus.getText(), Arrays.toString(instancesByStatus.get(instanceStatus).toArray())));
      }
    }
  }