use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.
the class WorkflowExecutionUtils method getInstanceCloseEventAsync.
private static CompletableFuture<HistoryEvent> getInstanceCloseEventAsync(IWorkflowService service, String domain, final WorkflowExecution workflowExecution, byte[] pageToken, long timeout, TimeUnit unit) {
// TODO: Interrupt service long poll call on timeout and on interrupt
long start = System.currentTimeMillis();
CompletableFuture<HistoryEvent> result = new CompletableFuture<>();
GetWorkflowExecutionHistoryRequest request = new GetWorkflowExecutionHistoryRequest();
request.setDomain(domain);
request.setExecution(workflowExecution);
request.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
request.setNextPageToken(pageToken);
CompletableFuture<GetWorkflowExecutionHistoryResponse> response = getWorkflowExecutionHistoryAsync(service, request);
return response.thenComposeAsync((r) -> {
if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
throw CheckedExceptionWrapper.wrap(new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit));
}
History history = r.getHistory();
if (history == null || history.getEvents().size() == 0) {
// Empty poll returned
return getInstanceCloseEventAsync(service, domain, workflowExecution, pageToken, timeout, unit);
}
HistoryEvent event = history.getEvents().get(0);
if (!isWorkflowExecutionCompletedEvent(event)) {
throw new RuntimeException("Last history event is not completion event: " + event);
}
// Workflow called continueAsNew. Start polling the new generation with new runId.
if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
WorkflowExecution nextWorkflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
return getInstanceCloseEventAsync(service, domain, nextWorkflowExecution, r.getNextPageToken(), timeout, unit);
}
return CompletableFuture.completedFuture(event);
});
}
use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.
the class WorkflowExecutionUtils method getInstanceCloseEvent.
/**
* Returns an instance closing event, potentially waiting for workflow to complete.
*/
public static HistoryEvent getInstanceCloseEvent(IWorkflowService service, String domain, WorkflowExecution workflowExecution, long timeout, TimeUnit unit) throws TimeoutException, EntityNotExistsError {
byte[] pageToken = null;
GetWorkflowExecutionHistoryResponse response;
// TODO: Interrupt service long poll call on timeout and on interrupt
long start = System.currentTimeMillis();
HistoryEvent event;
do {
GetWorkflowExecutionHistoryRequest r = new GetWorkflowExecutionHistoryRequest();
r.setDomain(domain);
r.setExecution(workflowExecution);
r.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
r.setNextPageToken(pageToken);
try {
response = Retryer.retryWithResult(retryParameters, () -> service.GetWorkflowExecutionHistory(r));
} catch (EntityNotExistsError e) {
throw e;
} catch (TException e) {
throw CheckedExceptionWrapper.wrap(e);
}
if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
throw new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit);
}
pageToken = response.getNextPageToken();
History history = response.getHistory();
if (history != null && history.getEvents().size() > 0) {
event = history.getEvents().get(0);
if (!isWorkflowExecutionCompletedEvent(event)) {
throw new RuntimeException("Last history event is not completion event: " + event);
}
// Workflow called continueAsNew. Start polling the new generation with new runId.
if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
pageToken = null;
workflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
continue;
}
break;
}
} while (true);
return event;
}
use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.
the class GenericWorkflowClientExternalImpl method queryWorkflow.
@Override
public byte[] queryWorkflow(QueryWorkflowParameters queryParameters) {
QueryWorkflowRequest request = new QueryWorkflowRequest();
request.setDomain(domain);
WorkflowExecution execution = new WorkflowExecution();
execution.setWorkflowId(queryParameters.getWorkflowId()).setRunId(queryParameters.getRunId());
request.setExecution(execution);
WorkflowQuery query = new WorkflowQuery();
query.setQueryArgs(queryParameters.getInput());
query.setQueryType(queryParameters.getQueryType());
request.setQuery(query);
try {
QueryWorkflowResponse response = service.QueryWorkflow(request);
return response.getQueryResult();
} catch (TException e) {
throw CheckedExceptionWrapper.wrap(e);
}
}
use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.
the class GenericWorkflowClientExternalImpl method signalWorkflowExecution.
@Override
public void signalWorkflowExecution(SignalExternalWorkflowParameters signalParameters) {
SignalWorkflowExecutionRequest request = new SignalWorkflowExecutionRequest();
request.setDomain(domain);
request.setInput(signalParameters.getInput());
request.setSignalName(signalParameters.getSignalName());
WorkflowExecution execution = new WorkflowExecution();
execution.setRunId(signalParameters.getRunId());
execution.setWorkflowId(signalParameters.getWorkflowId());
request.setWorkflowExecution(execution);
try {
service.SignalWorkflowExecution(request);
} catch (TException e) {
throw CheckedExceptionWrapper.wrap(e);
}
}
use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.
the class GenericWorkflowClientExternalImpl method startWorkflow.
@Override
public WorkflowExecution startWorkflow(StartWorkflowExecutionParameters startParameters) throws WorkflowExecutionAlreadyStartedError {
StartWorkflowExecutionRequest request = new StartWorkflowExecutionRequest();
request.setDomain(domain);
request.setInput(startParameters.getInput());
request.setExecutionStartToCloseTimeoutSeconds((int) startParameters.getExecutionStartToCloseTimeoutSeconds());
request.setTaskStartToCloseTimeoutSeconds((int) startParameters.getTaskStartToCloseTimeoutSeconds());
request.setWorkflowIdReusePolicy(startParameters.getWorkflowIdReusePolicy());
String taskList = startParameters.getTaskList();
if (taskList != null && !taskList.isEmpty()) {
TaskList tl = new TaskList();
tl.setName(taskList);
request.setTaskList(tl);
}
String workflowId = startParameters.getWorkflowId();
if (workflowId == null) {
workflowId = UUID.randomUUID().toString();
}
request.setWorkflowId(workflowId);
request.setWorkflowType(startParameters.getWorkflowType());
// if(startParameters.getChildPolicy() != null) {
// request.setChildPolicy(startParameters.getChildPolicy());
// }
StartWorkflowExecutionResponse result;
try {
result = service.StartWorkflowExecution(request);
} catch (WorkflowExecutionAlreadyStartedError e) {
throw e;
} catch (TException e) {
throw CheckedExceptionWrapper.wrap(e);
}
WorkflowExecution execution = new WorkflowExecution();
execution.setRunId(result.getRunId());
execution.setWorkflowId(request.getWorkflowId());
return execution;
}
Aggregations