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