Search in sources :

Example 1 with SingularityExecutorCleanupStatisticsBuilder

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();
}
Also used : Path(java.nio.file.Path) SingularityClientException(com.hubspot.singularity.client.SingularityClientException) TaskCleanupResult(com.hubspot.singularity.executor.task.TaskCleanupResult) SingularityExecutorTaskDefinition(com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition) SingularityExecutorCleanupStatisticsBuilder(com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics.SingularityExecutorCleanupStatisticsBuilder) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) IOException(java.io.IOException) ProcessFailedException(com.hubspot.singularity.runner.base.shared.ProcessFailedException) SingularityClientException(com.hubspot.singularity.client.SingularityClientException) IOException(java.io.IOException)

Aggregations

SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SingularityClientException (com.hubspot.singularity.client.SingularityClientException)1 SingularityExecutorCleanupStatisticsBuilder (com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics.SingularityExecutorCleanupStatisticsBuilder)1 SingularityExecutorTaskDefinition (com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition)1 TaskCleanupResult (com.hubspot.singularity.executor.task.TaskCleanupResult)1 ProcessFailedException (com.hubspot.singularity.runner.base.shared.ProcessFailedException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1