Search in sources :

Example 6 with Workflow

use of com.netflix.conductor.common.run.Workflow in project conductor by Netflix.

the class TestEvent method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(testWorkflowDefinition);
    Task task = new Task();
    task.getInputData().put("sink", "conductor");
    task.setReferenceTaskName("task0");
    task.setTaskId("task_id_0");
    QueueDAO dao = mock(QueueDAO.class);
    String[] publishedQueue = new String[1];
    List<Message> publishedMessages = new LinkedList<>();
    doAnswer((Answer<Void>) invocation -> {
        String queueName = invocation.getArgument(0, String.class);
        System.out.println(queueName);
        publishedQueue[0] = queueName;
        List<Message> messages = invocation.getArgument(1, List.class);
        publishedMessages.addAll(messages);
        return null;
    }).when(dao).push(any(), any());
    doAnswer((Answer<Boolean>) invocation -> {
        String messageId = invocation.getArgument(1, String.class);
        if (publishedMessages.get(0).getId().equals(messageId)) {
            publishedMessages.remove(0);
            return true;
        }
        return null;
    }).when(dao).ack(any(), any());
    Map<String, EventQueueProvider> providers = new HashMap<>();
    providers.put("conductor", new DynoEventQueueProvider(dao, new TestConfiguration(), Schedulers.from(Executors.newSingleThreadExecutor())));
    eventQueues = new EventQueues(providers, parametersUtils);
    Event event = new Event(eventQueues, parametersUtils, objectMapper);
    event.start(workflow, task, null);
    assertEquals(Task.Status.COMPLETED, task.getStatus());
    assertNotNull(task.getOutputData());
    assertEquals("conductor:" + workflow.getWorkflowName() + ":" + task.getReferenceTaskName(), task.getOutputData().get("event_produced"));
    assertEquals(task.getOutputData().get("event_produced"), "conductor:" + publishedQueue[0]);
    assertEquals(1, publishedMessages.size());
    assertEquals(task.getTaskId(), publishedMessages.get(0).getId());
    assertNotNull(publishedMessages.get(0).getPayload());
    event.cancel(workflow, task, null);
    assertTrue(publishedMessages.isEmpty());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) Task(com.netflix.conductor.common.metadata.tasks.Task) ParametersUtils(com.netflix.conductor.core.execution.ParametersUtils) Answer(org.mockito.stubbing.Answer) EventQueueProvider(com.netflix.conductor.core.events.EventQueueProvider) Workflow(com.netflix.conductor.common.run.Workflow) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Schedulers(rx.schedulers.Schedulers) LinkedList(java.util.LinkedList) Before(org.junit.Before) Message(com.netflix.conductor.core.events.queue.Message) MockQueueProvider(com.netflix.conductor.core.events.MockQueueProvider) DynoEventQueueProvider(com.netflix.conductor.core.events.queue.dyno.DynoEventQueueProvider) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EventQueues(com.netflix.conductor.core.events.EventQueues) Executors(java.util.concurrent.Executors) QueueDAO(com.netflix.conductor.dao.QueueDAO) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Task(com.netflix.conductor.common.metadata.tasks.Task) DynoEventQueueProvider(com.netflix.conductor.core.events.queue.dyno.DynoEventQueueProvider) Message(com.netflix.conductor.core.events.queue.Message) HashMap(java.util.HashMap) EventQueueProvider(com.netflix.conductor.core.events.EventQueueProvider) DynoEventQueueProvider(com.netflix.conductor.core.events.queue.dyno.DynoEventQueueProvider) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) Workflow(com.netflix.conductor.common.run.Workflow) LinkedList(java.util.LinkedList) QueueDAO(com.netflix.conductor.dao.QueueDAO) EventQueues(com.netflix.conductor.core.events.EventQueues) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.Test)

Example 7 with Workflow

use of com.netflix.conductor.common.run.Workflow in project conductor by Netflix.

the class TestEvent method testFailures.

@Test
public void testFailures() {
    Event event = new Event(eventQueues, parametersUtils, objectMapper);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(testWorkflowDefinition);
    Task task = new Task();
    task.setReferenceTaskName("task0");
    task.setTaskId("task_id_0");
    event.start(workflow, task, null);
    assertEquals(Task.Status.FAILED, task.getStatus());
    assertNotNull(task.getReasonForIncompletion());
    System.out.println(task.getReasonForIncompletion());
    task.getInputData().put("sink", "bad_sink");
    task.setStatus(Status.SCHEDULED);
    event.start(workflow, task, null);
    assertEquals(Task.Status.FAILED, task.getStatus());
    assertNotNull(task.getReasonForIncompletion());
    System.out.println(task.getReasonForIncompletion());
    task.setStatus(Status.SCHEDULED);
    task.setScheduledTime(System.currentTimeMillis());
    event.execute(workflow, task, null);
    assertEquals(Task.Status.SCHEDULED, task.getStatus());
    task.setScheduledTime(System.currentTimeMillis() - 610_000);
    event.start(workflow, task, null);
    assertEquals(Task.Status.FAILED, task.getStatus());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) Workflow(com.netflix.conductor.common.run.Workflow) Test(org.junit.Test)

