Search in sources :

Example 1 with SingularityTaskMetadata

use of com.hubspot.singularity.SingularityTaskMetadata in project Singularity by HubSpot.

the class TaskManager method getTaskHistory.

public Optional<SingularityTaskHistory> getTaskHistory(SingularityTaskId taskId) {
    final Optional<SingularityTask> task = getTaskCheckCache(taskId, true);
    if (!task.isPresent()) {
        return Optional.absent();
    }
    List<SingularityTaskHistoryUpdate> taskUpdates = getTaskHistoryUpdates(taskId);
    Optional<String> directory = getDirectory(taskId);
    Optional<String> containerId = getContainerId(taskId);
    List<SingularityTaskHealthcheckResult> healthchecks = getHealthcheckResults(taskId);
    List<SingularityLoadBalancerUpdate> loadBalancerUpdates = Lists.newArrayListWithCapacity(2);
    checkLoadBalancerHistory(loadBalancerUpdates, taskId, LoadBalancerRequestType.ADD);
    checkLoadBalancerHistory(loadBalancerUpdates, taskId, LoadBalancerRequestType.REMOVE);
    List<SingularityTaskShellCommandHistory> shellCommandHistory = getTaskShellCommandHistory(taskId);
    List<SingularityTaskMetadata> taskMetadata = getTaskMetadata(taskId);
    return Optional.of(new SingularityTaskHistory(taskUpdates, directory, containerId, healthchecks, task.get(), loadBalancerUpdates, shellCommandHistory, taskMetadata));
}
Also used : SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) SingularityTaskShellCommandHistory(com.hubspot.singularity.SingularityTaskShellCommandHistory) SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskMetadata(com.hubspot.singularity.SingularityTaskMetadata) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory)

Example 2 with SingularityTaskMetadata

use of com.hubspot.singularity.SingularityTaskMetadata in project Singularity by HubSpot.

the class SingularityMailPoller method shouldSendTaskFinishedMail.

private ShouldSendMailState shouldSendTaskFinishedMail(SingularityTaskId taskId, Optional<SingularityRequestWithState> requestWithState, Optional<SingularityTaskHistory> taskHistory) {
    if (!requestWithState.isPresent()) {
        LOG.warn("No request found for {}, can't send task finished email", taskId);
        return ShouldSendMailState.ERROR;
    }
    if (!taskHistory.isPresent()) {
        taskHistory = historyManager.getTaskHistory(taskId.getId());
    }
    if (!taskHistory.isPresent()) {
        LOG.warn("No task history found for {}, can't send task finished email", taskId);
        return ShouldSendMailState.ERROR;
    }
    if (taskMetadataConfiguration.getSendTaskCompletedMailOnceMetadataTypeIsAvailable().isPresent()) {
        for (SingularityTaskMetadata taskMetadata : taskHistory.get().getTaskMetadata()) {
            if (taskMetadata.getType().equals(taskMetadataConfiguration.getSendTaskCompletedMailOnceMetadataTypeIsAvailable().get())) {
                LOG.debug("Sending task finished email for {} because metadata type {} is present", taskId, taskMetadataConfiguration.getSendTaskCompletedMailOnceMetadataTypeIsAvailable().get());
                return ShouldSendMailState.SEND;
            }
        }
    }
    // check to see if it's too soon.
    if (taskMetadataConfiguration.getWaitToSendTaskCompletedMailBufferMillis() > 0) {
        Optional<SingularityTaskHistoryUpdate> lastUpdate = taskHistory.get().getLastTaskUpdate();
        if (!lastUpdate.isPresent()) {
            LOG.warn("Missing last update for {}, can't send task finished email", taskId);
            return ShouldSendMailState.ERROR;
        }
        final long timeSinceLastUpdate = System.currentTimeMillis() - lastUpdate.get().getTimestamp();
        if (timeSinceLastUpdate < taskMetadataConfiguration.getWaitToSendTaskCompletedMailBufferMillis()) {
            LOG.debug("Not sending task finished email for {} because last update was {} ago, buffer is {}", taskId, JavaUtils.durationFromMillis(timeSinceLastUpdate), JavaUtils.durationFromMillis(taskMetadataConfiguration.getWaitToSendTaskCompletedMailBufferMillis()));
            return ShouldSendMailState.WAIT;
        }
    }
    return ShouldSendMailState.SEND;
}
Also used : SingularityTaskMetadata(com.hubspot.singularity.SingularityTaskMetadata) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate)

