Search in sources :

Example 16 with TaskResult

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

the class TestSimpleActionProcessor method testCompleteTaskByTaskId.

@Test
public void testCompleteTaskByTaskId() throws Exception {
    TaskDetails taskDetails = new TaskDetails();
    taskDetails.setWorkflowId("${workflowId}");
    taskDetails.setTaskId("${taskId}");
    Action action = new Action();
    action.setAction(Type.complete_task);
    action.setComplete_task(taskDetails);
    Object payload = objectMapper.readValue("{\"workflowId\":\"workflow_1\", \"taskId\":\"task_1\"}", Object.class);
    Task task = new Task();
    task.setTaskId("task_1");
    task.setReferenceTaskName("testTask");
    when(workflowExecutor.getTask(eq("task_1"))).thenReturn(task);
    actionProcessor.execute(action, payload, "testEvent", "testMessage");
    ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class);
    verify(workflowExecutor).updateTask(argumentCaptor.capture());
    assertEquals(Status.COMPLETED, argumentCaptor.getValue().getStatus());
    assertEquals("testMessage", argumentCaptor.getValue().getOutputData().get("conductor.event.messageId"));
    assertEquals("testEvent", argumentCaptor.getValue().getOutputData().get("conductor.event.name"));
    assertEquals("workflow_1", argumentCaptor.getValue().getOutputData().get("workflowId"));
    assertEquals("task_1", argumentCaptor.getValue().getOutputData().get("taskId"));
}
Also used : Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) Task(com.netflix.conductor.common.metadata.tasks.Task) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Example 17 with TaskResult

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

the class SimpleActionProcessor method completeTask.

private Map<String, Object> completeTask(Action action, Object payload, TaskDetails taskDetails, Status status, String event, String messageId) {
    Map<String, Object> input = new HashMap<>();
    input.put("workflowId", taskDetails.getWorkflowId());
    input.put("taskId", taskDetails.getTaskId());
    input.put("taskRefName", taskDetails.getTaskRefName());
    input.putAll(taskDetails.getOutput());
    Map<String, Object> replaced = parametersUtils.replace(input, payload);
    String workflowId = (String) replaced.get("workflowId");
    String taskId = (String) replaced.get("taskId");
    String taskRefName = (String) replaced.get("taskRefName");
    Task task = null;
    if (StringUtils.isNotEmpty(taskId)) {
        task = executor.getTask(taskId);
    } else if (StringUtils.isNotEmpty(workflowId) && StringUtils.isNotEmpty(taskRefName)) {
        Workflow workflow = executor.getWorkflow(workflowId, true);
        if (workflow == null) {
            replaced.put("error", "No workflow found with ID: " + workflowId);
            return replaced;
        }
        task = workflow.getTaskByRefName(taskRefName);
    }
    if (task == null) {
        replaced.put("error", "No task found with taskId: " + taskId + ", reference name: " + taskRefName + ", workflowId: " + workflowId);
        return replaced;
    }
    task.setStatus(status);
    task.setOutputData(replaced);
    task.setOutputMessage(taskDetails.getOutputMessage());
    task.getOutputData().put("conductor.event.messageId", messageId);
    task.getOutputData().put("conductor.event.name", event);
    try {
        executor.updateTask(new TaskResult(task));
        logger.debug("Updated task: {} in workflow:{} with status: {} for event: {} for message:{}", taskId, workflowId, status, event, messageId);
    } catch (RuntimeException e) {
        Monitors.recordEventActionError(action.getAction().name(), task.getTaskType(), event);
        logger.error("Error updating task: {} in workflow: {} in action: {} for event: {} for message: {}", taskDetails.getTaskRefName(), taskDetails.getWorkflowId(), action.getAction(), event, messageId, e);
        replaced.put("error", e.getMessage());
        throw e;
    }
    return replaced;
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) HashMap(java.util.HashMap) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult)

Example 18 with TaskResult

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

the class TaskServiceTest method testUpdateTaskInValid.

@Test(expected = ConstraintViolationException.class)
public void testUpdateTaskInValid() {
    try {
        TaskResult taskResult = new TaskResult();
        taskService.updateTask(taskResult);
    } catch (ConstraintViolationException ex) {
        assertEquals(2, ex.getConstraintViolations().size());
        Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
        assertTrue(messages.contains("Workflow Id cannot be null or empty"));
        assertTrue(messages.contains("Task ID cannot be null or empty"));
        throw ex;
    }
}
Also used : Set(java.util.Set) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.Test)

