Search in sources :

Example 51 with WorkflowTask

use of com.netflix.conductor.common.metadata.workflow.WorkflowTask in project conductor by Netflix.

the class TestDeciderService method createConditionalWF.

private WorkflowDef createConditionalWF() {
    WorkflowTask workflowTask1 = new WorkflowTask();
    workflowTask1.setName("junit_task_1");
    Map<String, Object> inputParams1 = new HashMap<>();
    inputParams1.put("p1", "workflow.input.param1");
    inputParams1.put("p2", "workflow.input.param2");
    workflowTask1.setInputParameters(inputParams1);
    workflowTask1.setTaskReferenceName("t1");
    workflowTask1.setTaskDefinition(new TaskDef("junit_task_1"));
    WorkflowTask workflowTask2 = new WorkflowTask();
    workflowTask2.setName("junit_task_2");
    Map<String, Object> inputParams2 = new HashMap<>();
    inputParams2.put("tp1", "workflow.input.param1");
    workflowTask2.setInputParameters(inputParams2);
    workflowTask2.setTaskReferenceName("t2");
    workflowTask2.setTaskDefinition(new TaskDef("junit_task_2"));
    WorkflowTask workflowTask3 = new WorkflowTask();
    workflowTask3.setName("junit_task_3");
    Map<String, Object> inputParams3 = new HashMap<>();
    inputParams2.put("tp3", "workflow.input.param2");
    workflowTask3.setInputParameters(inputParams3);
    workflowTask3.setTaskReferenceName("t3");
    workflowTask3.setTaskDefinition(new TaskDef("junit_task_3"));
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setName("Conditional Workflow");
    workflowDef.setDescription("Conditional Workflow");
    workflowDef.setInputParameters(Arrays.asList("param1", "param2"));
    WorkflowTask decisionTask2 = new WorkflowTask();
    decisionTask2.setType(TaskType.DECISION.name());
    decisionTask2.setCaseValueParam("case");
    decisionTask2.setName("conditional2");
    decisionTask2.setTaskReferenceName("conditional2");
    Map<String, List<WorkflowTask>> dc = new HashMap<>();
    dc.put("one", Arrays.asList(workflowTask1, workflowTask3));
    dc.put("two", Collections.singletonList(workflowTask2));
    decisionTask2.setDecisionCases(dc);
    decisionTask2.getInputParameters().put("case", "workflow.input.param2");
    WorkflowTask decisionTask = new WorkflowTask();
    decisionTask.setType(TaskType.DECISION.name());
    decisionTask.setCaseValueParam("case");
    decisionTask.setName("conditional");
    decisionTask.setTaskReferenceName("conditional");
    Map<String, List<WorkflowTask>> decisionCases = new HashMap<>();
    decisionCases.put("nested", Collections.singletonList(decisionTask2));
    decisionCases.put("three", Collections.singletonList(workflowTask3));
    decisionTask.setDecisionCases(decisionCases);
    decisionTask.getInputParameters().put("case", "workflow.input.param1");
    decisionTask.getDefaultCase().add(workflowTask2);
    workflowDef.getTasks().add(decisionTask);
    WorkflowTask notifyTask = new WorkflowTask();
    notifyTask.setName("junit_task_4");
    notifyTask.setTaskReferenceName("junit_task_4");
    notifyTask.setTaskDefinition(new TaskDef("junit_task_4"));
    WorkflowTask finalDecisionTask = new WorkflowTask();
    finalDecisionTask.setName("finalcondition");
    finalDecisionTask.setTaskReferenceName("tf");
    finalDecisionTask.setType(TaskType.DECISION.name());
    finalDecisionTask.setCaseValueParam("finalCase");
    Map<String, Object> fi = new HashMap<>();
    fi.put("finalCase", "workflow.input.finalCase");
    finalDecisionTask.setInputParameters(fi);
    finalDecisionTask.getDecisionCases().put("notify", Collections.singletonList(notifyTask));
    workflowDef.getTasks().add(finalDecisionTask);
    return workflowDef;
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 52 with WorkflowTask

use of com.netflix.conductor.common.metadata.workflow.WorkflowTask in project conductor by Netflix.

the class WorkflowExecutor method skipTasksAffectedByTerminateTask.

/**
 * When a TERMINATE task runs, it only affects the workflow in which it runs; it does not do anything with
 * in-progress tasks and subworkflows that are still running. This recursive method will ensure that all tasks within
 * all subworkflows are set to SKIPPED status so they can complete.
 * @param workflow a subworkflow within the hierarchy of the original workflow containing the TERMINATE task
 */
private void skipTasksAffectedByTerminateTask(Workflow workflow) {
    if (!workflow.getStatus().isTerminal()) {
        List<Task> tasksToBeUpdated = new ArrayList<>();
        for (Task workflowTask : workflow.getTasks()) {
            if (!workflowTask.getStatus().isTerminal()) {
                workflowTask.setStatus(SKIPPED);
                tasksToBeUpdated.add(workflowTask);
            }
            if (TaskType.SUB_WORKFLOW.name().equals(workflowTask.getTaskType()) && StringUtils.isNotBlank(workflowTask.getSubWorkflowId())) {
                Workflow subWorkflow = executionDAOFacade.getWorkflowById(workflowTask.getSubWorkflowId(), true);
                if (subWorkflow != null) {
                    skipTasksAffectedByTerminateTask(subWorkflow);
                }
            }
        }
        if (!tasksToBeUpdated.isEmpty()) {
            executionDAOFacade.updateTasks(tasksToBeUpdated);
            workflow.setStatus(Workflow.WorkflowStatus.TERMINATED);
            workflow.setReasonForIncompletion("Parent workflow was terminated with a TERMINATE task");
            executionDAOFacade.updateWorkflow(workflow);
        }
    }
}
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) ArrayList(java.util.ArrayList) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow)

