Search in sources :

Example 1 with SingularityTaskState

use of com.hubspot.singularity.SingularityTaskState 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();
}
Also used : SingularityTaskState(com.hubspot.singularity.SingularityTaskState) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Aggregations

SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)1 SingularityPendingTask (com.hubspot.singularity.SingularityPendingTask)1 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)1 SingularityTaskState (com.hubspot.singularity.SingularityTaskState)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1