Search in sources :

Example 21 with Message

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

the class MySQLQueueDAO method popMessages.

private List<Message> popMessages(Connection connection, String queueName, int count, int timeout) {
    long start = System.currentTimeMillis();
    List<Message> messages = peekMessages(connection, queueName, count);
    while (messages.size() < count && ((System.currentTimeMillis() - start) < timeout)) {
        Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
        messages = peekMessages(connection, queueName, count);
    }
    if (messages.isEmpty()) {
        return messages;
    }
    List<Message> poppedMessages = new ArrayList<>();
    for (Message message : messages) {
        final String POP_MESSAGE = "UPDATE queue_message SET popped = true WHERE queue_name = ? AND message_id = ? AND popped = false";
        int result = query(connection, POP_MESSAGE, q -> q.addParameter(queueName).addParameter(message.getId()).executeUpdate());
        if (result == 1) {
            poppedMessages.add(message);
        }
    }
    return poppedMessages;
}
Also used : Message(com.netflix.conductor.core.events.queue.Message) ArrayList(java.util.ArrayList)

Example 22 with Message

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

the class PostgresQueueDAO method peekMessages.

private List<Message> peekMessages(Connection connection, String queueName, int count) {
    if (count < 1)
        return Collections.emptyList();
    final String PEEK_MESSAGES = "SELECT message_id, priority, payload FROM queue_message WHERE queue_name = ? AND popped = false AND deliver_on <= (current_timestamp + (1000 ||' microseconds')::interval) ORDER BY priority DESC, deliver_on, created_on LIMIT ? FOR UPDATE SKIP LOCKED";
    List<Message> messages = query(connection, PEEK_MESSAGES, p -> p.addParameter(queueName).addParameter(count).executeAndFetch(rs -> {
        List<Message> results = new ArrayList<>();
        while (rs.next()) {
            Message m = new Message();
            m.setId(rs.getString("message_id"));
            m.setPriority(rs.getInt("priority"));
            m.setPayload(rs.getString("payload"));
            results.add(m);
        }
        return results;
    }));
    return messages;
}
Also used : Message(com.netflix.conductor.core.events.queue.Message) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Connection(java.sql.Connection) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) TimeUnit(java.util.concurrent.TimeUnit) QueueDAO(com.netflix.conductor.dao.QueueDAO) List(java.util.List) Map(java.util.Map) DataSource(javax.sql.DataSource) Collections(java.util.Collections) Message(com.netflix.conductor.core.events.queue.Message) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with Message

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

the class WorkflowStatusPublisherIntegrationTest method testListenerOnCompletedWorkflow.

@Test
public void testListenerOnCompletedWorkflow() throws IOException, InterruptedException {
    clearWorkflows();
    WorkflowDef def = new WorkflowDef();
    def.setName(LINEAR_WORKFLOW_T1_T2);
    def.setDescription(def.getName());
    def.setVersion(WORKFLOW_VERSION);
    def.setSchemaVersion(2);
    def.setWorkflowStatusListenerEnabled(true);
    LinkedList<WorkflowTask> wftasks = new LinkedList<>();
    WorkflowTask wft1 = new WorkflowTask();
    wft1.setName("junit_task_1");
    wft1.setTaskReferenceName("t1");
    wftasks.add(wft1);
    def.setTasks(wftasks);
    metadataService.updateWorkflowDef(Collections.singletonList(def));
    String id = startOrLoadWorkflowExecution(def.getName(), 1, "testWorkflowCompletedListener", new HashMap<>(), null, null);
    List<Task> tasks = workflowExecutionService.getTasks("junit_task_1", null, 1);
    tasks.get(0).setStatus(COMPLETED);
    workflowExecutionService.updateTask(tasks.get(0));
    checkIfWorkflowIsCompleted(id);
    List<Message> callbackMessages = queueDAO.pollMessages(CALLBACK_QUEUE, 1, 200);
    queueDAO.ack(CALLBACK_QUEUE, callbackMessages.get(0).getId());
    WorkflowSummary payload = mapper.readValue(callbackMessages.get(0).getPayload(), WorkflowSummary.class);
    assertEquals(id, callbackMessages.get(0).getId());
    assertEquals(LINEAR_WORKFLOW_T1_T2, payload.getWorkflowType());
    assertEquals("testWorkflowCompletedListener", payload.getCorrelationId());
    assertEquals(Workflow.WorkflowStatus.COMPLETED, payload.getStatus());
}
Also used : WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Message(com.netflix.conductor.core.events.queue.Message) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 24 with Message

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

the class Event method start.

@Override
public void start(Workflow workflow, Task task, WorkflowExecutor provider) {
    Map<String, Object> payload = new HashMap<>(task.getInputData());
    payload.put("workflowInstanceId", workflow.getWorkflowId());
    payload.put("workflowType", workflow.getWorkflowName());
    payload.put("workflowVersion", workflow.getWorkflowVersion());
    payload.put("correlationId", workflow.getCorrelationId());
    String payloadJson;
    try {
        payloadJson = objectMapper.writeValueAsString(payload);
    } catch (JsonProcessingException e) {
        String msg = String.format("Error serializing JSON payload for task: %s, workflow: %s", task.getTaskId(), workflow.getWorkflowId());
        throw new ApplicationException(INTERNAL_ERROR, msg);
    }
    Message message = new Message(task.getTaskId(), payloadJson, task.getTaskId());
    ObservableQueue queue = getQueue(workflow, task);
    if (queue != null) {
        queue.publish(Collections.singletonList(message));
        logger.debug("Published message:{} to queue:{}", message.getId(), queue.getName());
        task.getOutputData().putAll(payload);
        if (isAsyncComplete(task)) {
            task.setStatus(Status.IN_PROGRESS);
        } else {
            task.setStatus(Status.COMPLETED);
        }
    } else {
        task.setReasonForIncompletion("No queue found to publish.");
        task.setStatus(Status.FAILED);
    }
}
Also used : ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 25 with Message

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

the class TestEvent method testAsyncComplete.

@Test
public void testAsyncComplete() throws Exception {
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(testWorkflowDefinition);
    Task task = new Task();
    task.getInputData().put("sink", "conductor");
    task.getInputData().put("asyncComplete", true);
    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<List<String>>) invocation -> {
        String messageId = invocation.getArgument(1, String.class);
        if (publishedMessages.get(0).getId().equals(messageId)) {
            publishedMessages.remove(0);
            return Collections.singletonList(messageId);
        }
        return null;
    }).when(dao).remove(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(Status.IN_PROGRESS, 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());
}
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)

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