use of com.hubspot.singularity.SingularityPendingTask in project Singularity by HubSpot.
the class SingularityScheduler method deleteScheduledTasks.
private void deleteScheduledTasks(final Collection<SingularityPendingTask> scheduledTasks, SingularityPendingRequest pendingRequest) {
List<SingularityPendingTask> tasksForDeploy = scheduledTasks.stream().filter(task -> pendingRequest.getRequestId().equals(task.getPendingTaskId().getRequestId())).filter(task -> pendingRequest.getDeployId().equals(task.getPendingTaskId().getDeployId())).collect(Collectors.toList());
for (SingularityPendingTask task : tasksForDeploy) {
LOG.debug("Deleting pending task {} in order to reschedule {}", task.getPendingTaskId().getId(), pendingRequest);
taskManager.deletePendingTask(task.getPendingTaskId());
}
}
use of com.hubspot.singularity.SingularityPendingTask in project Singularity by HubSpot.
the class SingularityScheduler method getDueTasks.
@Timed
public List<SingularityTaskRequest> getDueTasks() {
final List<SingularityPendingTask> tasks = taskManager.getPendingTasks();
final long now = System.currentTimeMillis();
final List<SingularityPendingTask> dueTasks = Lists.newArrayListWithCapacity(tasks.size());
for (SingularityPendingTask task : tasks) {
if (task.getPendingTaskId().getNextRunAt() <= now) {
dueTasks.add(task);
}
}
final List<SingularityTaskRequest> dueTaskRequests = taskRequestManager.getTaskRequests(dueTasks);
return checkForStaleScheduledTasks(dueTasks, dueTaskRequests);
}
use of com.hubspot.singularity.SingularityPendingTask in project Singularity by HubSpot.
the class TaskResource method deleteScheduledTask.
public Optional<SingularityPendingTask> deleteScheduledTask(String taskId, SingularityUser user) {
Optional<SingularityPendingTask> maybePendingTask = taskManager.getPendingTask(getPendingTaskIdFromStr(taskId));
if (maybePendingTask.isPresent()) {
SingularityPendingTaskId pendingTaskId = maybePendingTask.get().getPendingTaskId();
Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(pendingTaskId.getRequestId());
checkNotFound(maybeRequest.isPresent(), "Couldn't find: " + taskId);
SingularityRequest request = maybeRequest.get().getRequest();
authorizationHelper.checkForAuthorizationByRequestId(request.getId(), user, SingularityAuthorizationScope.WRITE);
checkBadRequest(request.getRequestType() == RequestType.ON_DEMAND, "Only ON_DEMAND tasks may be deleted.");
taskManager.markPendingTaskForDeletion(pendingTaskId);
}
return maybePendingTask;
}
use of com.hubspot.singularity.SingularityPendingTask 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.SingularityPendingTask in project Singularity by HubSpot.
the class SingularityScheduler method getScheduledTaskIds.
private List<SingularityPendingTask> getScheduledTaskIds(int numMissingInstances, List<SingularityTaskId> matchingTaskIds, SingularityRequest request, RequestState state, SingularityDeployStatistics deployStatistics, String deployId, SingularityPendingRequest pendingRequest, Optional<SingularityPendingDeploy> maybePendingDeploy) {
final Optional<Long> nextRunAt = getNextRunAt(request, state, deployStatistics, pendingRequest, maybePendingDeploy);
if (!nextRunAt.isPresent()) {
return Collections.emptyList();
}
final Set<Integer> inuseInstanceNumbers = Sets.newHashSetWithExpectedSize(matchingTaskIds.size());
for (SingularityTaskId matchingTaskId : matchingTaskIds) {
inuseInstanceNumbers.add(matchingTaskId.getInstanceNo());
}
final List<SingularityPendingTask> newTasks = Lists.newArrayListWithCapacity(numMissingInstances);
int nextInstanceNumber = 1;
for (int i = 0; i < numMissingInstances; i++) {
while (inuseInstanceNumbers.contains(nextInstanceNumber)) {
nextInstanceNumber++;
}
newTasks.add(new SingularityPendingTask(new SingularityPendingTaskId(request.getId(), deployId, nextRunAt.get(), nextInstanceNumber, pendingRequest.getPendingType(), pendingRequest.getTimestamp()), pendingRequest.getCmdLineArgsList(), pendingRequest.getUser(), pendingRequest.getRunId(), pendingRequest.getSkipHealthchecks(), pendingRequest.getMessage(), pendingRequest.getResources(), pendingRequest.getS3UploaderAdditionalFiles(), pendingRequest.getRunAsUserOverride(), pendingRequest.getEnvOverrides(), pendingRequest.getExtraArtifacts(), pendingRequest.getActionId()));
nextInstanceNumber++;
}
return newTasks;
}
Aggregations