Search in sources :

Example 1 with DeciderOutcome

use of com.netflix.conductor.core.execution.DeciderService.DeciderOutcome 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 2 with DeciderOutcome

use of com.netflix.conductor.core.execution.DeciderService.DeciderOutcome 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 3 with DeciderOutcome

use of com.netflix.conductor.core.execution.DeciderService.DeciderOutcome in project conductor by Netflix.

the class TestDeciderService method testFork.

@Test
public void testFork() throws IOException {
    InputStream stream = TestDeciderService.class.getResourceAsStream("/test.json");
    Workflow workflow = objectMapper.readValue(stream, Workflow.class);
    DeciderOutcome outcome = deciderService.decide(workflow);
    assertFalse(outcome.isComplete);
    assertEquals(5, outcome.tasksToBeScheduled.size());
    assertEquals(1, outcome.tasksToBeUpdated.size());
}
Also used : InputStream(java.io.InputStream) Workflow(com.netflix.conductor.common.run.Workflow) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) Test(org.junit.Test)

Example 4 with DeciderOutcome

use of com.netflix.conductor.core.execution.DeciderService.DeciderOutcome in project conductor by Netflix.

the class TestDeciderOutcomes method testWorkflowWithNoTasks.

@Test
public void testWorkflowWithNoTasks() throws Exception {
    InputStream stream = TestDeciderOutcomes.class.getResourceAsStream("/conditional_flow.json");
    WorkflowDef def = objectMapper.readValue(stream, WorkflowDef.class);
    assertNotNull(def);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(def);
    workflow.setStartTime(0);
    workflow.getInput().put("param1", "nested");
    workflow.getInput().put("param2", "one");
    DeciderOutcome outcome = deciderService.decide(workflow);
    assertNotNull(outcome);
    assertFalse(outcome.isComplete);
    assertTrue(outcome.tasksToBeUpdated.isEmpty());
    assertEquals(3, outcome.tasksToBeScheduled.size());
    System.out.println(outcome.tasksToBeScheduled);
    outcome.tasksToBeScheduled.forEach(t -> t.setStatus(Status.COMPLETED));
    workflow.getTasks().addAll(outcome.tasksToBeScheduled);
    outcome = deciderService.decide(workflow);
    assertFalse(outcome.isComplete);
    assertEquals(outcome.tasksToBeUpdated.toString(), 3, outcome.tasksToBeUpdated.size());
    assertEquals(1, outcome.tasksToBeScheduled.size());
    assertEquals("junit_task_3", outcome.tasksToBeScheduled.get(0).getTaskDefName());
    System.out.println(outcome.tasksToBeScheduled);
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) InputStream(java.io.InputStream) Workflow(com.netflix.conductor.common.run.Workflow) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) Test(org.junit.Test)

Example 5 with DeciderOutcome

use of com.netflix.conductor.core.execution.DeciderService.DeciderOutcome in project conductor by Netflix.

the class TestDeciderOutcomes method testDecisionCases.

@Test
public void testDecisionCases() {
    WorkflowDef def = new WorkflowDef();
    def.setName("test");
    WorkflowTask even = new WorkflowTask();
    even.setName("even");
    even.setType("SIMPLE");
    even.setTaskReferenceName("even");
    even.setTaskDefinition(new TaskDef("even"));
    WorkflowTask odd = new WorkflowTask();
    odd.setName("odd");
    odd.setType("SIMPLE");
    odd.setTaskReferenceName("odd");
    odd.setTaskDefinition(new TaskDef("odd"));
    WorkflowTask defaultt = new WorkflowTask();
    defaultt.setName("defaultt");
    defaultt.setType("SIMPLE");
    defaultt.setTaskReferenceName("defaultt");
    defaultt.setTaskDefinition(new TaskDef("defaultt"));
    WorkflowTask decide = new WorkflowTask();
    decide.setName("decide");
    decide.setWorkflowTaskType(TaskType.DECISION);
    decide.setTaskReferenceName("d0");
    decide.getInputParameters().put("Id", "${workflow.input.Id}");
    decide.getInputParameters().put("location", "${workflow.input.location}");
    decide.setCaseExpression("if ($.Id == null) 'bad input'; else if ( ($.Id != null && $.Id % 2 == 0) || $.location == 'usa') 'even'; else 'odd'; ");
    decide.getDecisionCases().put("even", Arrays.asList(even));
    decide.getDecisionCases().put("odd", Arrays.asList(odd));
    decide.setDefaultCase(Arrays.asList(defaultt));
    def.getTasks().add(decide);
    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(2, outcome.tasksToBeScheduled.size());
    assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
    // default
    assertEquals(defaultt.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
    System.out.println(outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
    assertEquals(Arrays.asList("bad input"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
    workflow.getInput().put("Id", 9);
    workflow.getInput().put("location", "usa");
    outcome = deciderService.decide(workflow);
    assertEquals(2, outcome.tasksToBeScheduled.size());
    assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
    // even because of location == usa
    assertEquals(even.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
    assertEquals(Arrays.asList("even"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
    workflow.getInput().put("Id", 9);
    workflow.getInput().put("location", "canada");
    outcome = deciderService.decide(workflow);
    assertEquals(2, outcome.tasksToBeScheduled.size());
    assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
    // odd
    assertEquals(odd.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
    assertEquals(Arrays.asList("odd"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
}
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) DeciderOutcome(com.netflix.conductor.core.execution.DeciderService.DeciderOutcome) Test(org.junit.Test)

Aggregations

Workflow (com.netflix.conductor.common.run.Workflow)10 DeciderOutcome (com.netflix.conductor.core.execution.DeciderService.DeciderOutcome)10 Test (org.junit.Test)10 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)9 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)8 Task (com.netflix.conductor.common.metadata.tasks.Task)6 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)5 InputStream (java.io.InputStream)4 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)2 TaskType (com.netflix.conductor.common.metadata.workflow.TaskType)2 JsonMapperProvider (com.netflix.conductor.common.utils.JsonMapperProvider)2 Configuration (com.netflix.conductor.core.config.Configuration)2 DecisionTaskMapper (com.netflix.conductor.core.execution.mapper.DecisionTaskMapper)2 DynamicTaskMapper (com.netflix.conductor.core.execution.mapper.DynamicTaskMapper)2 EventTaskMapper (com.netflix.conductor.core.execution.mapper.EventTaskMapper)2 ForkJoinDynamicTaskMapper (com.netflix.conductor.core.execution.mapper.ForkJoinDynamicTaskMapper)2 ForkJoinTaskMapper (com.netflix.conductor.core.execution.mapper.ForkJoinTaskMapper)2