Example 8 with Workflow

use of com.netflix.conductor.common.run.Workflow in project conductor by Netflix.

the class TestEvent method testSinkParam.

@Test
public void testSinkParam() {
    String sink = "sqs:queue_name";
    WorkflowDef def = new WorkflowDef();
    def.setName("wf0");
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(def);
    Task task1 = new Task();
    task1.setReferenceTaskName("t1");
    task1.getOutputData().put("q", "t1_queue");
    workflow.getTasks().add(task1);
    Task task2 = new Task();
    task2.setReferenceTaskName("t2");
    task2.getOutputData().put("q", "task2_queue");
    workflow.getTasks().add(task2);
    Task task = new Task();
    task.setReferenceTaskName("event");
    task.getInputData().put("sink", sink);
    task.setTaskType(TaskType.EVENT.name());
    workflow.getTasks().add(task);
    Event event = new Event(eventQueues, parametersUtils, objectMapper);
    ObservableQueue queue = event.getQueue(workflow, task);
    assertNotNull(task.getReasonForIncompletion(), queue);
    assertEquals("queue_name", queue.getName());
    assertEquals("sqs", queue.getType());
    sink = "sqs:${t1.output.q}";
    task.getInputData().put("sink", sink);
    queue = event.getQueue(workflow, task);
    assertNotNull(queue);
    assertEquals("t1_queue", queue.getName());
    assertEquals("sqs", queue.getType());
    System.out.println(task.getOutputData().get("event_produced"));
    sink = "sqs:${t2.output.q}";
    task.getInputData().put("sink", sink);
    queue = event.getQueue(workflow, task);
    assertNotNull(queue);
    assertEquals("task2_queue", queue.getName());
    assertEquals("sqs", queue.getType());
    System.out.println(task.getOutputData().get("event_produced"));
    sink = "conductor";
    task.getInputData().put("sink", sink);
    queue = event.getQueue(workflow, task);
    assertNotNull(queue);
    assertEquals(workflow.getWorkflowName() + ":" + task.getReferenceTaskName(), queue.getName());
    assertEquals("conductor", queue.getType());
    System.out.println(task.getOutputData().get("event_produced"));
    sink = "sqs:static_value";
    task.getInputData().put("sink", sink);
    queue = event.getQueue(workflow, task);
    assertNotNull(queue);
    assertEquals("static_value", queue.getName());
    assertEquals("sqs", queue.getType());
    assertEquals(sink, task.getOutputData().get("event_produced"));
    System.out.println(task.getOutputData().get("event_produced"));
    sink = "bad:queue";
    task.getInputData().put("sink", sink);
    queue = event.getQueue(workflow, task);
    assertNull(queue);
    assertEquals(Task.Status.FAILED, task.getStatus());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Workflow(com.netflix.conductor.common.run.Workflow) Test(org.junit.Test)

Example 9 with Workflow

use of com.netflix.conductor.common.run.Workflow in project conductor by Netflix.

the class TestWorkflowExecutor method testRerunWorkflowWithTaskId.

@Test
public void testRerunWorkflowWithTaskId() {
    // setup
    Workflow workflow = new Workflow();
    workflow.setWorkflowId("testRerunWorkflowId");
    workflow.setWorkflowType("testRerunWorkflowId");
    workflow.setVersion(1);
    workflow.setOwnerApp("junit_testRerunWorkflowId");
    workflow.setStartTime(10L);
    workflow.setEndTime(100L);
    // noinspection unchecked
    workflow.setOutput(Collections.EMPTY_MAP);
    workflow.setStatus(Workflow.WorkflowStatus.FAILED);
    workflow.setReasonForIncompletion("task1 failed");
    workflow.setFailedReferenceTaskNames(new HashSet<String>() {

        {
            add("task1_ref1");
        }
    });
    Task task_1_1 = new Task();
    task_1_1.setTaskId(UUID.randomUUID().toString());
    task_1_1.setSeq(20);
    task_1_1.setRetryCount(1);
    task_1_1.setTaskType(TaskType.SIMPLE.toString());
    task_1_1.setStatus(Status.FAILED);
    task_1_1.setRetried(true);
    task_1_1.setTaskDefName("task1");
    task_1_1.setWorkflowTask(new WorkflowTask());
    task_1_1.setReferenceTaskName("task1_ref1");
    Task task_2_1 = new Task();
    task_2_1.setTaskId(UUID.randomUUID().toString());
    task_2_1.setSeq(22);
    task_2_1.setRetryCount(1);
    task_2_1.setStatus(Status.CANCELED);
    task_2_1.setTaskType(TaskType.SIMPLE.toString());
    task_2_1.setTaskDefName("task2");
    task_2_1.setWorkflowTask(new WorkflowTask());
    task_2_1.setReferenceTaskName("task2_ref1");
    workflow.getTasks().addAll(Arrays.asList(task_1_1, task_2_1));
    // end of setup
    // when:
    when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow);
    WorkflowDef workflowDef = new WorkflowDef();
    when(metadataDAO.getWorkflowDef(anyString(), anyInt())).thenReturn(Optional.of(workflowDef));
    RerunWorkflowRequest rerunWorkflowRequest = new RerunWorkflowRequest();
    rerunWorkflowRequest.setReRunFromWorkflowId(workflow.getWorkflowId());
    rerunWorkflowRequest.setReRunFromTaskId(task_1_1.getTaskId());
    workflowExecutor.rerun(rerunWorkflowRequest);
    // when:
    when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow);
    assertEquals(Workflow.WorkflowStatus.RUNNING, workflow.getStatus());
    assertEquals(null, workflow.getReasonForIncompletion());
    assertEquals(new HashSet<>(), workflow.getFailedReferenceTaskNames());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RerunWorkflowRequest(com.netflix.conductor.common.metadata.workflow.RerunWorkflowRequest) Test(org.junit.Test)

