Search in sources :

Example 1 with TaskCleanupType

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

the class SingularityMesosSchedulerImpl method killAndRecord.

public void killAndRecord(SingularityTaskId taskId, Optional<RequestCleanupType> requestCleanupType, Optional<TaskCleanupType> taskCleanupType, Optional<Long> originalTimestamp, Optional<Integer> retries, Optional<String> user) {
    Preconditions.checkState(isRunning());
    Optional<TaskCleanupType> maybeCleanupFromRequestAndTask = getTaskCleanupType(requestCleanupType, taskCleanupType);
    if (maybeCleanupFromRequestAndTask.isPresent() && (maybeCleanupFromRequestAndTask.get() == TaskCleanupType.USER_REQUESTED_DESTROY || maybeCleanupFromRequestAndTask.get() == TaskCleanupType.REQUEST_DELETING)) {
        Optional<SingularityTask> task = taskManager.getTask(taskId);
        if (task.isPresent()) {
            if (task.get().getTaskRequest().getDeploy().getCustomExecutorCmd().isPresent()) {
                byte[] messageBytes = transcoder.toBytes(new SingularityTaskDestroyFrameworkMessage(taskId, user));
                mesosSchedulerClient.frameworkMessage(MesosProtosUtils.toExecutorId(task.get().getMesosTask().getExecutor().getExecutorId()), MesosProtosUtils.toAgentId(task.get().getMesosTask().getAgentId()), messageBytes);
            } else {
                LOG.warn("Not using custom executor, will not send framework message to destroy task");
            }
        } else {
            String message = String.format("No task data available to build kill task framework message for task %s", taskId);
            exceptionNotifier.notify(message);
            LOG.error(message);
        }
    }
    mesosSchedulerClient.kill(TaskID.newBuilder().setValue(taskId.toString()).build());
    taskManager.saveKilledRecord(new SingularityKilledTaskIdRecord(taskId, System.currentTimeMillis(), originalTimestamp.or(System.currentTimeMillis()), requestCleanupType, taskCleanupType, retries.or(-1) + 1));
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityTaskDestroyFrameworkMessage(com.hubspot.singularity.SingularityTaskDestroyFrameworkMessage)

Example 2 with TaskCleanupType

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

the class SingularityCleaner method pause.

private TaskCleanupType pause(SingularityRequestCleanup requestCleanup, Iterable<SingularityTaskId> activeTaskIds) {
    final long start = System.currentTimeMillis();
    boolean killTasks = requestCleanup.getKillTasks().or(configuration.isDefaultValueForKillTasksOfPausedRequests());
    if (requestCleanup.getRunShellCommandBeforeKill().isPresent()) {
        killTasks = false;
    }
    TaskCleanupType cleanupType = killTasks ? TaskCleanupType.PAUSE : TaskCleanupType.PAUSING;
    for (SingularityTaskId taskId : activeTaskIds) {
        LOG.debug("Adding task {} to cleanup (pause)", taskId.getId());
        Optional<SingularityTaskShellCommandRequestId> runBeforeKillId = Optional.absent();
        if (requestCleanup.getRunShellCommandBeforeKill().isPresent()) {
            SingularityTaskShellCommandRequest shellRequest = new SingularityTaskShellCommandRequest(taskId, requestCleanup.getUser(), System.currentTimeMillis(), requestCleanup.getRunShellCommandBeforeKill().get());
            taskManager.saveTaskShellCommandRequestToQueue(shellRequest);
            runBeforeKillId = Optional.of(shellRequest.getId());
        }
        taskManager.createTaskCleanup(new SingularityTaskCleanup(requestCleanup.getUser(), cleanupType, start, taskId, requestCleanup.getMessage(), requestCleanup.getActionId(), runBeforeKillId));
    }
    return cleanupType;
}
Also used : SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 3 with TaskCleanupType

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

the class TaskResource method killTask.

public SingularityTaskCleanup killTask(String taskId, Optional<SingularityKillTaskRequest> killTaskRequest, SingularityUser user) {
    final SingularityTask task = checkActiveTask(taskId, SingularityAuthorizationScope.WRITE, user);
    Optional<String> message = Optional.absent();
    Optional<Boolean> override = Optional.absent();
    Optional<String> actionId = Optional.absent();
    Optional<Boolean> waitForReplacementTask = Optional.absent();
    Optional<SingularityTaskShellCommandRequestId> runBeforeKillId = Optional.absent();
    if (killTaskRequest.isPresent()) {
        actionId = killTaskRequest.get().getActionId();
        message = killTaskRequest.get().getMessage();
        override = killTaskRequest.get().getOverride();
        waitForReplacementTask = killTaskRequest.get().getWaitForReplacementTask();
        if (killTaskRequest.get().getRunShellCommandBeforeKill().isPresent()) {
            SingularityTaskShellCommandRequest shellCommandRequest = startShellCommand(task.getTaskId(), killTaskRequest.get().getRunShellCommandBeforeKill().get(), user);
            runBeforeKillId = Optional.of(shellCommandRequest.getId());
        }
    }
    TaskCleanupType cleanupType = TaskCleanupType.USER_REQUESTED;
    if (waitForReplacementTask.or(Boolean.FALSE)) {
        cleanupType = TaskCleanupType.USER_REQUESTED_TASK_BOUNCE;
        validator.checkActionEnabled(SingularityAction.BOUNCE_TASK);
    } else {
        validator.checkActionEnabled(SingularityAction.KILL_TASK);
    }
    final long now = System.currentTimeMillis();
    final SingularityTaskCleanup taskCleanup;
    if (override.isPresent() && override.get()) {
        LOG.debug("Requested destroy of {}", taskId);
        cleanupType = TaskCleanupType.USER_REQUESTED_DESTROY;
        taskCleanup = new SingularityTaskCleanup(user.getEmail(), cleanupType, now, task.getTaskId(), message, actionId, runBeforeKillId);
        taskManager.saveTaskCleanup(taskCleanup);
    } else {
        taskCleanup = new SingularityTaskCleanup(user.getEmail(), cleanupType, now, task.getTaskId(), message, actionId, runBeforeKillId);
        SingularityCreateResult result = taskManager.createTaskCleanup(taskCleanup);
        if (result == SingularityCreateResult.EXISTED && userRequestedKillTakesPriority(taskId)) {
            taskManager.saveTaskCleanup(taskCleanup);
        } else {
            while (result == SingularityCreateResult.EXISTED) {
                Optional<SingularityTaskCleanup> cleanup = taskManager.getTaskCleanup(taskId);
                if (cleanup.isPresent()) {
                    throw new WebApplicationException(Response.status(Status.CONFLICT).entity(cleanup.get()).type(MediaType.APPLICATION_JSON).build());
                }
                result = taskManager.createTaskCleanup(taskCleanup);
            }
        }
    }
    if (cleanupType == TaskCleanupType.USER_REQUESTED_TASK_BOUNCE) {
        requestManager.addToPendingQueue(new SingularityPendingRequest(task.getTaskId().getRequestId(), task.getTaskId().getDeployId(), now, user.getEmail(), PendingType.TASK_BOUNCE, Optional.<List<String>>absent(), Optional.<String>absent(), Optional.<Boolean>absent(), message, actionId));
    }
    return taskCleanup;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId)

Aggregations

TaskCleanupType (com.hubspot.singularity.TaskCleanupType)3 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)2 SingularityTaskShellCommandRequest (com.hubspot.singularity.SingularityTaskShellCommandRequest)2 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)2 ImmutableList (com.google.common.collect.ImmutableList)1 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityKilledTaskIdRecord (com.hubspot.singularity.SingularityKilledTaskIdRecord)1 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)1 SingularityTaskDestroyFrameworkMessage (com.hubspot.singularity.SingularityTaskDestroyFrameworkMessage)1 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1