Search in sources :

Example 6 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse 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;
}
Also used : TException(org.apache.thrift.TException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

GetWorkflowExecutionHistoryResponse (com.uber.cadence.GetWorkflowExecutionHistoryResponse)6 GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)3 History (com.uber.cadence.History)3 HistoryEvent (com.uber.cadence.HistoryEvent)3 TException (org.apache.thrift.TException)3 EntityNotExistsError (com.uber.cadence.EntityNotExistsError)2 WorkflowExecution (com.uber.cadence.WorkflowExecution)2 TimeoutException (java.util.concurrent.TimeoutException)2 BadRequestError (com.uber.cadence.BadRequestError)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 CompletableFuture (java.util.concurrent.CompletableFuture)1