Search in sources :

Example 6 with Message

use of com.netflix.conductor.core.events.queue.Message 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 7 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class Event method cancel.

@Override
public void cancel(Workflow workflow, Task task, WorkflowExecutor provider) {
    Message message = new Message(task.getTaskId(), null, task.getTaskId());
    getQueue(workflow, task).ack(Collections.singletonList(message));
}
Also used : Message(com.netflix.conductor.core.events.queue.Message)

Example 8 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class TestElasticSearchRestDAOV6 method shouldAddMessage.

@Test
public void shouldAddMessage() {
    String queue = "queue";
    Message message1 = new Message(uuid(), "payload1", null);
    Message message2 = new Message(uuid(), "payload2", null);
    indexDAO.addMessage(queue, message1);
    indexDAO.addMessage(queue, message2);
    List<Message> indexedMessages = tryFindResults(() -> indexDAO.getMessages(queue), 2);
    assertEquals(2, indexedMessages.size());
    assertTrue("Not all messages was indexed", indexedMessages.containsAll(Arrays.asList(message1, message2)));
}
Also used : Message(com.netflix.conductor.core.events.queue.Message) Test(org.junit.Test)

Example 9 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class AMQPObservableQueue method asMessage.

private static Message asMessage(AMQPSettings settings, GetResponse response) throws Exception {
    if (response == null) {
        return null;
    }
    final Message message = new Message();
    message.setId(response.getProps().getMessageId());
    message.setPayload(new String(response.getBody(), settings.getContentEncoding()));
    message.setReceipt(String.valueOf(response.getEnvelope().getDeliveryTag()));
    return message;
}
Also used : Message(com.netflix.conductor.core.events.queue.Message)

Example 10 with Message

use of com.netflix.conductor.core.events.queue.Message in project conductor by Netflix.

the class AMQPObservableQueue method observe.

@Override
public Observable<Message> observe() {
    receiveMessages();
    Observable.OnSubscribe<Message> onSubscribe = subscriber -> {
        Observable<Long> interval = Observable.interval(pollTimeInMS, TimeUnit.MILLISECONDS);
        interval.flatMap((Long x) -> {
            List<Message> available = new LinkedList<>();
            messages.drainTo(available);
            if (!available.isEmpty()) {
                AtomicInteger count = new AtomicInteger(0);
                StringBuilder buffer = new StringBuilder();
                available.forEach(msg -> {
                    buffer.append(msg.getId()).append("=").append(msg.getPayload());
                    count.incrementAndGet();
                    if (count.get() < available.size()) {
                        buffer.append(",");
                    }
                });
                logger.info(String.format("Batch from %s to conductor is %s", settings.getQueueOrExchangeName(), buffer.toString()));
            }
            return Observable.from(available);
        }).subscribe(subscriber::onNext, subscriber::onError);
    };
    return Observable.create(onSubscribe);
}
Also used : Arrays(java.util.Arrays) DeliverCallback(com.rabbitmq.client.DeliverCallback) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Connection(com.rabbitmq.client.Connection) BasicProperties(com.rabbitmq.client.BasicProperties) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Observable(rx.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) LinkedList(java.util.LinkedList) Consumer(com.rabbitmq.client.Consumer) GetResponse(com.rabbitmq.client.GetResponse) Message(com.netflix.conductor.core.events.queue.Message) Logger(org.slf4j.Logger) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException) UUID(java.util.UUID) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Monitors(com.netflix.conductor.metrics.Monitors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Envelope(com.rabbitmq.client.Envelope) Configuration(com.netflix.conductor.core.config.Configuration) Channel(com.rabbitmq.client.Channel) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) AMQP(com.rabbitmq.client.AMQP) Address(com.rabbitmq.client.Address) Message(com.netflix.conductor.core.events.queue.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(rx.Observable) LinkedList(java.util.LinkedList)

Aggregations

Message (com.netflix.conductor.core.events.queue.Message)42 ArrayList (java.util.ArrayList)18 Test (org.junit.Test)16 LinkedList (java.util.LinkedList)12 List (java.util.List)11 ObservableQueue (com.netflix.conductor.core.events.queue.ObservableQueue)10 HashMap (java.util.HashMap)10 Collections (java.util.Collections)9 Map (java.util.Map)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Connection (java.sql.Connection)7 Executors (java.util.concurrent.Executors)7 Task (com.netflix.conductor.common.metadata.tasks.Task)6 TimeUnit (java.util.concurrent.TimeUnit)6 Workflow (com.netflix.conductor.common.run.Workflow)5 QueueDAO (com.netflix.conductor.dao.QueueDAO)5 Collectors (java.util.stream.Collectors)5 SearchHit (org.elasticsearch.search.SearchHit)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5