Example 3 with SingularityTaskMetadata

use of com.hubspot.singularity.SingularityTaskMetadata in project Singularity by HubSpot.

the class TaskResource method postTaskMetadata.

@POST
@Path("/task/{taskId}/metadata")
@ApiOperation(value = "Post metadata about a task that will be persisted along with it and displayed in the UI")
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid metadata object or doesn't match allowed types"), @ApiResponse(code = 404, message = "Task doesn't exist"), @ApiResponse(code = 409, message = "Metadata with this type/timestamp already existed") })
@Consumes({ MediaType.APPLICATION_JSON })
public void postTaskMetadata(@Auth SingularityUser user, @PathParam("taskId") String taskId, final SingularityTaskMetadataRequest taskMetadataRequest) {
    SingularityTaskId taskIdObj = getTaskIdFromStr(taskId);
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.WRITE);
    validator.checkActionEnabled(SingularityAction.ADD_METADATA);
    WebExceptions.checkBadRequest(taskMetadataRequest.getTitle().length() < taskMetadataConfiguration.getMaxMetadataTitleLength(), "Task metadata title too long, must be less than %s bytes", taskMetadataConfiguration.getMaxMetadataTitleLength());
    int messageLength = taskMetadataRequest.getMessage().isPresent() ? taskMetadataRequest.getMessage().get().length() : 0;
    WebExceptions.checkBadRequest(!taskMetadataRequest.getMessage().isPresent() || messageLength < taskMetadataConfiguration.getMaxMetadataMessageLength(), "Task metadata message too long, must be less than %s bytes", taskMetadataConfiguration.getMaxMetadataMessageLength());
    if (taskMetadataConfiguration.getAllowedMetadataTypes().isPresent()) {
        WebExceptions.checkBadRequest(taskMetadataConfiguration.getAllowedMetadataTypes().get().contains(taskMetadataRequest.getType()), "%s is not one of the allowed metadata types %s", taskMetadataRequest.getType(), taskMetadataConfiguration.getAllowedMetadataTypes().get());
    }
    WebExceptions.checkNotFound(taskManager.taskExistsInZk(taskIdObj), "Task %s not found in ZooKeeper (can not save metadata to tasks which have been persisted", taskIdObj);
    final SingularityTaskMetadata taskMetadata = new SingularityTaskMetadata(taskIdObj, System.currentTimeMillis(), taskMetadataRequest.getType(), taskMetadataRequest.getTitle(), taskMetadataRequest.getMessage(), user.getEmail(), taskMetadataRequest.getLevel());
    SingularityCreateResult result = taskManager.saveTaskMetadata(taskMetadata);
    WebExceptions.checkConflict(result == SingularityCreateResult.CREATED, "Task metadata conficted with existing metadata for %s at %s", taskMetadata.getType(), taskMetadata.getTimestamp());
}
Also used : SingularityTaskMetadata(com.hubspot.singularity.SingularityTaskMetadata) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Aggregations

SingularityTaskMetadata (com.hubspot.singularity.SingularityTaskMetadata)3 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)2 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityLoadBalancerUpdate (com.hubspot.singularity.SingularityLoadBalancerUpdate)1 SingularityTask (com.hubspot.singularity.SingularityTask)1 SingularityTaskHealthcheckResult (com.hubspot.singularity.SingularityTaskHealthcheckResult)1 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)1 SingularityTaskShellCommandHistory (com.hubspot.singularity.SingularityTaskShellCommandHistory)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1