use of com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics.SingularityExecutorCleanupStatisticsBuilder in project Singularity by HubSpot.
the class SingularityExecutorCleanup method clean.
public SingularityExecutorCleanupStatistics clean() {
final SingularityExecutorCleanupStatisticsBuilder statisticsBldr = new SingularityExecutorCleanupStatisticsBuilder();
final Path directory = Paths.get(executorConfiguration.getGlobalTaskDefinitionDirectory());
Set<String> runningTaskIds = null;
try {
runningTaskIds = getRunningTaskIds();
} catch (Exception e) {
LOG.error("While fetching running tasks from singularity", e);
exceptionNotifier.notify(String.format("Error fetching running tasks (%s)", e.getMessage()), e, Collections.<String, String>emptyMap());
statisticsBldr.setErrorMessage(e.getMessage());
return statisticsBldr.build();
}
LOG.info("Found {} running tasks from Mesos", runningTaskIds);
statisticsBldr.setMesosRunningTasks(runningTaskIds.size());
if (runningTaskIds.isEmpty()) {
if (!isDecommissioned()) {
if (cleanupConfiguration.isSafeModeWontRunWithNoTasks()) {
final String errorMessage = "Running in safe mode and found 0 running tasks - aborting cleanup";
LOG.error(errorMessage);
statisticsBldr.setErrorMessage(errorMessage);
return statisticsBldr.build();
} else {
LOG.warn("Found 0 running tasks - proceeding with cleanup as we are not in safe mode");
}
} else {
if (!cleanupConfiguration.isCleanTasksWhenDecommissioned()) {
return statisticsBldr.build();
}
}
}
if (cleanupConfiguration.isRunDockerCleanup()) {
cleanDocker(runningTaskIds);
}
for (Path file : JavaUtils.iterable(directory)) {
if (!Objects.toString(file.getFileName()).endsWith(executorConfiguration.getGlobalTaskDefinitionSuffix())) {
LOG.debug("Ignoring file {} that doesn't have suffix {}", file, executorConfiguration.getGlobalTaskDefinitionSuffix());
statisticsBldr.incrInvalidTasks();
continue;
}
statisticsBldr.incrTotalTaskFiles();
try {
Optional<SingularityExecutorTaskDefinition> maybeTaskDefinition = jsonObjectFileHelper.read(file, LOG, SingularityExecutorTaskDefinition.class);
if (!maybeTaskDefinition.isPresent()) {
statisticsBldr.incrInvalidTasks();
continue;
}
SingularityExecutorTaskDefinition taskDefinition = withDefaults(maybeTaskDefinition.get());
final String taskId = taskDefinition.getTaskId();
LOG.info("{} - Starting possible cleanup", taskId);
if (runningTaskIds.contains(taskId) || executorStillRunning(taskDefinition)) {
statisticsBldr.incrRunningTasksIgnored();
continue;
}
Optional<SingularityTaskHistory> taskHistory = null;
try {
taskHistory = singularityClient.getHistoryForTask(taskId);
} catch (SingularityClientException sce) {
LOG.error("{} - Failed fetching history", taskId, sce);
exceptionNotifier.notify(String.format("Error fetching history (%s)", sce.getMessage()), sce, ImmutableMap.<String, String>of("taskId", taskId));
statisticsBldr.incrErrorTasks();
continue;
}
TaskCleanupResult result = cleanTask(taskDefinition, taskHistory);
LOG.info("{} - {}", taskId, result);
switch(result) {
case ERROR:
statisticsBldr.incrErrorTasks();
break;
case SUCCESS:
statisticsBldr.incrSuccessfullyCleanedTasks();
break;
case WAITING:
statisticsBldr.incrWaitingTasks();
break;
default:
break;
}
} catch (IOException ioe) {
LOG.error("Couldn't read file {}", file, ioe);
exceptionNotifier.notify(String.format("Error reading file (%s)", ioe.getMessage()), ioe, ImmutableMap.of("file", file.toString()));
statisticsBldr.incrIoErrorTasks();
}
}
return statisticsBldr.build();
}
Aggregations