Search in sources :

Example 26 with WorkflowExecution

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

Example 27 with WorkflowExecution

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

Example 28 with WorkflowExecution

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);
    }
}
Also used : TException(org.apache.thrift.TException) WorkflowQuery(com.uber.cadence.WorkflowQuery) QueryWorkflowResponse(com.uber.cadence.QueryWorkflowResponse) WorkflowExecution(com.uber.cadence.WorkflowExecution) QueryWorkflowRequest(com.uber.cadence.QueryWorkflowRequest)

Example 29 with WorkflowExecution

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);
    }
}
Also used : TException(org.apache.thrift.TException) SignalWorkflowExecutionRequest(com.uber.cadence.SignalWorkflowExecutionRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution)

Example 30 with WorkflowExecution

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;
}
Also used : TException(org.apache.thrift.TException) TaskList(com.uber.cadence.TaskList) StartWorkflowExecutionResponse(com.uber.cadence.StartWorkflowExecutionResponse) WorkflowExecutionAlreadyStartedError(com.uber.cadence.WorkflowExecutionAlreadyStartedError) WorkflowExecution(com.uber.cadence.WorkflowExecution) StartWorkflowExecutionRequest(com.uber.cadence.StartWorkflowExecutionRequest)

Aggregations

WorkflowExecution (com.uber.cadence.WorkflowExecution)30 CancellationException (java.util.concurrent.CancellationException)11 SignalExternalWorkflowException (com.uber.cadence.workflow.SignalExternalWorkflowException)8 ChildWorkflowTerminatedException (com.uber.cadence.workflow.ChildWorkflowTerminatedException)7 ChildWorkflowTimedOutException (com.uber.cadence.workflow.ChildWorkflowTimedOutException)7 StartChildWorkflowFailedException (com.uber.cadence.workflow.StartChildWorkflowFailedException)7 HistoryEvent (com.uber.cadence.HistoryEvent)6 Test (org.junit.Test)6 TimeoutException (java.util.concurrent.TimeoutException)5 TException (org.apache.thrift.TException)5 WorkflowStub (com.uber.cadence.client.WorkflowStub)4 GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)3 History (com.uber.cadence.History)3 StartChildWorkflowExecutionFailedEventAttributes (com.uber.cadence.StartChildWorkflowExecutionFailedEventAttributes)3 WorkflowExecutionAlreadyStartedError (com.uber.cadence.WorkflowExecutionAlreadyStartedError)3 WorkflowType (com.uber.cadence.WorkflowType)3 DeterministicRunnerTest (com.uber.cadence.internal.sync.DeterministicRunnerTest)3 ChildWorkflowExecutionCanceledEventAttributes (com.uber.cadence.ChildWorkflowExecutionCanceledEventAttributes)2 ChildWorkflowExecutionCompletedEventAttributes (com.uber.cadence.ChildWorkflowExecutionCompletedEventAttributes)2 ChildWorkflowExecutionFailedCause (com.uber.cadence.ChildWorkflowExecutionFailedCause)2