Search in sources :

Example 1 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse in project cadence-client by uber-java.

the class WorkflowExecutionUtils method getHistoryPage.

public static GetWorkflowExecutionHistoryResponse getHistoryPage(byte[] nextPageToken, IWorkflowService service, String domain, WorkflowExecution workflowExecution) {
    GetWorkflowExecutionHistoryRequest getHistoryRequest = new GetWorkflowExecutionHistoryRequest();
    getHistoryRequest.setDomain(domain);
    getHistoryRequest.setExecution(workflowExecution);
    getHistoryRequest.setNextPageToken(nextPageToken);
    GetWorkflowExecutionHistoryResponse history;
    try {
        history = service.GetWorkflowExecutionHistory(getHistoryRequest);
    } catch (TException e) {
        throw new Error(e);
    }
    if (history == null) {
        throw new IllegalArgumentException("unknown workflow execution: " + workflowExecution);
    }
    return history;
}
Also used : TException(org.apache.thrift.TException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) BadRequestError(com.uber.cadence.BadRequestError) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse)

Example 2 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse in project cadence-client by uber-java.

the class TestWorkflowService method GetWorkflowExecutionHistory.

// Generator ignores that AsyncMethodCallback is generic
@SuppressWarnings("unchecked")
@Override
public void GetWorkflowExecutionHistory(GetWorkflowExecutionHistoryRequest getRequest, AsyncMethodCallback resultHandler) throws TException {
    ForkJoinPool.commonPool().execute(() -> {
        try {
            GetWorkflowExecutionHistoryResponse result = GetWorkflowExecutionHistory(getRequest);
            resultHandler.onComplete(result);
        } catch (TException e) {
            resultHandler.onError(e);
        }
    });
}
Also used : TException(org.apache.thrift.TException) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse)

Example 3 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse in project cadence-client by uber-java.

the class TestWorkflowStoreImpl method getWorkflowExecutionHistory.

@Override
public GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory(ExecutionId executionId, GetWorkflowExecutionHistoryRequest getRequest) throws EntityNotExistsError {
    HistoryStore history;
    // Used to eliminate the race condition on waitForNewEvents
    long expectedNextEventId;
    lock.lock();
    try {
        history = getHistoryStore(executionId);
        if (!getRequest.isWaitForNewEvent() && getRequest.getHistoryEventFilterType() != HistoryEventFilterType.CLOSE_EVENT) {
            List<HistoryEvent> events = history.getEventsLocked();
            // Copy the list as it is mutable. Individual events assumed immutable.
            ArrayList<HistoryEvent> eventsCopy = new ArrayList<>(events);
            return new GetWorkflowExecutionHistoryResponse().setHistory(new History().setEvents(eventsCopy));
        }
        expectedNextEventId = history.getNextEventIdLocked();
    } finally {
        lock.unlock();
    }
    List<HistoryEvent> events = history.waitForNewEvents(expectedNextEventId, getRequest.getHistoryEventFilterType());
    GetWorkflowExecutionHistoryResponse result = new GetWorkflowExecutionHistoryResponse();
    if (events != null) {
        result.setHistory(new History().setEvents(events));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History)

Example 4 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse in project cadence-client by uber-java.

the class WorkflowExecutionUtils method getHistory.

public static Iterator<HistoryEvent> getHistory(IWorkflowService service, String domain, WorkflowExecution workflowExecution) {
    return new Iterator<HistoryEvent>() {

        byte[] nextPageToken;

        Iterator<HistoryEvent> current;

        {
            getNextPage();
        }

        @Override
        public boolean hasNext() {
            return current.hasNext() || nextPageToken != null;
        }

        @Override
        public HistoryEvent next() {
            if (current.hasNext()) {
                return current.next();
            }
            getNextPage();
            return current.next();
        }

        private void getNextPage() {
            GetWorkflowExecutionHistoryResponse history = getHistoryPage(nextPageToken, service, domain, workflowExecution);
            current = history.getHistory().getEvents().iterator();
            nextPageToken = history.getNextPageToken();
        }
    };
}
Also used : Iterator(java.util.Iterator) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse)

Example 5 with GetWorkflowExecutionHistoryResponse

use of com.uber.cadence.GetWorkflowExecutionHistoryResponse 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);
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) 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