Search in sources :

Example 11 with SubWorkflowParams

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

the class WorkflowServiceTest method createWorkflowWithInlineSubWorkflowWithInlineSubWorkflow.

private void createWorkflowWithInlineSubWorkflowWithInlineSubWorkflow() {
    WorkflowDef subWorkflowDef = createWorkflowWithInlineSubWorkflow();
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.setName("subWorkflowTask");
    workflowTask.setType(SUB_WORKFLOW.name());
    SubWorkflowParams swp = new SubWorkflowParams();
    swp.setName("dummy-name");
    swp.setWorkflowDef(subWorkflowDef);
    workflowTask.setSubWorkflowParam(swp);
    Map<String, Object> input = new HashMap<>();
    input.put("test", "test value");
    input.put("param1", "sub workflow task input param1");
    input.put("param2", 21);
    workflowTask.setInputParameters(input);
    workflowTask.setTaskReferenceName("sw2");
    WorkflowDef main = new WorkflowDef();
    main.setSchemaVersion(2);
    main.setInputParameters(Arrays.asList("param1", "param2"));
    main.setName(WF_WITH_INLINE_SUB_WF_WITH_INLINE_SUB_WF);
    main.getTasks().add(workflowTask);
    Map<String, Object> outputParameters = new HashMap<>();
    outputParameters.put("o1", "${workflow.input.param1}");
    outputParameters.put("o2", "${sw2.output.o2}");
    main.setOutputParameters(outputParameters);
    metadataService.updateWorkflowDef(Collections.singletonList(main));
}
Also used : SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask)

Example 12 with SubWorkflowParams

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

the class AbstractWorkflowServiceTest method testTerminateTaskInASubworkflow.

/**
 * This test verifies that a Subworkflow with Terminate task calls decide on parent, and helps progress it immediately.
 */
