Search in sources :

Example 46 with WorkflowTask

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

the class TestDeciderOutcomes method testOptional.

@Test
public void testOptional() {
    WorkflowDef def = new WorkflowDef();
    def.setName("test");
    WorkflowTask task1 = new WorkflowTask();
    task1.setName("task0");
    task1.setType("SIMPLE");
    task1.setTaskReferenceName("t0");
    task1.getInputParameters().put("taskId", "${CPEWF_TASK_ID}");
    task1.setOptional(true);
    task1.setTaskDefinition(new TaskDef("task0"));
    WorkflowTask task2 = new WorkflowTask();
    task2.setName("task1");
    task2.setType("SIMPLE");
    task2.setTaskReferenceName("t1");
    task2.setTaskDefinition(new TaskDef("task1"));
    def.getTasks().add(task1);
    def.getTasks().add(task2);
    def.setSchemaVersion(2);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(def);
    workflow.setStartTime(System.currentTimeMillis());
    DeciderOutcome outcome = deciderService.decide(workflow);
    assertNotNull(outcome);
    System.out.println("Schedule after starting: " + outcome.tasksToBeScheduled);
    assertEquals(1, outcome.tasksToBeScheduled.size());
    assertEquals(task1.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
    System.out.println("TaskId of the scheduled task in input: " + outcome.tasksToBeScheduled.get(0).getInputData());
    for (int i = 0; i < 3; i++) {
        String task1Id = outcome.tasksToBeScheduled.get(0).getTaskId();
        assertEquals(task1Id, outcome.tasksToBeScheduled.get(0).getInputData().get("taskId"));
        workflow.getTasks().clear();
        workflow.getTasks().addAll(outcome.tasksToBeScheduled);
        workflow.getTasks().get(0).setStatus(Status.FAILED);
        outcome = deciderService.decide(workflow);
        assertNotNull(outcome);
        System.out.println("Schedule: " + outcome.tasksToBeScheduled);
        System.out.println("Update: " + outcome.tasksToBeUpdated);
        assertEquals(1, outcome.tasksToBeUpdated.size());
        assertEquals(1, outcome.tasksToBeScheduled.size());
        assertEquals(Task.Status.FAILED, workflow.getTasks().get(0).getStatus());
        assertEquals(task1Id, outcome.tasksToBeUpdated.get(0).getTaskId());
        assertEquals(task1.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
        assertEquals(i + 1, outcome.tasksToBeScheduled.get(0).getRetryCount());
    }
    String task1Id = outcome.tasksToBeScheduled.get(0).getTaskId();
    workflow.getTasks().clear();
    workflow.getTasks().addAll(outcome.tasksToBeScheduled);
    workflow.getTasks().get(0).setStatus(Status.FAILED);
    outcome = deciderService.decide(workflow);
    assertNotNull(outcome);
    System.out.println("Schedule: " + outcome.tasksToBeScheduled);
    System.out.println("Update: " + outcome.tasksToBeUpdated);
    assertEquals(1, outcome.tasksToBeUpdated.size());
    assertEquals(1, outcome.tasksToBeScheduled.size());
    assertEquals(Task.Status.COMPLETED_WITH_ERRORS, workflow.getTasks().get(0).getStatus());
    assertEquals(task1Id, outcome.tasksToBeUpdated.get(0).getTaskId());
    assertEquals(task2.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) Test(org.junit.Test)

Example 47 with WorkflowTask

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

the class TestDeciderOutcomes method testOptionalWithDynamicFork.

@Test
public void testOptionalWithDynamicFork() {
    WorkflowDef def = new WorkflowDef();
    def.setName("test");
    WorkflowTask task1 = new WorkflowTask();
    task1.setName("fork0");
    task1.setWorkflowTaskType(TaskType.FORK_JOIN_DYNAMIC);
    task1.setTaskReferenceName("fork0");
    task1.setDynamicForkTasksInputParamName("forkedInputs");
    task1.setDynamicForkTasksParam("forks");
    task1.getInputParameters().put("forks", "${workflow.input.forks}");
    task1.getInputParameters().put("forkedInputs", "${workflow.input.forkedInputs}");
    WorkflowTask task2 = new WorkflowTask();
    task2.setName("join0");
    task2.setType("JOIN");
    task2.setTaskReferenceName("join0");
    def.getTasks().add(task1);
    def.getTasks().add(task2);
    def.setSchemaVersion(2);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(def);
    List<WorkflowTask> forks = new LinkedList<>();
    Map<String, Map<String, Object>> forkedInputs = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("f" + i);
        workflowTask.setTaskReferenceName("f" + i);
        workflowTask.setWorkflowTaskType(TaskType.SIMPLE);
        workflowTask.setOptional(true);
        workflowTask.setTaskDefinition(new TaskDef("f" + i));
        forks.add(workflowTask);
        forkedInputs.put(workflowTask.getTaskReferenceName(), new HashMap<>());
    }
    workflow.getInput().put("forks", forks);
    workflow.getInput().put("forkedInputs", forkedInputs);
    workflow.setStartTime(System.currentTimeMillis());
    DeciderOutcome outcome = deciderService.decide(workflow);
    assertNotNull(outcome);
    assertEquals(5, outcome.tasksToBeScheduled.size());
    assertEquals(0, outcome.tasksToBeUpdated.size());
    assertEquals(SystemTaskType.FORK.name(), outcome.tasksToBeScheduled.get(0).getTaskType());
    assertEquals(Task.Status.COMPLETED, outcome.tasksToBeScheduled.get(0).getStatus());
    for (int retryCount = 0; retryCount < 4; retryCount++) {
        for (Task taskToBeScheduled : outcome.tasksToBeScheduled) {
            if (taskToBeScheduled.getTaskDefName().equals("join0")) {
                assertEquals(Task.Status.IN_PROGRESS, taskToBeScheduled.getStatus());
            } else if (taskToBeScheduled.getTaskType().matches("(f0|f1|f2)")) {
                assertEquals(Task.Status.SCHEDULED, taskToBeScheduled.getStatus());
                taskToBeScheduled.setStatus(Status.FAILED);
            }
            taskToBeScheduled.setUpdateTime(System.currentTimeMillis());
        }
        workflow.getTasks().addAll(outcome.tasksToBeScheduled);
        outcome = deciderService.decide(workflow);
        assertNotNull(outcome);
    }
    assertEquals(SystemTaskType.JOIN.name(), outcome.tasksToBeScheduled.get(0).getTaskType());
    for (int i = 0; i < 3; i++) {
        assertEquals(Task.Status.COMPLETED_WITH_ERRORS, outcome.tasksToBeUpdated.get(i).getStatus());
        assertEquals("f" + (i), outcome.tasksToBeUpdated.get(i).getTaskDefName());
    }
    assertEquals(Task.Status.IN_PROGRESS, outcome.tasksToBeScheduled.get(0).getStatus());
    new Join().execute(workflow, outcome.tasksToBeScheduled.get(0), null);
    assertEquals(Task.Status.COMPLETED, outcome.tasksToBeScheduled.get(0).getStatus());
    outcome.tasksToBeScheduled.stream().map(task -> task.getStatus() + ":" + task.getTaskType() + ":").forEach(System.out::println);
    outcome.tasksToBeUpdated.stream().map(task -> task.getStatus() + ":" + task.getTaskType() + ":").forEach(System.out::println);
}
Also used : Arrays(java.util.Arrays) TaskMapper(com.netflix.conductor.core.execution.mapper.TaskMapper) Join(com.netflix.conductor.core.execution.tasks.Join) ForkJoinDynamicTaskMapper(com.netflix.conductor.core.execution.mapper.ForkJoinDynamicTaskMapper) Assert.assertNotSame(org.junit.Assert.assertNotSame) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) MetadataDAO(com.netflix.conductor.dao.MetadataDAO) Task(com.netflix.conductor.common.metadata.tasks.Task) SimpleTaskMapper(com.netflix.conductor.core.execution.mapper.SimpleTaskMapper) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ExternalPayloadStorageUtils(com.netflix.conductor.core.utils.ExternalPayloadStorageUtils) UserDefinedTaskMapper(com.netflix.conductor.core.execution.mapper.UserDefinedTaskMapper) WaitTaskMapper(com.netflix.conductor.core.execution.mapper.WaitTaskMapper) Workflow(com.netflix.conductor.common.run.Workflow) Map(java.util.Map) JoinTaskMapper(com.netflix.conductor.core.execution.mapper.JoinTaskMapper) LinkedList(java.util.LinkedList) Before(org.junit.Before) ForkJoinTaskMapper(com.netflix.conductor.core.execution.mapper.ForkJoinTaskMapper) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) SubWorkflowTaskMapper(com.netflix.conductor.core.execution.mapper.SubWorkflowTaskMapper) HTTPTaskMapper(com.netflix.conductor.core.execution.mapper.HTTPTaskMapper) DecisionTaskMapper(com.netflix.conductor.core.execution.mapper.DecisionTaskMapper) DynamicTaskMapper(com.netflix.conductor.core.execution.mapper.DynamicTaskMapper) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Include(com.fasterxml.jackson.annotation.JsonInclude.Include) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) EventTaskMapper(com.netflix.conductor.core.execution.mapper.EventTaskMapper) Configuration(com.netflix.conductor.core.config.Configuration) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) InputStream(java.io.InputStream) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) Join(com.netflix.conductor.core.execution.tasks.Join) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) HashMap(java.util.HashMap) Map(java.util.Map) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) Test(org.junit.Test)

