in plugin-azure-server-base/src/main/java/jetbrains/buildServer/clouds/base/tasks/UpdateInstancesTask.java [51:134]
public void run() {
final Map<InstanceStatus, List<String>> instancesByStatus = new HashMap<InstanceStatus, List<String>>();
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) {
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<String>());
}
instancesByStatus.get(realInstanceStatus).add(realInstanceName);
if ((isStatusPermanent(instance.getStatus()) || isStuck(instance))
&& isStatusPermanent(realInstanceStatus)
&& realInstanceStatus != instance.getStatus()
&& !instance.getProvisioningInProgress()) {
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();
for (final G cloudInstance : instances) {
try {
final String instanceName = cloudInstance.getName();
final AbstractInstance instance = realInstances.get(instanceName);
if (instance == null) {
if (cloudInstance.canBeCollected()) {
image.removeInstance(cloudInstance.getInstanceId());
LOG.info(String.format("Removed instance %s(%x) with status %s", cloudInstance.getName(), cloudInstance.hashCode(), cloudInstance.getStatus()));
}
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.toString(), ex);
}
}
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())));
}
}
}