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());
}
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());
}
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);
}
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 !");
}
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());
});
}
Aggregations