Example 53 with WorkflowTask

use of com.netflix.conductor.common.metadata.workflow.WorkflowTask in project conductor by Netflix.

the class DeciderService method checkForWorkflowCompletion.

@VisibleForTesting
boolean checkForWorkflowCompletion(final Workflow workflow) throws TerminateWorkflowException {
    List<Task> allTasks = workflow.getTasks();
    if (allTasks.isEmpty()) {
        return false;
    }
    if (containsSuccessfulTerminateTask.test(workflow)) {
        return true;
    }
    Map<String, Status> taskStatusMap = new HashMap<>();
    workflow.getTasks().forEach(task -> taskStatusMap.put(task.getReferenceTaskName(), task.getStatus()));
    List<WorkflowTask> workflowTasks = workflow.getWorkflowDefinition().getTasks();
    boolean allCompletedSuccessfully = workflowTasks.stream().parallel().allMatch(wftask -> {
        Status status = taskStatusMap.get(wftask.getTaskReferenceName());
        return status != null && status.isSuccessful() && status.isTerminal();
    });
    boolean noPendingTasks = taskStatusMap.values().stream().allMatch(Status::isTerminal);
    boolean noPendingSchedule = workflow.getTasks().stream().parallel().noneMatch(wftask -> {
        String next = getNextTasksToBeScheduled(workflow, wftask);
        return next != null && !taskStatusMap.containsKey(next);
    });
    return allCompletedSuccessfully && noPendingTasks && noPendingSchedule;
}
Also used : Status(com.netflix.conductor.common.metadata.tasks.Task.Status) WorkflowStatus(com.netflix.conductor.common.run.Workflow.WorkflowStatus) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 54 with WorkflowTask

use of com.netflix.conductor.common.metadata.workflow.WorkflowTask in project conductor by Netflix.

the class DeciderService method decide.

