Search in sources :

Example 16 with EventHandler

use of com.netflix.conductor.common.metadata.events.EventHandler in project conductor by Netflix.

the class RedisEventHandlerDAO method getEventHandler.

private EventHandler getEventHandler(String name) {
    EventHandler eventHandler = null;
    String json = dynoClient.hget(nsKey(EVENT_HANDLERS), name);
    if (json != null) {
        eventHandler = readValue(json, EventHandler.class);
    }
    return eventHandler;
}
Also used : EventHandler(com.netflix.conductor.common.metadata.events.EventHandler)

Example 17 with EventHandler

use of com.netflix.conductor.common.metadata.events.EventHandler in project conductor by Netflix.

the class RedisEventHandlerDAOTest method testEventHandlers.

@Test
public void testEventHandlers() {
    String event1 = "SQS::arn:account090:sqstest1";
    String event2 = "SQS::arn:account090:sqstest2";
    EventHandler eventHandler = new EventHandler();
    eventHandler.setName(UUID.randomUUID().toString());
    eventHandler.setActive(false);
    Action action = new Action();
    action.setAction(Type.start_workflow);
    action.setStart_workflow(new StartWorkflow());
    action.getStart_workflow().setName("test_workflow");
    eventHandler.getActions().add(action);
    eventHandler.setEvent(event1);
    redisEventHandlerDAO.addEventHandler(eventHandler);
    List<EventHandler> allEventHandlers = redisEventHandlerDAO.getAllEventHandlers();
    assertNotNull(allEventHandlers);
    assertEquals(1, allEventHandlers.size());
    assertEquals(eventHandler.getName(), allEventHandlers.get(0).getName());
    assertEquals(eventHandler.getEvent(), allEventHandlers.get(0).getEvent());
    List<EventHandler> byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event1, true);
    assertNotNull(byEvents);
    // event is marked as in-active
    assertEquals(0, byEvents.size());
    eventHandler.setActive(true);
    eventHandler.setEvent(event2);
    redisEventHandlerDAO.updateEventHandler(eventHandler);
    allEventHandlers = redisEventHandlerDAO.getAllEventHandlers();
    assertNotNull(allEventHandlers);
    assertEquals(1, allEventHandlers.size());
    byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event1, true);
    assertNotNull(byEvents);
    assertEquals(0, byEvents.size());
    byEvents = redisEventHandlerDAO.getEventHandlersForEvent(event2, true);
    assertNotNull(byEvents);
    assertEquals(1, byEvents.size());
}
Also used : Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Test(org.junit.Test)

Example 18 with EventHandler

use of com.netflix.conductor.common.metadata.events.EventHandler in project conductor by Netflix.

the class TestSimpleEventProcessor method testEventProcessor.