Example 19 with TaskResult

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

the class AbstractWorkflowServiceTest method testKafkaTaskDefTemplateSuccess.

@Test
public void testKafkaTaskDefTemplateSuccess() throws Exception {
    try {
        registerKafkaWorkflow();
    } catch (ApplicationException e) {
    }
    Map<String, Object> input = getKafkaInput();
    String workflowInstanceId = startOrLoadWorkflowExecution("template_kafka_workflow", 1, "testTaskDefTemplate", input, null, null);
    assertNotNull(workflowInstanceId);
    Workflow workflow = workflowExecutionService.getExecutionStatus(workflowInstanceId, true);
    assertNotNull(workflow);
    assertTrue(workflow.getReasonForIncompletion(), !workflow.getStatus().isTerminal());
    assertEquals(1, workflow.getTasks().size());
    Task task = workflow.getTasks().get(0);
    Map<String, Object> taskInput = task.getInputData();
    assertNotNull(taskInput);
    assertTrue(taskInput.containsKey("kafka_request"));
    assertTrue(taskInput.get("kafka_request") instanceof Map);
    String expected = "{\"kafka_request\":{\"topic\":\"test_kafka_topic\",\"bootStrapServers\":\"localhost:9092\",\"value\":{\"requestDetails\":{\"key1\":\"value1\",\"key2\":42},\"outputPath\":\"s3://bucket/outputPath\",\"inputPaths\":[\"file://path1\",\"file://path2\"]}}}";
    assertEquals(expected, objectMapper.writeValueAsString(taskInput));
    TaskResult taskResult = new TaskResult(task);
    taskResult.setStatus(TaskResult.Status.COMPLETED);
    // Polling for the first task
    Task task1 = workflowExecutionService.poll("KAFKA_PUBLISH", "test");
    assertNotNull(task1);
    assertTrue(workflowExecutionService.ackTaskReceived(task1.getTaskId()));
    assertEquals(workflowInstanceId, task1.getWorkflowInstanceId());
    workflowExecutionService.updateTask(taskResult);
    workflowExecutor.decide(workflowInstanceId);
    workflow = workflowExecutionService.getExecutionStatus(workflowInstanceId, true);
    assertNotNull(workflow);
    assertEquals(WorkflowStatus.COMPLETED, workflow.getStatus());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) UserTask(com.netflix.conductor.tests.utils.UserTask) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 20 with TaskResult

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

the class AbstractWorkflowServiceTest method testWait.

@Test
public void testWait() {
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setName("test_wait");
    workflowDef.setSchemaVersion(2);
    WorkflowTask waitWorkflowTask = new WorkflowTask();
    waitWorkflowTask.setWorkflowTaskType(TaskType.WAIT);
    waitWorkflowTask.setName("wait");
    waitWorkflowTask.setTaskReferenceName("wait0");
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.setName("junit_task_1");
    workflowTask.setTaskReferenceName("t1");
    workflowDef.getTasks().add(waitWorkflowTask);
    workflowDef.getTasks().add(workflowTask);
    metadataService.registerWorkflowDef(workflowDef);
    String workflowId = startOrLoadWorkflowExecution(workflowDef.getName(), workflowDef.getVersion(), "", new HashMap<>(), null, null);
    Workflow workflow = workflowExecutor.getWorkflow(workflowId, true);
    assertNotNull(workflow);
    assertEquals(1, workflow.getTasks().size());
    assertEquals(RUNNING, workflow.getStatus());
    Task waitTask = workflow.getTasks().get(0);
    assertEquals(TaskType.WAIT.name(), waitTask.getTaskType());
    waitTask.setStatus(COMPLETED);
    workflowExecutor.updateTask(new TaskResult(waitTask));
    Task task = workflowExecutionService.poll("junit_task_1", "test");
    assertNotNull(task);
    task.setStatus(Status.COMPLETED);
    workflowExecutionService.updateTask(task);
    workflow = workflowExecutionService.getExecutionStatus(workflowId, true);
    assertNotNull(workflow);
    assertEquals("tasks:" + workflow.getTasks(), WorkflowStatus.COMPLETED, workflow.getStatus());
}
Also used : 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) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

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