Example 48 with WorkflowTask

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

the class TestDeciderService method testCheckForWorkflowCompletion.

@Test
public void testCheckForWorkflowCompletion() {
    WorkflowDef conditionalWorkflowDef = createConditionalWF();
    WorkflowTask terminateWT = new WorkflowTask();
    terminateWT.setType(TaskType.TERMINATE.name());
    terminateWT.setTaskReferenceName("terminate");
    terminateWT.setName("terminate");
    terminateWT.getInputParameters().put("terminationStatus", "COMPLETED");
    conditionalWorkflowDef.getTasks().add(terminateWT);
    // when workflow has no tasks
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(conditionalWorkflowDef);
    // then workflow completion check returns false
    assertFalse(deciderService.checkForWorkflowCompletion(workflow));
    // when only part of the tasks are completed
    Task decTask = new Task();
    decTask.setTaskType(TaskType.DECISION.name());
    decTask.setReferenceTaskName("conditional2");
    decTask.setStatus(Status.COMPLETED);
    Task task1 = new Task();
    decTask.setTaskType(TaskType.SIMPLE.name());
    task1.setReferenceTaskName("t1");
    task1.setStatus(Status.COMPLETED);
    workflow.getTasks().addAll(Arrays.asList(decTask, task1));
    // then workflow completion check returns false
    assertFalse(deciderService.checkForWorkflowCompletion(workflow));
    // when the terminate task is COMPLETED
    Task task2 = new Task();
    decTask.setTaskType(TaskType.SIMPLE.name());
    task2.setReferenceTaskName("t2");
    task2.setStatus(Status.SCHEDULED);
    Task terminateTask = new Task();
    decTask.setTaskType(TaskType.TERMINATE.name());
    terminateTask.setReferenceTaskName("terminate");
    terminateTask.setStatus(Status.COMPLETED);
    workflow.getTasks().addAll(Arrays.asList(task2, terminateTask));
    // then the workflow completion check returns true
    assertTrue(deciderService.checkForWorkflowCompletion(workflow));
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 49 with WorkflowTask

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

the class TestDeciderService method testCustomRetryWithWorkflowTask.

@Test
public void testCustomRetryWithWorkflowTask() {
    Workflow workflow = createDefaultWorkflow();
    Task task = new Task();
    task.setStatus(Status.FAILED);
    task.setTaskId("t1");
    task.setStartDelayInSeconds(30);
    TaskDef taskDef = new TaskDef();
    taskDef.setRetryDelaySeconds(60);
    taskDef.setRetryLogic(TaskDef.RetryLogic.CUSTOM);
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.setRetryLogic(RetryLogic.FIXED);
    workflowTask.setStartDelay(80);
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // but workflow task would be preferred which is FIXED (retryDelay will come from workflowTask)
    Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
    assertEquals(80, task2.get().getStartDelayInSeconds());
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // but workflow task would be preferred which is CUSTOM (retryDelay will be 0 since task.retryDelay = -1)
    task2.get().setStartDelayInSeconds(-1);
    workflowTask.setRetryLogic(RetryLogic.CUSTOM);
    workflowTask.setStartDelay(90);
    // Custom retry policy from Workflow,
    Optional<Task> task3 = deciderService.retry(taskDef, workflowTask, task2.get(), workflow);
    assertEquals(0, task3.get().getStartDelayInSeconds());
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // workflow task would be preferred which is CUSTOM (retryDelay will come from the workflowTask,
    // since task.retryDelay = 0)
    task3.get().setStartDelayInSeconds(0);
    Optional<Task> task4 = deciderService.retry(taskDef, workflowTask, task3.get(), workflow);
    assertEquals(90, task4.get().getCallbackAfterSeconds());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 50 with WorkflowTask

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

the class TestDeciderService method createNestedWorkflow.

private WorkflowDef createNestedWorkflow() {
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setName("Nested Workflow");
    workflowDef.setDescription(workflowDef.getName());
    workflowDef.setVersion(1);
    workflowDef.setInputParameters(Arrays.asList("param1", "param2"));
    Map<String, Object> inputParams = new HashMap<>();
    inputParams.put("p1", "workflow.input.param1");
    inputParams.put("p2", "workflow.input.param2");
    List<WorkflowTask> tasks = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setName("junit_task_" + i);
        workflowTask.setInputParameters(inputParams);
        workflowTask.setTaskReferenceName("t" + i);
        workflowTask.setTaskDefinition(new TaskDef("junit_task_" + i));
        tasks.add(workflowTask);
    }
    WorkflowTask decisionTask = new WorkflowTask();
    decisionTask.setType(TaskType.DECISION.name());
    decisionTask.setName("Decision");
    decisionTask.setTaskReferenceName("d1");
    decisionTask.setDefaultCase(Collections.singletonList(tasks.get(8)));
    decisionTask.setCaseValueParam("case");
    Map<String, List<WorkflowTask>> decisionCases = new HashMap<>();
    decisionCases.put("a", Arrays.asList(tasks.get(6), tasks.get(9)));
    decisionCases.put("b", Collections.singletonList(tasks.get(7)));
    decisionTask.setDecisionCases(decisionCases);
    WorkflowDef subWorkflowDef = createLinearWorkflow();
    WorkflowTask subWorkflow = new WorkflowTask();
    subWorkflow.setType(TaskType.SUB_WORKFLOW.name());
    subWorkflow.setName("sw1");
    SubWorkflowParams subWorkflowParams = new SubWorkflowParams();
    subWorkflowParams.setName(subWorkflowDef.getName());
    subWorkflow.setSubWorkflowParam(subWorkflowParams);
    subWorkflow.setTaskReferenceName("sw1");
    WorkflowTask forkTask2 = new WorkflowTask();
    forkTask2.setType(TaskType.FORK_JOIN.name());
    forkTask2.setName("second fork");
    forkTask2.setTaskReferenceName("fork2");
    forkTask2.getForkTasks().add(Arrays.asList(tasks.get(2), tasks.get(4)));
    forkTask2.getForkTasks().add(Arrays.asList(tasks.get(3), decisionTask));
    WorkflowTask joinTask2 = new WorkflowTask();
    joinTask2.setName("join2");
    joinTask2.setType(TaskType.JOIN.name());
    joinTask2.setTaskReferenceName("join2");
    joinTask2.setJoinOn(Arrays.asList("t4", "d1"));
    WorkflowTask forkTask1 = new WorkflowTask();
    forkTask1.setType(TaskType.FORK_JOIN.name());
    forkTask1.setName("fork1");
    forkTask1.setTaskReferenceName("fork1");
    forkTask1.getForkTasks().add(Collections.singletonList(tasks.get(1)));
    forkTask1.getForkTasks().add(Arrays.asList(forkTask2, joinTask2));
    forkTask1.getForkTasks().add(Collections.singletonList(subWorkflow));
    WorkflowTask joinTask1 = new WorkflowTask();
    joinTask1.setName("join1");
    joinTask1.setType(TaskType.JOIN.name());
    joinTask1.setTaskReferenceName("join1");
    joinTask1.setJoinOn(Arrays.asList("t1", "fork2"));
    workflowDef.getTasks().add(forkTask1);
    workflowDef.getTasks().add(joinTask1);
    workflowDef.getTasks().add(tasks.get(5));
    return workflowDef;
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

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