@Test
public void testTerminateTaskInASubworkflow() {
    WorkflowDef subWorkflowDef = new WorkflowDef();
    subWorkflowDef.setName("test_terminate_task_wf");
    subWorkflowDef.setSchemaVersion(2);
    subWorkflowDef.setVersion(1);
    Map<String, Object> lambdaTaskInputParams = new HashMap<>();
    lambdaTaskInputParams.put("input", "${workflow.input}");
    lambdaTaskInputParams.put("scriptExpression", "if ($.input.a==1){return {testvalue: true}} else{return {testvalue: false}}");
    WorkflowTask lambdaWorkflowTask = new WorkflowTask();
    lambdaWorkflowTask.setWorkflowTaskType(TaskType.LAMBDA);
    lambdaWorkflowTask.setName("lambda");
    lambdaWorkflowTask.setInputParameters(lambdaTaskInputParams);
    lambdaWorkflowTask.setTaskReferenceName("lambda0");
    Map<String, Object> terminateTaskInputParams = new HashMap<>();
    terminateTaskInputParams.put(Terminate.getTerminationStatusParameter(), "COMPLETED");
    terminateTaskInputParams.put(Terminate.getTerminationWorkflowOutputParameter(), "${lambda0.output}");
    WorkflowTask terminateWorkflowTask = new WorkflowTask();
    terminateWorkflowTask.setType(TaskType.TASK_TYPE_TERMINATE);
    terminateWorkflowTask.setName("terminate");
    terminateWorkflowTask.setInputParameters(terminateTaskInputParams);
    terminateWorkflowTask.setTaskReferenceName("terminate0");
    WorkflowTask workflowTask2 = new WorkflowTask();
    workflowTask2.setName("junit_task_2");
    workflowTask2.setTaskReferenceName("t2");
    subWorkflowDef.getTasks().addAll(Arrays.asList(lambdaWorkflowTask, terminateWorkflowTask, workflowTask2));
    assertNotNull(subWorkflowDef);
    metadataService.registerWorkflowDef(subWorkflowDef);
    // Create Parent workflow
    WorkflowDef parentWorkflowDef = new WorkflowDef();
    parentWorkflowDef.setName("test_parent_wf_for_terminate_task_subwf");
    parentWorkflowDef.setSchemaVersion(2);
    WorkflowTask subWorkflowTask = new WorkflowTask();
    subWorkflowTask.setWorkflowTaskType(SUB_WORKFLOW);
    subWorkflowTask.setName("subWF");
    subWorkflowTask.setTaskReferenceName("subWF");
    SubWorkflowParams subWorkflowParams = new SubWorkflowParams();
    subWorkflowParams.setName(subWorkflowDef.getName());
    subWorkflowParams.setVersion(subWorkflowDef.getVersion());
    subWorkflowTask.setSubWorkflowParam(subWorkflowParams);
    parentWorkflowDef.getTasks().addAll(Arrays.asList(subWorkflowTask));
    assertNotNull(parentWorkflowDef);
    metadataService.registerWorkflowDef(parentWorkflowDef);
    Map wfInput = Collections.singletonMap("a", 1);
    String workflowId = startOrLoadWorkflowExecution(parentWorkflowDef.getName(), parentWorkflowDef.getVersion(), "", wfInput, null, null);
    Workflow workflow = workflowExecutor.getWorkflow(workflowId, true);
    assertNotNull(workflow);
    assertEquals(1, workflow.getTasks().size());
    SubWorkflow subWorkflowSystemTask = new SubWorkflow();
    // Simulating SystemTaskWorkerCoordinator to execute async system tasks
    String subWorkflowTaskId = workflow.getTaskByRefName("subWF").getTaskId();
    workflowExecutor.executeSystemTask(subWorkflowSystemTask, subWorkflowTaskId, 1);
    workflow = workflowExecutionService.getExecutionStatus(workflowId, true);
    Task task = workflow.getTaskByRefName("subWF");
    Workflow subWorkflow = workflowExecutionService.getExecutionStatus(task.getSubWorkflowId(), true);
    assertNotNull(workflow);
    assertNotNull(task);
    assertEquals(COMPLETED, task.getStatus());
    assertNotNull(subWorkflow);
    assertEquals("tasks:" + workflow.getTasks(), WorkflowStatus.COMPLETED, workflow.getStatus());
    assertEquals("tasks:" + subWorkflow.getTasks(), WorkflowStatus.COMPLETED, subWorkflow.getStatus());
    assertEquals(TaskType.TASK_TYPE_LAMBDA, subWorkflow.getTasks().get(0).getTaskType());
    assertEquals(TaskType.TASK_TYPE_TERMINATE, subWorkflow.getTasks().get(1).getTaskType());
    assertEquals(subWorkflow.getTasks().get(1).getOutputData(), subWorkflow.getOutput());
    assertEquals(SUB_WORKFLOW.name(), workflow.getTasks().get(0).getTaskType());
    metadataService.unregisterWorkflowDef(parentWorkflowDef.getName(), parentWorkflowDef.getVersion());
    metadataService.unregisterWorkflowDef(subWorkflowDef.getName(), subWorkflowDef.getVersion());
}
Also used : SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) UserTask(com.netflix.conductor.tests.utils.UserTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with SubWorkflowParams

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

the class SubWorkflowTaskMapper method getMappedTasks.

@Override
public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) {
    logger.debug("TaskMapperContext {} in SubWorkflowTaskMapper", taskMapperContext);
    WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule();
    Workflow workflowInstance = taskMapperContext.getWorkflowInstance();
    String taskId = taskMapperContext.getTaskId();
    // Check if the are sub workflow parameters, if not throw an exception, cannot initiate a sub-workflow without workflow params
    SubWorkflowParams subWorkflowParams = getSubWorkflowParams(taskToSchedule);
    Map<String, Object> resolvedParams = getSubWorkflowInputParameters(workflowInstance, subWorkflowParams);
    String subWorkflowName = resolvedParams.get("name").toString();
    Integer subWorkflowVersion = getSubWorkflowVersion(resolvedParams, subWorkflowName);
    Object subWorkflowDefinition = resolvedParams.get("workflowDefinition");
    Map subWorkflowTaskToDomain = null;
    Object uncheckedTaskToDomain = resolvedParams.get("taskToDomain");
    if (uncheckedTaskToDomain instanceof Map) {
        subWorkflowTaskToDomain = (Map) uncheckedTaskToDomain;
    }
    Task subWorkflowTask = new Task();
    subWorkflowTask.setTaskType(SubWorkflow.NAME);
    subWorkflowTask.setTaskDefName(taskToSchedule.getName());
    subWorkflowTask.setReferenceTaskName(taskToSchedule.getTaskReferenceName());
    subWorkflowTask.setWorkflowInstanceId(workflowInstance.getWorkflowId());
    subWorkflowTask.setWorkflowType(workflowInstance.getWorkflowName());
    subWorkflowTask.setCorrelationId(workflowInstance.getCorrelationId());
    subWorkflowTask.setScheduledTime(System.currentTimeMillis());
    subWorkflowTask.getInputData().put("subWorkflowName", subWorkflowName);
    subWorkflowTask.getInputData().put("subWorkflowVersion", subWorkflowVersion);
    subWorkflowTask.getInputData().put("subWorkflowTaskToDomain", subWorkflowTaskToDomain);
    subWorkflowTask.getInputData().put("subWorkflowDefinition", subWorkflowDefinition);
    subWorkflowTask.getInputData().put("workflowInput", taskMapperContext.getTaskInput());
    subWorkflowTask.setTaskId(taskId);
    subWorkflowTask.setStatus(Task.Status.SCHEDULED);
    subWorkflowTask.setWorkflowTask(taskToSchedule);
    subWorkflowTask.setWorkflowPriority(workflowInstance.getPriority());
    logger.debug("SubWorkflowTask {} created to be Scheduled", subWorkflowTask);
    return Collections.singletonList(subWorkflowTask);
}
Also used : SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with SubWorkflowParams

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

