this.singularityClient = singularityClient;
this.templateManager = templateManager;
}
public SingularityExecutorCleanupStatistics clean() {
final SingularityExecutorCleanupStatisticsBuilder statisticsBldr = new SingularityExecutorCleanupStatisticsBuilder();
final Path directory = Paths.get(executorConfiguration.getGlobalTaskDefinitionDirectory());
Set<String> runningTaskIds = null;
try {
runningTaskIds = getRunningTaskIds();
} catch (SingularityClientException sce) {
LOG.error("While fetching running tasks from singularity", sce);
statisticsBldr.setErrorMessage(sce.getMessage());
return statisticsBldr.build();
}
LOG.info("Found {} running tasks from Mesos", runningTaskIds);
statisticsBldr.setMesosRunningTasks(runningTaskIds.size());
if (runningTaskIds.isEmpty()) {
if (cleanupConfiguration.isSafeModeWontRunWithNoTasks()) {
final String errorMessage = String.format("Running in safe mode (%s) and found 0 running tasks - aborting cleanup", SingularityExecutorCleanupConfigurationLoader.SAFE_MODE_WONT_RUN_WITH_NO_TASKS);
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 ({})", SingularityExecutorCleanupConfigurationLoader.SAFE_MODE_WONT_RUN_WITH_NO_TASKS);
}
}
for (Path file : JavaUtils.iterable(directory)) {
if (!file.getFileName().toString().endsWith(executorConfiguration.getGlobalTaskDefinitionSuffix())) {
LOG.debug("Ignoring file {} that doesn't have suffix {}", file, executorConfiguration.getGlobalTaskDefinitionSuffix());
statisticsBldr.incrInvalidTasks();
continue;
}
statisticsBldr.incrTotalTaskFiles();
try {
Optional<SingularityExecutorTaskDefinition> taskDefinition = jsonObjectFileHelper.read(file, LOG, SingularityExecutorTaskDefinition.class);
if (!taskDefinition.isPresent()) {
statisticsBldr.incrInvalidTasks();
continue;
}
final String taskId = taskDefinition.get().getTaskId();
if (runningTaskIds.contains(taskId)) {
statisticsBldr.incrRunningTasksIgnored();
continue;
}
Optional<SingularityTaskHistory> taskHistory = null;
try {
taskHistory = singularityClient.getHistoryForTask(taskId);
} catch (SingularityClientException sce) {
LOG.error("While fetching history for {}", taskId, sce);
statisticsBldr.incrErrorTasks();
continue;
}
if (cleanTask(taskDefinition.get(), taskHistory)) {
statisticsBldr.incrSuccessfullyCleanedTasks();
} else {
statisticsBldr.incrErrorTasks();
}
} catch (IOException ioe) {
LOG.error("Couldn't read file {}", file, ioe);
statisticsBldr.incrIoErrorTasks();
}
}
return statisticsBldr.build();
}