Example 10 with Workflow

use of com.netflix.conductor.common.run.Workflow in project conductor by Netflix.

the class TestWorkflowExecutor method testRestartWorkflow.

@Test
public void testRestartWorkflow() {
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.setName("test_task");
    workflowTask.setTaskReferenceName("task_ref");
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setName("testDef");
    workflowDef.setVersion(1);
    workflowDef.setRestartable(true);
    workflowDef.getTasks().addAll(Collections.singletonList(workflowTask));
    Task task_1 = new Task();
    task_1.setTaskId(UUID.randomUUID().toString());
    task_1.setSeq(1);
    task_1.setStatus(Status.FAILED);
    task_1.setTaskDefName(workflowTask.getName());
    task_1.setReferenceTaskName(workflowTask.getTaskReferenceName());
    Task task_2 = new Task();
    task_2.setTaskId(UUID.randomUUID().toString());
    task_2.setSeq(2);
    task_2.setStatus(Status.FAILED);
    task_2.setTaskDefName(workflowTask.getName());
    task_2.setReferenceTaskName(workflowTask.getTaskReferenceName());
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(workflowDef);
    workflow.setWorkflowId("test-workflow-id");
    workflow.getTasks().addAll(Arrays.asList(task_1, task_2));
    workflow.setStatus(Workflow.WorkflowStatus.FAILED);
    when(executionDAOFacade.getWorkflowById(anyString(), anyBoolean())).thenReturn(workflow);
    doNothing().when(executionDAOFacade).removeTask(any());
    when(metadataDAO.getWorkflowDef(workflow.getWorkflowName(), workflow.getWorkflowVersion())).thenReturn(Optional.of(workflowDef));
    when(metadataDAO.getTaskDef(workflowTask.getName())).thenReturn(new TaskDef());
    when(executionDAOFacade.updateWorkflow(any())).thenReturn("");
    workflowExecutor.rewind(workflow.getWorkflowId(), false);
    assertEquals(Workflow.WorkflowStatus.RUNNING, workflow.getStatus());
    verify(metadataDAO, never()).getLatestWorkflowDef(any());
    ArgumentCaptor<Workflow> argumentCaptor = ArgumentCaptor.forClass(Workflow.class);
    verify(executionDAOFacade, times(1)).createWorkflow(argumentCaptor.capture());
    assertEquals(workflow.getWorkflowId(), argumentCaptor.getAllValues().get(0).getWorkflowId());
    assertEquals(workflow.getWorkflowDefinition(), argumentCaptor.getAllValues().get(0).getWorkflowDefinition());
    // add a new version of the workflow definition and restart with latest
    workflow.setStatus(Workflow.WorkflowStatus.COMPLETED);
    workflowDef = new WorkflowDef();
    workflowDef.setName("testDef");
    workflowDef.setVersion(2);
    workflowDef.setRestartable(true);
    workflowDef.getTasks().addAll(Collections.singletonList(workflowTask));
    when(metadataDAO.getLatestWorkflowDef(workflow.getWorkflowName())).thenReturn(Optional.of(workflowDef));
    workflowExecutor.rewind(workflow.getWorkflowId(), true);
    assertEquals(Workflow.WorkflowStatus.RUNNING, workflow.getStatus());
    verify(metadataDAO, times(1)).getLatestWorkflowDef(anyString());
    argumentCaptor = ArgumentCaptor.forClass(Workflow.class);
    verify(executionDAOFacade, times(2)).createWorkflow(argumentCaptor.capture());
    assertEquals(workflow.getWorkflowId(), argumentCaptor.getAllValues().get(1).getWorkflowId());
    assertEquals(workflowDef, argumentCaptor.getAllValues().get(1).getWorkflowDefinition());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) 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)

Aggregations

Workflow (com.netflix.conductor.common.run.Workflow)360 Test (org.junit.Test)259 Task (com.netflix.conductor.common.metadata.tasks.Task)246 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)206 HashMap (java.util.HashMap)154 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)149 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)130 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)97 UserTask (com.netflix.conductor.tests.utils.UserTask)73 LinkedList (java.util.LinkedList)57 Map (java.util.Map)55 List (java.util.List)54 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)52 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)50 Collections (java.util.Collections)36 Collectors (java.util.stream.Collectors)35 Logger (org.slf4j.Logger)35 LoggerFactory (org.slf4j.LoggerFactory)35 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)34 WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)34