Search in sources :

Example 6 with ObservableQueue

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

the class TestQueueManager method test.

@Test
public void test() throws Exception {
    Map<Status, ObservableQueue> queues = new HashMap<>();
    queues.put(Status.COMPLETED, queue);
    QueueManager qm = new QueueManager(queues, es, objectMapper);
    qm.updateByTaskRefName("v_0", "t0", new HashMap<>(), Status.COMPLETED);
    Uninterruptibles.sleepUninterruptibly(1_000, TimeUnit.MILLISECONDS);
    assertTrue(updatedTasks.stream().anyMatch(task -> task.getTaskId().equals("t0")));
}
Also used : Status(com.netflix.conductor.common.metadata.tasks.Task.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeClass(org.junit.BeforeClass) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) Wait(com.netflix.conductor.core.execution.tasks.Wait) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Task(com.netflix.conductor.common.metadata.tasks.Task) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) LinkedList(java.util.LinkedList) Mockito.doReturn(org.mockito.Mockito.doReturn) 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) QueueManager(com.netflix.conductor.contribs.queue.QueueManager) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ExecutionService(com.netflix.conductor.service.ExecutionService) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) QueueManager(com.netflix.conductor.contribs.queue.QueueManager) Test(org.junit.Test)

Example 7 with ObservableQueue

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

the class TestQueueManager method testWithTaskId.

@Test
public void testWithTaskId() throws Exception {
    Map<Status, ObservableQueue> queues = new HashMap<>();
    queues.put(Status.COMPLETED, queue);
    QueueManager qm = new QueueManager(queues, es, objectMapper);
    qm.updateByTaskId("v_2", "t2", new HashMap<>(), Status.COMPLETED);
    Uninterruptibles.sleepUninterruptibly(1_000, TimeUnit.MILLISECONDS);
    assertTrue(updatedTasks.stream().anyMatch(task -> task.getTaskId().equals("t2")));
}
Also used : Status(com.netflix.conductor.common.metadata.tasks.Task.Status) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeClass(org.junit.BeforeClass) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) Wait(com.netflix.conductor.core.execution.tasks.Wait) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Task(com.netflix.conductor.common.metadata.tasks.Task) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Workflow(com.netflix.conductor.common.run.Workflow) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) LinkedList(java.util.LinkedList) Mockito.doReturn(org.mockito.Mockito.doReturn) 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) QueueManager(com.netflix.conductor.contribs.queue.QueueManager) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ExecutionService(com.netflix.conductor.service.ExecutionService) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) QueueManager(com.netflix.conductor.contribs.queue.QueueManager) Test(org.junit.Test)

Example 8 with ObservableQueue

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

the class QueueManager method size.

public Map<String, Long> size() {
    Map<String, Long> size = new HashMap<>();
    queues.entrySet().forEach(e -> {
        ObservableQueue queue = e.getValue();
        size.put(queue.getName(), queue.size());
    });
    return size;
}
Also used : HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue)

Example 9 with ObservableQueue

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

the class QueueManager method queues.

public Map<Status, String> queues() {
    Map<Status, String> size = new HashMap<>();
    queues.entrySet().forEach(e -> {
        ObservableQueue queue = e.getValue();
        size.put(e.getKey(), queue.getURI());
    });
    return size;
}
Also used : Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue)

Example 10 with ObservableQueue

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

the class QueueManager method startMonitor.