private DeciderOutcome decide(final Workflow workflow, List<Task> preScheduledTasks) throws TerminateWorkflowException {
    DeciderOutcome outcome = new DeciderOutcome();
    if (workflow.getStatus().isTerminal()) {
        // you cannot evaluate a terminal workflow
        LOGGER.debug("Workflow {} is already finished. Reason: {}", workflow, workflow.getReasonForIncompletion());
        return outcome;
    }
    checkWorkflowTimeout(workflow);
    if (workflow.getStatus().equals(WorkflowStatus.PAUSED)) {
        LOGGER.debug("Workflow " + workflow.getWorkflowId() + " is paused");
        return outcome;
    }
    // Filter the list of tasks and include only tasks that are not retried, not executed
    // marked to be skipped and not part of System tasks that is DECISION, FORK, JOIN
    // This list will be empty for a new workflow being started
    List<Task> pendingTasks = workflow.getTasks().stream().filter(isNonPendingTask).collect(Collectors.toList());
    // Get all the tasks that have not completed their lifecycle yet
    // This list will be empty for a new workflow
    Set<String> executedTaskRefNames = workflow.getTasks().stream().filter(Task::isExecuted).map(Task::getReferenceTaskName).collect(Collectors.toSet());
    Map<String, Task> tasksToBeScheduled = new LinkedHashMap<>();
    preScheduledTasks.forEach(preScheduledTask -> {
        tasksToBeScheduled.put(preScheduledTask.getReferenceTaskName(), preScheduledTask);
    });
    // A new workflow does not enter this code branch
    for (Task pendingTask : pendingTasks) {
        if (SystemTaskType.is(pendingTask.getTaskType()) && !pendingTask.getStatus().isTerminal()) {
            tasksToBeScheduled.putIfAbsent(pendingTask.getReferenceTaskName(), pendingTask);
            executedTaskRefNames.remove(pendingTask.getReferenceTaskName());
        }
        Optional<TaskDef> taskDefinition = pendingTask.getTaskDefinition();
        if (!taskDefinition.isPresent()) {
            taskDefinition = Optional.ofNullable(workflow.getWorkflowDefinition().getTaskByRefName(pendingTask.getReferenceTaskName())).map(WorkflowTask::getTaskDefinition);
        }
        if (taskDefinition.isPresent()) {
            checkTaskTimeout(taskDefinition.get(), pendingTask);
            checkTaskPollTimeout(taskDefinition.get(), pendingTask);
            // If the task has not been updated for "responseTimeoutSeconds" then mark task as TIMED_OUT
            if (isResponseTimedOut(taskDefinition.get(), pendingTask)) {
                timeoutTask(taskDefinition.get(), pendingTask);
            }
        }
        if (!pendingTask.getStatus().isSuccessful()) {
            WorkflowTask workflowTask = pendingTask.getWorkflowTask();
            if (workflowTask == null) {
                workflowTask = workflow.getWorkflowDefinition().getTaskByRefName(pendingTask.getReferenceTaskName());
            }
            Optional<Task> retryTask = retry(taskDefinition.orElse(null), workflowTask, pendingTask, workflow);
            if (retryTask.isPresent()) {
                tasksToBeScheduled.put(retryTask.get().getReferenceTaskName(), retryTask.get());
                executedTaskRefNames.remove(retryTask.get().getReferenceTaskName());
                outcome.tasksToBeUpdated.add(pendingTask);
            } else {
                pendingTask.setStatus(COMPLETED_WITH_ERRORS);
            }
        }
        if (!pendingTask.isExecuted() && !pendingTask.isRetried() && pendingTask.getStatus().isTerminal()) {
            pendingTask.setExecuted(true);
            List<Task> nextTasks = getNextTask(workflow, pendingTask);
            if (pendingTask.isLoopOverTask() && !TaskType.DO_WHILE.name().equals(pendingTask.getTaskType()) && !nextTasks.isEmpty()) {
                nextTasks = filterNextLoopOverTasks(nextTasks, pendingTask, workflow);
            }
            nextTasks.forEach(nextTask -> tasksToBeScheduled.putIfAbsent(nextTask.getReferenceTaskName(), nextTask));
            outcome.tasksToBeUpdated.add(pendingTask);
            LOGGER.debug("Scheduling Tasks from {}, next = {} for workflowId: {}", pendingTask.getTaskDefName(), nextTasks.stream().map(Task::getTaskDefName).collect(Collectors.toList()), workflow.getWorkflowId());
        }
    }
    // All the tasks that need to scheduled are added to the outcome, in case of
    List<Task> unScheduledTasks = tasksToBeScheduled.values().stream().filter(task -> !executedTaskRefNames.contains(task.getReferenceTaskName())).collect(Collectors.toList());
    if (!unScheduledTasks.isEmpty()) {
        LOGGER.debug("Scheduling Tasks: {} for workflow: {}", unScheduledTasks.stream().map(Task::getTaskDefName).collect(Collectors.toList()), workflow.getWorkflowId());
        outcome.tasksToBeScheduled.addAll(unScheduledTasks);
    }
    if (containsSuccessfulTerminateTask.test(workflow) || (outcome.tasksToBeScheduled.isEmpty() && checkForWorkflowCompletion(workflow))) {
        LOGGER.debug("Marking workflow: {} as complete.", workflow);
        outcome.isComplete = true;
    }
    return outcome;
}
Also used : TaskUtils(com.netflix.conductor.common.utils.TaskUtils) TaskMapper(com.netflix.conductor.core.execution.mapper.TaskMapper) IDGenerator(com.netflix.conductor.core.utils.IDGenerator) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) LoggerFactory(org.slf4j.LoggerFactory) TaskMapperContext(com.netflix.conductor.core.execution.mapper.TaskMapperContext) HashMap(java.util.HashMap) MetadataDAO(com.netflix.conductor.dao.MetadataDAO) Task(com.netflix.conductor.common.metadata.tasks.Task) StringUtils(org.apache.commons.lang3.StringUtils) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) SUB_WORKFLOW(com.netflix.conductor.common.metadata.workflow.TaskType.SUB_WORKFLOW) COMPLETED_WITH_ERRORS(com.netflix.conductor.common.metadata.tasks.Task.Status.COMPLETED_WITH_ERRORS) ExternalPayloadStorageUtils(com.netflix.conductor.core.utils.ExternalPayloadStorageUtils) Workflow(com.netflix.conductor.common.run.Workflow) IN_PROGRESS(com.netflix.conductor.common.metadata.tasks.Task.Status.IN_PROGRESS) Map(java.util.Map) SKIPPED(com.netflix.conductor.common.metadata.tasks.Task.Status.SKIPPED) Operation(com.netflix.conductor.common.utils.ExternalPayloadStorage.Operation) Named(javax.inject.Named) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Logger(org.slf4j.Logger) WorkflowStatus(com.netflix.conductor.common.run.Workflow.WorkflowStatus) Predicate(java.util.function.Predicate) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Set(java.util.Set) Collectors(java.util.stream.Collectors) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SCHEDULED(com.netflix.conductor.common.metadata.tasks.Task.Status.SCHEDULED) Monitors(com.netflix.conductor.metrics.Monitors) TERMINATE(com.netflix.conductor.common.metadata.workflow.TaskType.TERMINATE) List(java.util.List) TIMED_OUT(com.netflix.conductor.common.metadata.tasks.Task.Status.TIMED_OUT) Optional(java.util.Optional) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Configuration(com.netflix.conductor.core.config.Configuration) PayloadType(com.netflix.conductor.common.utils.ExternalPayloadStorage.PayloadType) Collections(java.util.Collections) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with WorkflowTask

