use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.
the class TaskTrackerResource method getTaskStateByRunId.
@GET
@Path("/run/{requestId}/{runId}")
@ApiOperation(value = "Get the current state of a task by taskId whether it is pending, active, or inactive")
@ApiResponses({ @ApiResponse(code = 404, message = "Task with this runId does not exist") })
public Optional<SingularityTaskState> getTaskStateByRunId(@Auth SingularityUser user, @PathParam("requestId") String requestId, @PathParam("runId") String runId) {
authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ);
// Check if it's active or inactive
Optional<SingularityTaskId> maybeTaskId = taskManager.getTaskByRunId(requestId, runId);
if (maybeTaskId.isPresent()) {
Optional<SingularityTaskState> maybeTaskState = getTaskStateFromId(maybeTaskId.get());
if (maybeTaskState.isPresent()) {
return maybeTaskState;
}
} else {
Optional<SingularityTaskHistory> maybeTaskHistory = historyManager.getTaskHistoryByRunId(requestId, runId);
if (maybeTaskHistory.isPresent()) {
return Optional.of(SingularityTaskState.fromTaskHistory(maybeTaskHistory.get()));
}
}
// Check if it's pending
for (SingularityPendingTask pendingTask : taskManager.getPendingTasksForRequest(requestId)) {
if (pendingTask.getRunId().isPresent() && pendingTask.getRunId().get().equals(runId)) {
return Optional.of(new SingularityTaskState(Optional.absent(), pendingTask.getPendingTaskId(), pendingTask.getRunId(), Optional.absent(), Collections.emptyList(), true));
}
}
for (SingularityPendingRequest pendingRequest : requestManager.getPendingRequests()) {
if (pendingRequest.getRequestId().equals(requestId) && pendingRequest.getRunId().isPresent() && pendingRequest.getRunId().get().equals(runId)) {
return Optional.of(new SingularityTaskState(Optional.absent(), Optional.absent(), pendingRequest.getRunId(), Optional.absent(), Collections.emptyList(), true));
}
}
return Optional.absent();
}
use of com.hubspot.singularity.SingularityTaskHistory 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();
}
use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.
the class SandboxResource method checkHistory.
private SingularityTaskHistory checkHistory(String taskId, SingularityUser user) {
final SingularityTaskId taskIdObj = getTaskIdObject(taskId);
final SingularityTaskHistory taskHistory = getTaskHistoryRequired(taskIdObj, user);
if (!taskHistory.getDirectory().isPresent()) {
logSupport.checkDirectoryAndContainerId(taskIdObj);
throw badRequest("Task %s does not have a directory yet - check again soon (enqueued request to refetch)", taskId);
}
return taskHistory;
}
use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.
the class SandboxResource method browse.
@GET
@Path("/{taskId}/browse")
@ApiOperation("Retrieve information about a specific task's sandbox.")
public SingularitySandbox browse(@Auth SingularityUser user, @ApiParam("The task ID to browse") @PathParam("taskId") String taskId, @ApiParam("The path to browse from") @QueryParam("path") String path) {
authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
// Remove all trailing slashes from the path
if (path != null) {
path = path.replaceAll("\\/+$", "");
}
final String currentDirectory = getCurrentDirectory(taskId, path);
final SingularityTaskHistory history = checkHistory(taskId, user);
final String slaveHostname = history.getTask().getHostname();
final String pathToRoot = history.getDirectory().get();
final String fullPath = new File(pathToRoot, currentDirectory).toString();
final int substringTruncationLength = currentDirectory.length() == 0 ? pathToRoot.length() + 1 : pathToRoot.length() + currentDirectory.length() + 2;
try {
Collection<MesosFileObject> mesosFiles = sandboxManager.browse(slaveHostname, fullPath);
List<SingularitySandboxFile> sandboxFiles = Lists.newArrayList(Iterables.transform(mesosFiles, new Function<MesosFileObject, SingularitySandboxFile>() {
@Override
public SingularitySandboxFile apply(MesosFileObject input) {
return new SingularitySandboxFile(input.getPath().substring(substringTruncationLength), input.getMtime(), input.getSize(), input.getMode());
}
}));
return new SingularitySandbox(sandboxFiles, pathToRoot, currentDirectory, slaveHostname);
} catch (SlaveNotFoundException snfe) {
throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
}
}
Aggregations