the class SubWorkflowTaskMapperTest method testTaskToDomain.

@SuppressWarnings("unchecked")
@Test
public void testTaskToDomain() {
    // Given
    WorkflowDef workflowDef = new WorkflowDef();
    Workflow workflowInstance = new Workflow();
    workflowInstance.setWorkflowDefinition(workflowDef);
    WorkflowTask taskToSchedule = new WorkflowTask();
    Map<String, String> taskToDomain = new HashMap<String, String>() {

        {
            put("*", "unittest");
        }
    };
    SubWorkflowParams subWorkflowParams = new SubWorkflowParams();
    subWorkflowParams.setName("Foo");
    subWorkflowParams.setVersion(2);
    subWorkflowParams.setTaskToDomain(taskToDomain);
    taskToSchedule.setSubWorkflowParam(subWorkflowParams);
    Map<String, Object> taskInput = new HashMap<>();
    Map<String, Object> subWorkflowParamMap = new HashMap<>();
    subWorkflowParamMap.put("name", "FooWorkFlow");
    subWorkflowParamMap.put("version", 2);
    when(parametersUtils.getTaskInputV2(anyMap(), any(Workflow.class), any(), any())).thenReturn(subWorkflowParamMap);
    // When
    TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder().withWorkflowDefinition(workflowDef).withWorkflowInstance(workflowInstance).withTaskToSchedule(taskToSchedule).withTaskInput(taskInput).withRetryCount(0).withTaskId(IDGenerator.generate()).withDeciderService(deciderService).build();
    List<Task> mappedTasks = subWorkflowTaskMapper.getMappedTasks(taskMapperContext);
    // Then
    assertTrue(!mappedTasks.isEmpty());
    assertEquals(1, mappedTasks.size());
    Task subWorkFlowTask = mappedTasks.get(0);
    assertEquals(Task.Status.SCHEDULED, subWorkFlowTask.getStatus());
    assertEquals(SubWorkflow.NAME, subWorkFlowTask.getTaskType());
}
Also used : SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) 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) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 15 with SubWorkflowParams

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

the class WorkflowTaskTypeConstraintTest method testWorkflowTaskTypeSubworkflow.

@Test
public void testWorkflowTaskTypeSubworkflow() {
    WorkflowTask workflowTask = createSampleWorkflowTask();
    workflowTask.setType("SUB_WORKFLOW");
    SubWorkflowParams subWorkflowTask = new SubWorkflowParams();
    workflowTask.setSubWorkflowParam(subWorkflowTask);
    Set<ConstraintViolation<WorkflowTask>> result = validator.validate(workflowTask);
    assertEquals(2, result.size());
    List<String> validationErrors = new ArrayList<>();
    result.forEach(e -> validationErrors.add(e.getMessage()));
    assertTrue(validationErrors.contains("SubWorkflowParams name cannot be null"));
    assertTrue(validationErrors.contains("SubWorkflowParams name cannot be empty"));
}
Also used : SubWorkflowParams(com.netflix.conductor.common.metadata.workflow.SubWorkflowParams) ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

SubWorkflowParams (com.netflix.conductor.common.metadata.workflow.SubWorkflowParams)26 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)21 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)17 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 LinkedList (java.util.LinkedList)5 Task (com.netflix.conductor.common.metadata.tasks.Task)4 Workflow (com.netflix.conductor.common.run.Workflow)4 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)4 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ConstraintViolation (javax.validation.ConstraintViolation)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DynamicForkJoinTaskList (com.netflix.conductor.common.metadata.workflow.DynamicForkJoinTaskList)1 JsonMapperProvider (com.netflix.conductor.common.utils.JsonMapperProvider)1 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)1 TerminateWorkflowException (com.netflix.conductor.core.execution.TerminateWorkflowException)1