Search in sources :

Example 1 with SingularityExecutorTaskDefinition

use of com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition 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)

Example 2 with SingularityExecutorTaskDefinition

use of com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition in project Singularity by HubSpot.

the class SingularityExecutorTaskBuilder method buildTask.

public SingularityExecutorTask buildTask(String taskId, ExecutorDriver driver, TaskInfo taskInfo, Logger log) {
    SingularityTaskExecutorData taskExecutorData = readExecutorData(jsonObjectMapper, taskInfo);
    SingularityExecutorTaskDefinition taskDefinition = new SingularityExecutorTaskDefinition(taskId, taskExecutorData, MesosUtils.getTaskDirectoryPath(taskId).toString(), executorPid, taskExecutorData.getServiceLog(), Files.getFileExtension(taskExecutorData.getServiceLog()), taskExecutorData.getServiceFinishedTailLog(), executorConfiguration.getTaskAppDirectory(), executorConfiguration.getExecutorBashLog(), executorConfiguration.getLogrotateStateFile(), executorConfiguration.getSignatureVerifyOut());
    jsonObjectFileHelper.writeObject(taskDefinition, executorConfiguration.getTaskDefinitionPath(taskId), log);
    return new SingularityExecutorTask(driver, executorUtils, baseConfiguration, executorConfiguration, taskDefinition, executorPid, artifactFetcher, taskInfo, templateManager, log, jsonObjectFileHelper, dockerUtils, s3Configuration, jsonObjectMapper);
}
Also used : SingularityTaskExecutorData(com.hubspot.singularity.SingularityTaskExecutorData) SingularityExecutorTaskDefinition(com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition) SingularityExecutorTask(com.hubspot.singularity.executor.task.SingularityExecutorTask)

Aggregations

SingularityExecutorTaskDefinition (com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition)2 SingularityTaskExecutorData (com.hubspot.singularity.SingularityTaskExecutorData)1 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SingularityClientException (com.hubspot.singularity.client.SingularityClientException)1 SingularityExecutorCleanupStatisticsBuilder (com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics.SingularityExecutorCleanupStatisticsBuilder)1 SingularityExecutorTask (com.hubspot.singularity.executor.task.SingularityExecutorTask)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