Search in sources :

Example 61 with Task

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

the class CassandraExecutionDAO method createWorkflow.

@Override
public String createWorkflow(Workflow workflow) {
    try {
        List<Task> tasks = workflow.getTasks();
        workflow.setTasks(new LinkedList<>());
        String payload = toJson(workflow);
        recordCassandraDaoRequests("createWorkflow", "n/a", workflow.getWorkflowName());
        recordCassandraDaoPayloadSize("createWorkflow", payload.length(), "n/a", workflow.getWorkflowName());
        session.execute(insertWorkflowStatement.bind(UUID.fromString(workflow.getWorkflowId()), 1, "", payload, 0, 1));
        workflow.setTasks(tasks);
        return workflow.getWorkflowId();
    } catch (Exception e) {
        Monitors.error(CLASS_NAME, "createWorkflow");
        String errorMsg = String.format("Error creating workflow: %s", workflow.getWorkflowId());
        LOGGER.error(errorMsg, e);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException)

Example 62 with Task

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

the class TaskClient method pollTask.

/**
 * Perform a poll for a task of a specific task type.
 *
 * @param taskType The taskType to poll for
 * @param domain   The domain of the task type
 * @param workerId Name of the client worker. Used for logging.
 * @return Task waiting to be executed.
 */
public Task pollTask(String taskType, String workerId, String domain) {
    Preconditions.checkArgument(StringUtils.isNotBlank(taskType), "Task type cannot be blank");
    Preconditions.checkArgument(StringUtils.isNotBlank(workerId), "Worker id cannot be blank");
    Object[] params = new Object[] { "workerid", workerId, "domain", domain };
    Task task = Optional.ofNullable(getForEntity("tasks/poll/{taskType}", params, Task.class, taskType)).orElse(new Task());
    populateTaskPayloads(task);
    return task;
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task)

Example 63 with Task

use of com.netflix.conductor.common.metadata.tasks.Task 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 64 with Task

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

the class TestDeciderService method testGetTaskInput.

@SuppressWarnings("unchecked")
@Test
public void testGetTaskInput() {
    Map<String, Object> ip = new HashMap<>();
    ip.put("workflowInputParam", "${workflow.input.requestId}");
    ip.put("taskOutputParam", "${task2.output.location}");
    List<Map<String, Object>> json = new LinkedList<>();
    Map<String, Object> m1 = new HashMap<>();
    m1.put("name", "person name");
    m1.put("city", "New York");
    m1.put("phone", 2120001234);
    m1.put("status", "${task2.output.isPersonActive}");
    Map<String, Object> m2 = new HashMap<>();
    m2.put("employer", "City Of New York");
    m2.put("color", "purple");
    m2.put("requestId", "${workflow.input.requestId}");
    json.add(m1);
    json.add(m2);
    ip.put("complexJson", json);
    WorkflowDef def = new WorkflowDef();
    def.setName("testGetTaskInput");
    def.setSchemaVersion(2);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(def);
    workflow.getInput().put("requestId", "request id 001");
    Task task = new Task();
    task.setReferenceTaskName("task2");
    task.getOutputData().put("location", "http://location");
    task.getOutputData().put("isPersonActive", true);
    workflow.getTasks().add(task);
    Map<String, Object> taskInput = parametersUtils.getTaskInput(ip, workflow, null, null);
    assertNotNull(taskInput);
    assertTrue(taskInput.containsKey("workflowInputParam"));
    assertTrue(taskInput.containsKey("taskOutputParam"));
    assertEquals("request id 001", taskInput.get("workflowInputParam"));
    assertEquals("http://location", taskInput.get("taskOutputParam"));
    assertNotNull(taskInput.get("complexJson"));
    assertTrue(taskInput.get("complexJson") instanceof List);
    List<Map<String, Object>> resolvedInput = (List<Map<String, Object>>) taskInput.get("complexJson");
    assertEquals(2, resolvedInput.size());
}
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) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 65 with Task

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

the class TestDeciderService method testDeciderGetNextTask.

@Test
public void testDeciderGetNextTask() {
    WorkflowDef workflowDef = createDoWhileInForkWorkflow();
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(workflowDef);
    Task task1 = new Task();
    task1.setReferenceTaskName("fork");
    task1.setStatus(Status.COMPLETED);
    task1.setTaskId("task1");
    Task task2 = new Task();
    task2.setReferenceTaskName("loopTask");
    task2.setStatus(Status.SCHEDULED);
    task2.setTaskId("task2");
    task2.setIteration(1);
    Task task3 = new Task();
    task3.setReferenceTaskName("junit_task_0__1");
    task3.setStatus(Status.COMPLETED);
    task3.setTaskId("task3");
    task3.setIteration(1);
    Task task4 = new Task();
    task4.setReferenceTaskName("junit_task_3");
    task4.setStatus(Status.COMPLETED);
    task4.setTaskId("task4");
    Task task5 = new Task();
    task5.setReferenceTaskName("join");
    task5.setStatus(Status.IN_PROGRESS);
    task5.setTaskId("task5");
    workflow.setTasks(Arrays.asList(task1, task2, task3, task4, task5));
    // verify the next task of first task in DoWhile
    List<Task> nextTask1 = deciderService.getNextTask(workflow, task3);
    assertEquals(1, nextTask1.size());
    assertEquals("junit_task_1", nextTask1.get(0).getReferenceTaskName());
    Task task6 = new Task();
    task6.setReferenceTaskName("junit_task_1__1");
    task6.setStatus(Status.COMPLETED);
    task6.setTaskId("task6");
    task6.setIteration(1);
    workflow.setTasks(Arrays.asList(task1, task2, task3, task4, task5, task6));
    // verify the next task of last task in DoWhile
    List<Task> nextTask2 = deciderService.getNextTask(workflow, task6);
    assertEquals(0, nextTask2.size());
    task2.setStatus(Status.COMPLETED);
    task2.setIteration(10);
    // verify the next task of DoWhile
    List<Task> nextTask3 = deciderService.getNextTask(workflow, task2);
    assertEquals(1, nextTask3.size());
    assertEquals("junit_task_2", nextTask3.get(0).getReferenceTaskName());
}
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) Test(org.junit.Test)

Aggregations

Task (com.netflix.conductor.common.metadata.tasks.Task)357 Workflow (com.netflix.conductor.common.run.Workflow)249 Test (org.junit.Test)248 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)227 HashMap (java.util.HashMap)147 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)121 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)110 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)95 UserTask (com.netflix.conductor.tests.utils.UserTask)73 Map (java.util.Map)53 LinkedList (java.util.LinkedList)51 WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)45 List (java.util.List)45 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)41 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)39 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)38 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)29 Collectors (java.util.stream.Collectors)29 TaskType (com.netflix.conductor.common.metadata.workflow.TaskType)28