Search in sources :

Example 41 with TaskResult

use of com.netflix.conductor.common.metadata.tasks.TaskResult in project conductor by Netflix.

the class WorkflowExecutor method executeSystemTask.

// Executes the async system task
public void executeSystemTask(WorkflowSystemTask systemTask, String taskId, int callbackTime) {
    try {
        Task task = executionDAOFacade.getTaskById(taskId);
        if (task == null) {
            LOGGER.error("TaskId: {} could not be found while executing SystemTask", taskId);
            return;
        }
        LOGGER.debug("Task: {} fetched from execution DAO for taskId: {}", task, taskId);
        String queueName = QueueUtils.getQueueName(task);
        if (task.getStatus().isTerminal()) {
            // Tune the SystemTaskWorkerCoordinator's queues - if the queue size is very big this can happen!
            LOGGER.info("Task {}/{} was already completed.", task.getTaskType(), task.getTaskId());
            queueDAO.remove(queueName, task.getTaskId());
            return;
        }
        String workflowId = task.getWorkflowInstanceId();
        Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, true);
        if (task.getStartTime() == 0) {
            task.setStartTime(System.currentTimeMillis());
            Monitors.recordQueueWaitTime(task.getTaskDefName(), task.getQueueWaitTime());
        }
        if (workflow.getStatus().isTerminal()) {
            LOGGER.info("Workflow {} has been completed for {}/{}", workflow.getWorkflowId(), systemTask.getName(), task.getTaskId());
            if (!task.getStatus().isTerminal()) {
                task.setStatus(CANCELED);
            }
            executionDAOFacade.updateTask(task);
            queueDAO.remove(queueName, task.getTaskId());
            return;
        }
        if (task.getStatus().equals(SCHEDULED)) {
            if (executionDAOFacade.exceedsInProgressLimit(task)) {
                // to do add a metric to record this
                LOGGER.warn("Concurrent Execution limited for {}:{}", taskId, task.getTaskDefName());
                // Postpone a message, so that it would be available for poll again.
                queueDAO.postpone(queueName, taskId, task.getWorkflowPriority(), queueTaskMessagePostponeSeconds);
                return;
            }
            if (task.getRateLimitPerFrequency() > 0 && executionDAOFacade.exceedsRateLimitPerFrequency(task, metadataDAO.getTaskDef(task.getTaskDefName()))) {
                LOGGER.warn("RateLimit Execution limited for {}:{}, limit:{}", taskId, task.getTaskDefName(), task.getRateLimitPerFrequency());
                // Postpone a message, so that it would be available for poll again.
                queueDAO.postpone(queueName, taskId, task.getWorkflowPriority(), queueTaskMessagePostponeSeconds);
                return;
            }
        }
        LOGGER.debug("Executing {}/{}-{}", task.getTaskType(), task.getTaskId(), task.getStatus());
        if (task.getStatus() == SCHEDULED || !systemTask.isAsyncComplete(task)) {
            task.setPollCount(task.getPollCount() + 1);
            executionDAOFacade.updateTask(task);
        }
        deciderService.populateTaskData(task);
        // Stop polling for asyncComplete system tasks that are not in SCHEDULED state
        if (systemTask.isAsyncComplete(task) && task.getStatus() != SCHEDULED) {
            queueDAO.remove(QueueUtils.getQueueName(task), task.getTaskId());
            return;
        }
        switch(task.getStatus()) {
            case SCHEDULED:
                systemTask.start(workflow, task, this);
                break;
            case IN_PROGRESS:
                systemTask.execute(workflow, task, this);
                break;
            default:
                break;
        }
        if (!task.getStatus().isTerminal()) {
            task.setCallbackAfterSeconds(callbackTime);
        }
        updateTask(new TaskResult(task));
        LOGGER.debug("Done Executing {}/{}-{} output={}", task.getTaskType(), task.getTaskId(), task.getStatus(), task.getOutputData().toString());
    } catch (Exception e) {
        Monitors.error(className, "executeSystemTask");
        LOGGER.error("Error executing system task - {}, with id: {}", systemTask, taskId, e);
    }
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult)

Aggregations

TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)41 Test (org.junit.Test)29 Task (com.netflix.conductor.common.metadata.tasks.Task)22 Workflow (com.netflix.conductor.common.run.Workflow)17 TaskClient (com.netflix.conductor.client.http.TaskClient)11 Worker (com.netflix.conductor.client.worker.Worker)11 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)8 HashMap (java.util.HashMap)8 StartWorkflowRequest (com.netflix.conductor.common.metadata.workflow.StartWorkflowRequest)7 UserTask (com.netflix.conductor.tests.utils.UserTask)7 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)6 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)4 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)4 Map (java.util.Map)4 ConductorClientException (com.netflix.conductor.client.exceptions.ConductorClientException)2 Action (com.netflix.conductor.common.metadata.events.EventHandler.Action)2 StartWorkflow (com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow)2 TaskDetails (com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails)2