Search in sources :

Example 6 with EventHandler

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

the class TestSimpleEventProcessor method testEventProcessorWithRetriableError.

@Test
public void testEventProcessorWithRetriableError() {
    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(queue.rePublishIfNoAck()).thenReturn(false);
    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.BACKEND_ERROR, "some 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, never()).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 7 with EventHandler

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

the class TestSimpleEventProcessor method testEventHandlerWithCondition.

@Test
public void testEventHandlerWithCondition() {
    EventHandler eventHandler = new EventHandler();
    eventHandler.setName("cms_intermediate_video_ingest_handler");
    eventHandler.setActive(true);
    eventHandler.setEvent("sqs:dev_cms_asset_ingest_queue");
    eventHandler.setCondition("$.Message.testKey1 == 'level1' && $.Message.metadata.testKey2 == 123456");
    Map<String, Object> startWorkflowInput = new LinkedHashMap<>();
    startWorkflowInput.put("param1", "${Message.metadata.testKey2}");
    startWorkflowInput.put("param2", "SQS-${MessageId}");
    Action startWorkflowAction = new Action();
    startWorkflowAction.setAction(Type.start_workflow);
    startWorkflowAction.setStart_workflow(new StartWorkflow());
    startWorkflowAction.getStart_workflow().setName("cms_artwork_automation");
    startWorkflowAction.getStart_workflow().setVersion(1);
    startWorkflowAction.getStart_workflow().setInput(startWorkflowInput);
    startWorkflowAction.setExpandInlineJSON(true);
    eventHandler.getActions().add(startWorkflowAction);
    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), eq(null));
    WorkflowDef workflowDef = new WorkflowDef();
    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());
    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue(started.get());
}
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) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) TestConfiguration(com.netflix.conductor.core.execution.TestConfiguration) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) LinkedHashMap(java.util.LinkedHashMap) StartWorkflow(com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 8 with EventHandler

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

the class SimpleEventProcessor method executeEvent.

/**
 * Executes all the actions configured on all the event handlers triggered by the {@link Message} on the queue
 * If any of the actions on an event handler fails due to a transient failure, the execution is not persisted such that it can be retried
 *
 * @return a list of {@link EventExecution} that failed due to transient failures.
 */
protected List<EventExecution> executeEvent(String event, Message msg) throws Exception {
    List<EventHandler> eventHandlerList = metadataService.getEventHandlersForEvent(event, true);
    Object payloadObject = getPayloadObject(msg.getPayload());
    List<EventExecution> transientFailures = new ArrayList<>();
    for (EventHandler eventHandler : eventHandlerList) {
        String condition = eventHandler.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            logger.debug("Checking condition: {} for event: {}", condition, event);
            Boolean success = ScriptEvaluator.evalBool(condition, jsonUtils.expand(payloadObject));
            if (!success) {
                String id = msg.getId() + "_" + 0;
                EventExecution eventExecution = new EventExecution(id, msg.getId());
                eventExecution.setCreated(System.currentTimeMillis());
                eventExecution.setEvent(eventHandler.getEvent());
                eventExecution.setName(eventHandler.getName());
                eventExecution.setStatus(Status.SKIPPED);
                eventExecution.getOutput().put("msg", msg.getPayload());
                eventExecution.getOutput().put("condition", condition);
                executionService.addEventExecution(eventExecution);
                logger.debug("Condition: {} not successful for event: {} with payload: {}", condition, eventHandler.getEvent(), msg.getPayload());
                continue;
            }
        }
        CompletableFuture<List<EventExecution>> future = executeActionsForEventHandler(eventHandler, msg);
        future.whenComplete((result, error) -> result.forEach(eventExecution -> {
            if (error != null || eventExecution.getStatus() == Status.IN_PROGRESS) {
                transientFailures.add(eventExecution);
            } else {
                executionService.updateEventExecution(eventExecution);
            }
        })).get();
    }
    return processTransientFailures(transientFailures);
}
Also used : MetadataService(com.netflix.conductor.service.MetadataService) Action(com.netflix.conductor.common.metadata.events.EventHandler.Action) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) JsonUtils(com.netflix.conductor.core.utils.JsonUtils) Inject(javax.inject.Inject) Map(java.util.Map) Status(com.netflix.conductor.common.metadata.events.EventExecution.Status) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) CompletableFutures(com.spotify.futures.CompletableFutures) Message(com.netflix.conductor.core.events.queue.Message) Logger(org.slf4j.Logger) RetryUtil(com.netflix.conductor.common.utils.RetryUtil) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Monitors(com.netflix.conductor.metrics.Monitors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutionService(com.netflix.conductor.service.ExecutionService) Configuration(com.netflix.conductor.core.config.Configuration) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Collections(java.util.Collections) EventExecution(com.netflix.conductor.common.metadata.events.EventExecution) ArrayList(java.util.ArrayList) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 9 with EventHandler

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

the class MetadataServiceTest method testValidateEventNoEvent.

@Test(expected = ConstraintViolationException.class)
public void testValidateEventNoEvent() {
    try {
        EventHandler eventHandler = new EventHandler();
        metadataService.addEventHandler(eventHandler);
    } catch (ConstraintViolationException ex) {
        assertEquals(3, ex.getConstraintViolations().size());
        Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations());
        assertTrue(messages.contains("Missing event handler name"));
        assertTrue(messages.contains("Missing event location"));
        assertTrue(messages.contains("No actions specified. Please specify at-least one action"));
        throw ex;
    }
    fail("metadataService.addEventHandler did not throw ConstraintViolationException !");
}
Also used : Set(java.util.Set) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.Test)

Example 10 with EventHandler

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

the class MySQLMetadataDAO method updateEventHandler.

@Override
public void updateEventHandler(EventHandler eventHandler) {
    Preconditions.checkNotNull(eventHandler.getName(), "EventHandler name cannot be null");
    // @formatter:off
    final String UPDATE_EVENT_HANDLER_QUERY = "UPDATE meta_event_handler SET " + "event = ?, active = ?, json_data = ?, " + "modified_on = CURRENT_TIMESTAMP WHERE name = ?";
    // @formatter:on
    withTransaction(tx -> {
        EventHandler existing = getEventHandler(tx, eventHandler.getName());
        if (existing == null) {
            throw new ApplicationException(ApplicationException.Code.NOT_FOUND, "EventHandler with name " + eventHandler.getName() + " not found!");
        }
        execute(tx, UPDATE_EVENT_HANDLER_QUERY, q -> q.addParameter(eventHandler.getEvent()).addParameter(eventHandler.isActive()).addJsonParameter(eventHandler).addParameter(eventHandler.getName()).executeUpdate());
    });
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) EventHandler(com.netflix.conductor.common.metadata.events.EventHandler)

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