@Test
public void testEventProcessor() {
    // setup event handler
    EventHandler eventHandler = new EventHandler();
    eventHandler.setName(UUID.randomUUID().toString());
    eventHandler.setActive(true);
    Map<String, String> taskToDomain = new HashMap<>();
    taskToDomain.put("*", "dev");
    Action startWorkflowAction = new Action();
    startWorkflowAction.setAction(Type.start_workflow);
    startWorkflowAction.setStart_workflow(new StartWorkflow());
    startWorkflowAction.getStart_workflow().setName("workflow_x");
    startWorkflowAction.getStart_workflow().setVersion(1);
    startWorkflowAction.getStart_workflow().setTaskToDomain(taskToDomain);
    eventHandler.getActions().add(startWorkflowAction);
    Action completeTaskAction = new Action();
    completeTaskAction.setAction(Type.complete_task);
    completeTaskAction.setComplete_task(new TaskDetails());
    completeTaskAction.getComplete_task().setTaskRefName("task_x");
    completeTaskAction.getComplete_task().setWorkflowId(UUID.randomUUID().toString());
    completeTaskAction.getComplete_task().setOutput(new HashMap<>());
    eventHandler.getActions().add(completeTaskAction);
    eventHandler.setEvent(event);
    when(metadataService.getAllEventHandlers()).thenReturn(Collections.singletonList(eventHandler));
    when(metadataService.getEventHandlersForEvent(event, true)).thenReturn(Collections.singletonList(eventHandler));
    when(executionService.addEventExecution(any())).thenReturn(true);
    when(queue.rePublishIfNoAck()).thenReturn(false);
    String id = UUID.randomUUID().toString();
    AtomicBoolean started = new AtomicBoolean(false);
    doAnswer((Answer<String>) invocation -> {
        started.set(true);
        return id;
    }).when(workflowExecutor).startWorkflow(eq(startWorkflowAction.getStart_workflow().getName()), eq(startWorkflowAction.getStart_workflow().getVersion()), eq(startWorkflowAction.getStart_workflow().getCorrelationId()), anyMap(), eq(null), eq(event), anyMap());
    AtomicBoolean completed = new AtomicBoolean(false);
    doAnswer((Answer<String>) invocation -> {
        completed.set(true);
        return null;
    }).when(workflowExecutor).updateTask(any());
    Task task = new Task();
    task.setReferenceTaskName(completeTaskAction.getComplete_task().getTaskRefName());
    Workflow workflow = new Workflow();
    workflow.setTasks(Collections.singletonList(task));
    when(workflowExecutor.getWorkflow(completeTaskAction.getComplete_task().getWorkflowId(), true)).thenReturn(workflow);
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setVersion(startWorkflowAction.getStart_workflow().getVersion());
    workflowDef.setName(startWorkflowAction.getStart_workflow().getName());
    when(metadataService.getWorkflowDef(any(), any())).thenReturn(workflowDef);
    SimpleActionProcessor actionProcessor = new SimpleActionProcessor(workflowExecutor, parametersUtils, jsonUtils);
    SimpleEventProcessor eventProcessor = new SimpleEventProcessor(executionService, metadataService, actionProcessor, eventQueues, jsonUtils, new TestConfiguration(), objectMapper);
    assertNotNull(eventProcessor.getQueues());
    assertEquals(1, eventProcessor.getQueues().size());
    String queueEvent = eventProcessor.getQueues().keySet().iterator().next();
    assertEquals(eventHandler.getEvent(), queueEvent);
    String eventProcessorQueue = eventProcessor.getQueues().values().iterator().next();
    assertEquals(queueURI, eventProcessorQueue);
    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue(started.get());
    assertTrue(completed.get());
    verify(queue, atMost(1)).ack(any());
    verify(queue, never()).publish(any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MetadataService(com.netflix.conductor.service.MetadataService) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) Task(com.netflix.conductor.common.metadata.tasks.Task) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Observable(rx.Observable) ParametersUtils(com.netflix.conductor.core.execution.ParametersUtils) JsonUtils(com.netflix.conductor.core.utils.JsonUtils) LinkedHashMap(java.util.LinkedHashMap) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) WorkflowExecutor(com.netflix.conductor.core.execution.WorkflowExecutor) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) Before(org.junit.Before) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) 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) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) UUID(java.util.UUID) Type(com.netflix.conductor.common.metadata.events.EventHandler.Action.Type) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.atMost(org.mockito.Mockito.atMost) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) ExecutionService(com.netflix.conductor.service.ExecutionService) 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) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) Test(org.junit.Test)

Example 19 with EventHandler

use of com.netflix.conductor.common.metadata.events.EventHandler in project conductor by Netflix.

the class TestSimpleEventProcessor method testEventProcessorWithNonRetriableError.