use of com.netflix.conductor.common.metadata.workflow.WorkflowTask in project conductor by Netflix.

the class DeciderService method getTasksToBeScheduled.

public List<Task> getTasksToBeScheduled(Workflow workflow, WorkflowTask taskToSchedule, int retryCount, String retriedTaskId) {
    workflow = populateWorkflowAndTaskData(workflow);
    Map<String, Object> input = parametersUtils.getTaskInput(taskToSchedule.getInputParameters(), workflow, null, null);
    TaskType taskType = TaskType.USER_DEFINED;
    String type = taskToSchedule.getType();
    if (TaskType.isSystemTask(type)) {
        taskType = TaskType.valueOf(type);
    }
    // get tasks already scheduled (in progress/terminal) for  this workflow instance
    List<String> tasksInWorkflow = workflow.getTasks().stream().filter(runningTask -> runningTask.getStatus().equals(Status.IN_PROGRESS) || runningTask.getStatus().isTerminal()).map(Task::getReferenceTaskName).collect(Collectors.toList());
    String taskId = IDGenerator.generate();
    TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder().withWorkflowDefinition(workflow.getWorkflowDefinition()).withWorkflowInstance(workflow).withTaskDefinition(taskToSchedule.getTaskDefinition()).withTaskToSchedule(taskToSchedule).withTaskInput(input).withRetryCount(retryCount).withRetryTaskId(retriedTaskId).withTaskId(taskId).withDeciderService(this).build();
    // for static forks, each branch of the fork creates a join task upon completion
    // for dynamic forks, a join task is created with the fork and also with each branch of the fork
    // a new task must only be scheduled if a task with the same reference name is not already in this workflow instance
    List<Task> tasks = taskMappers.get(taskType.name()).getMappedTasks(taskMapperContext).stream().filter(task -> !tasksInWorkflow.contains(task.getReferenceTaskName())).collect(Collectors.toList());
    tasks.forEach(this::externalizeTaskData);
    return tasks;
}
Also used : TaskUtils(com.netflix.conductor.common.utils.TaskUtils) TaskMapper(com.netflix.conductor.core.execution.mapper.TaskMapper) IDGenerator(com.netflix.conductor.core.utils.IDGenerator) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) LoggerFactory(org.slf4j.LoggerFactory) TaskMapperContext(com.netflix.conductor.core.execution.mapper.TaskMapperContext) HashMap(java.util.HashMap) MetadataDAO(com.netflix.conductor.dao.MetadataDAO) Task(com.netflix.conductor.common.metadata.tasks.Task) StringUtils(org.apache.commons.lang3.StringUtils) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) SUB_WORKFLOW(com.netflix.conductor.common.metadata.workflow.TaskType.SUB_WORKFLOW) COMPLETED_WITH_ERRORS(com.netflix.conductor.common.metadata.tasks.Task.Status.COMPLETED_WITH_ERRORS) ExternalPayloadStorageUtils(com.netflix.conductor.core.utils.ExternalPayloadStorageUtils) Workflow(com.netflix.conductor.common.run.Workflow) IN_PROGRESS(com.netflix.conductor.common.metadata.tasks.Task.Status.IN_PROGRESS) Map(java.util.Map) SKIPPED(com.netflix.conductor.common.metadata.tasks.Task.Status.SKIPPED) Operation(com.netflix.conductor.common.utils.ExternalPayloadStorage.Operation) Named(javax.inject.Named) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Logger(org.slf4j.Logger) WorkflowStatus(com.netflix.conductor.common.run.Workflow.WorkflowStatus) Predicate(java.util.function.Predicate) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Set(java.util.Set) Collectors(java.util.stream.Collectors) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SCHEDULED(com.netflix.conductor.common.metadata.tasks.Task.Status.SCHEDULED) Monitors(com.netflix.conductor.metrics.Monitors) TERMINATE(com.netflix.conductor.common.metadata.workflow.TaskType.TERMINATE) List(java.util.List) TIMED_OUT(com.netflix.conductor.common.metadata.tasks.Task.Status.TIMED_OUT) Optional(java.util.Optional) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Configuration(com.netflix.conductor.core.config.Configuration) PayloadType(com.netflix.conductor.common.utils.ExternalPayloadStorage.PayloadType) Collections(java.util.Collections) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) TaskMapperContext(com.netflix.conductor.core.execution.mapper.TaskMapperContext)

Aggregations

WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)244 Test (org.junit.Test)166 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)148 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)106 Workflow (com.netflix.conductor.common.run.Workflow)106 HashMap (java.util.HashMap)103 Task (com.netflix.conductor.common.metadata.tasks.Task)98 ArrayList (java.util.ArrayList)52 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)44 ConstraintViolation (javax.validation.ConstraintViolation)41 LinkedList (java.util.LinkedList)40 List (java.util.List)36 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)32 Map (java.util.Map)26 SubWorkflowParams (com.netflix.conductor.common.metadata.workflow.SubWorkflowParams)25 DynamicForkJoinTaskList (com.netflix.conductor.common.metadata.workflow.DynamicForkJoinTaskList)18 WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)14 UserTask (com.netflix.conductor.tests.utils.UserTask)14 ValidatorFactory (javax.validation.ValidatorFactory)13 VisibleForTesting (com.google.common.annotations.VisibleForTesting)11