in aws-ecs-server/src/main/kotlin/jetbrains/buildServer/clouds/ecs/EcsCloudImageImpl.kt [173:227]
override fun populateInstances() {
LOG.debug("Populating instances for $name")
try {
val startedBy = startedByTeamCity(serverUUID)
val runningTasks = apiConnector.listRunningTasks(cluster, startedBy).mapNotNull { taskArn -> apiConnector.describeTask(taskArn, cluster) }
val stoppedTasks = apiConnector.listStoppedTasks(cluster, startedBy).mapNotNull { taskArn -> apiConnector.describeTask(taskArn, cluster) }
LOG.debug("Will process ${runningTasks.size} running and ${stoppedTasks.size} stopped tasks")
synchronized(myIdToInstanceMap) {
val keySet = HashSet(myIdToInstanceMap.keys)
val newTasks = ArrayList<EcsTask>()
for (task in runningTasks.union(stoppedTasks)) {
val taskProfileId = task.getOverridenContainerEnv(PROFILE_ID_ECS_ENV)
val taskImageId = task.getOverridenContainerEnv(IMAGE_ID_ECS_ENV)
if(profileId == taskProfileId && taskImageId == id){
val instanceId = task.getOverridenContainerEnv(INSTANCE_ID_ECS_ENV)
if(instanceId == null) {
LOG.warn("Can't resolve cloud instance id of ecs task ${task.arn}")
continue
}
if (keySet.remove(instanceId)) {
val instance = myIdToInstanceMap[instanceId]
if (instance == null) {
LOG.warn("Unable to find instance with id '$instanceId'. Was it removed?")
continue
}
instance.update(task)
} else {
newTasks.add(task)
}
}
}
//remove absent instances
keySet.forEach {
LOG.info("Instance '$it' is no longer available")
myIdToInstanceMap.remove(it)
}
newTasks.forEach{
val instanceId = it.getOverridenContainerEnv(INSTANCE_ID_ECS_ENV)
if (instanceId != null) {
LOG.info("Found new instance '$instanceId'")
myIdToInstanceMap[instanceId] = EcsCloudInstanceImpl(instanceId, this, it, apiConnector)
} else {
LOG.info("Found no instance id for task with arn '${it.arn}'")
}
}
myCurrentError = null
}
} catch (ex: Throwable) {
val msg = "Unable to populate instances for ${imageData.id}"
LOG.warnAndDebugDetails(msg, ex)
myCurrentError = CloudErrorInfo(msg, ex.message.toString(), ex)
}
}