@Test
public void testEventProcessorWithNonRetriableError() {
    EventHandler eventHandler = new EventHandler();
    eventHandler.setName(UUID.randomUUID().toString());
    eventHandler.setActive(true);
    eventHandler.setEvent(event);
    Action completeTaskAction = new Action();
    completeTaskAction.setAction(Type.complete_task);
    completeTaskAction.setComplete_task(new TaskDetails());
    completeTaskAction.getComplete_task().setTaskRefName("task_x");
    completeTaskAction.getComplete_task().setWorkflowId(UUID.randomUUID().toString());
    completeTaskAction.getComplete_task().setOutput(new HashMap<>());
    eventHandler.getActions().add(completeTaskAction);
    when(metadataService.getAllEventHandlers()).thenReturn(Collections.singletonList(eventHandler));
    when(metadataService.getEventHandlersForEvent(event, true)).thenReturn(Collections.singletonList(eventHandler));
    when(executionService.addEventExecution(any())).thenReturn(true);
    when(actionProcessor.execute(any(), any(), any(), any())).thenThrow(new ApplicationException(ApplicationException.Code.INVALID_INPUT, "some non-retriable error"));
    SimpleEventProcessor eventProcessor = new SimpleEventProcessor(executionService, metadataService, actionProcessor, eventQueues, jsonUtils, new TestConfiguration(), objectMapper);
    assertNotNull(eventProcessor.getQueues());
    assertEquals(1, eventProcessor.getQueues().size());
    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    verify(queue, atMost(1)).ack(any());
    verify(queue, never()).publish(any());
}
Also used : Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) TaskDetails(com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) Test(org.junit.Test)

Example 20 with EventHandler

use of com.netflix.conductor.common.metadata.events.EventHandler in project conductor by Netflix.

the class MySQLMetadataDAOTest method testEventHandlers.

@Test
public void testEventHandlers() {
    String event1 = "SQS::arn:account090:sqstest1";
    String event2 = "SQS::arn:account090:sqstest2";
    EventHandler eh = new EventHandler();
    eh.setName(UUID.randomUUID().toString());
    eh.setActive(false);
    EventHandler.Action action = new EventHandler.Action();
    action.setAction(EventHandler.Action.Type.start_workflow);
    action.setStart_workflow(new EventHandler.StartWorkflow());
    action.getStart_workflow().setName("workflow_x");
    eh.getActions().add(action);
    eh.setEvent(event1);
    dao.addEventHandler(eh);
    List<EventHandler> all = dao.getAllEventHandlers();
    assertNotNull(all);
    assertEquals(1, all.size());
    assertEquals(eh.getName(), all.get(0).getName());
    assertEquals(eh.getEvent(), all.get(0).getEvent());
    List<EventHandler> byEvents = dao.getEventHandlersForEvent(event1, true);
    assertNotNull(byEvents);
    // event is marked as in-active
    assertEquals(0, byEvents.size());
    eh.setActive(true);
    eh.setEvent(event2);
    dao.updateEventHandler(eh);
    all = dao.getAllEventHandlers();
    assertNotNull(all);
    assertEquals(1, all.size());
    byEvents = dao.getEventHandlersForEvent(event1, true);
    assertNotNull(byEvents);
    assertEquals(0, byEvents.size());
    byEvents = dao.getEventHandlersForEvent(event2, true);
    assertNotNull(byEvents);
    assertEquals(1, byEvents.size());
}
Also used : EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) Test(org.junit.Test)

Aggregations

EventHandler (com.netflix.conductor.common.metadata.events.EventHandler)26 Test (org.junit.Test)14 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)13 Action (com.netflix.conductor.common.metadata.events.EventHandler.Action)6 ArrayList (java.util.ArrayList)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 TaskDetails (com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails)4 TestConfiguration (com.netflix.conductor.core.execution.TestConfiguration)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TimeUnit (java.util.concurrent.TimeUnit)4 EventExecution (com.netflix.conductor.common.metadata.events.EventExecution)3 StartWorkflow (com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow)3 Message (com.netflix.conductor.core.events.queue.Message)3 ObservableQueue (com.netflix.conductor.core.events.queue.ObservableQueue)3 JsonUtils (com.netflix.conductor.core.utils.JsonUtils)3 ExecutionService (com.netflix.conductor.service.ExecutionService)3 MetadataService (com.netflix.conductor.service.MetadataService)3 LinkedList (java.util.LinkedList)3