private void startMonitor(Status status, ObservableQueue queue) {
    queue.observe().subscribe((Message msg) -> {
        try {
            logger.debug("Got message {}", msg.getPayload());
            String payload = msg.getPayload();
            JsonNode payloadJSON = objectMapper.readTree(payload);
            String externalId = getValue("externalId", payloadJSON);
            if (externalId == null || "".equals(externalId)) {
                logger.error("No external Id found in the payload {}", payload);
                queue.ack(Collections.singletonList(msg));
                return;
            }
            JsonNode json = objectMapper.readTree(externalId);
            String workflowId = getValue("workflowId", json);
            String taskRefName = getValue("taskRefName", json);
            String taskId = getValue("taskId", json);
            if (workflowId == null || "".equals(workflowId)) {
                // This is a bad message, we cannot process it
                logger.error("No workflow id found in the message. {}", payload);
                queue.ack(Collections.singletonList(msg));
                return;
            }
            Workflow workflow = executionService.getExecutionStatus(workflowId, true);
            Optional<Task> taskOptional;
            if (StringUtils.isNotEmpty(taskId)) {
                taskOptional = workflow.getTasks().stream().filter(task -> !task.getStatus().isTerminal() && task.getTaskId().equals(taskId)).findFirst();
            } else if (StringUtils.isEmpty(taskRefName)) {
                logger.error("No taskRefName found in the message. If there is only one WAIT task, will mark it as completed. {}", payload);
                taskOptional = workflow.getTasks().stream().filter(task -> !task.getStatus().isTerminal() && task.getTaskType().equals(Wait.NAME)).findFirst();
            } else {
                taskOptional = workflow.getTasks().stream().filter(task -> !task.getStatus().isTerminal() && task.getReferenceTaskName().equals(taskRefName)).findFirst();
            }
            if (!taskOptional.isPresent()) {
                logger.error("No matching tasks found to be marked as completed for workflow {}, taskRefName {}, taskId {}", workflowId, taskRefName, taskId);
                queue.ack(Collections.singletonList(msg));
                return;
            }
            Task task = taskOptional.get();
            task.setStatus(status);
            task.getOutputData().putAll(objectMapper.convertValue(payloadJSON, _mapType));
            executionService.updateTask(task);
            List<String> failures = queue.ack(Collections.singletonList(msg));
            if (!failures.isEmpty()) {
                logger.error("Not able to ack the messages {}", failures.toString());
            }
        } catch (JsonParseException e) {
            logger.error("Bad message? : {} ", msg, e);
            queue.ack(Collections.singletonList(msg));
        } catch (ApplicationException e) {
            if (e.getCode().equals(Code.NOT_FOUND)) {
                logger.error("Workflow ID specified is not valid for this environment");
                queue.ack(Collections.singletonList(msg));
            }
            logger.error("Error processing message: {}", msg, e);
        } catch (Exception e) {
            logger.error("Error processing message: {}", msg, e);
        }
    }, (Throwable t) -> {
        logger.error(t.getMessage(), t);
    });
    logger.info("QueueListener::STARTED...listening for " + queue.getName());
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) HashMap(java.util.HashMap) Wait(com.netflix.conductor.core.execution.tasks.Wait) Singleton(javax.inject.Singleton) Task(com.netflix.conductor.common.metadata.tasks.Task) StringUtils(org.apache.commons.lang3.StringUtils) Inject(javax.inject.Inject) Workflow(com.netflix.conductor.common.run.Workflow) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Code(com.netflix.conductor.core.execution.ApplicationException.Code) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UUID(java.util.UUID) List(java.util.List) Optional(java.util.Optional) ExecutionService(com.netflix.conductor.service.ExecutionService) Collections(java.util.Collections) ObservableQueue(com.netflix.conductor.core.events.queue.ObservableQueue) Task(com.netflix.conductor.common.metadata.tasks.Task) ApplicationException(com.netflix.conductor.core.execution.ApplicationException) Message(com.netflix.conductor.core.events.queue.Message) Workflow(com.netflix.conductor.common.run.Workflow) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ApplicationException(com.netflix.conductor.core.execution.ApplicationException)

Aggregations

ObservableQueue (com.netflix.conductor.core.events.queue.ObservableQueue)14 HashMap (java.util.HashMap)10 Test (org.junit.Test)7 Task (com.netflix.conductor.common.metadata.tasks.Task)6 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)6 Workflow (com.netflix.conductor.common.run.Workflow)5 Message (com.netflix.conductor.core.events.queue.Message)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 QueueManager (com.netflix.conductor.contribs.queue.QueueManager)3 Wait (com.netflix.conductor.core.execution.tasks.Wait)3 ExecutionService (com.netflix.conductor.service.ExecutionService)3 List (java.util.List)3 Map (java.util.Map)3 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)2 Provides (com.google.inject.Provides)2 JsonMapperProvider (com.netflix.conductor.common.utils.JsonMapperProvider)2 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)2 LinkedList (java.util.LinkedList)2 TimeUnit (java.util.concurrent.TimeUnit)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2