Search in sources :

Example 1 with WorkflowExecution

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

the class WorkflowExecutionUtils method waitForWorkflowInstanceCompletionAcrossGenerations.

/**
 * Like {@link #waitForWorkflowInstanceCompletion(IWorkflowService, String, WorkflowExecution,
 * long, TimeUnit)} , except will wait for continued generations of the original workflow
 * execution too.
 *
 * @see #waitForWorkflowInstanceCompletion(IWorkflowService, String, WorkflowExecution, long,
 *     TimeUnit)
 */
public static WorkflowExecutionCloseStatus waitForWorkflowInstanceCompletionAcrossGenerations(IWorkflowService service, String domain, WorkflowExecution workflowExecution, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, EntityNotExistsError {
    WorkflowExecution lastExecutionToRun = workflowExecution;
    long millisecondsAtFirstWait = System.currentTimeMillis();
    WorkflowExecutionCloseStatus lastExecutionToRunCloseStatus = waitForWorkflowInstanceCompletion(service, domain, lastExecutionToRun, timeout, unit);
    // keep waiting if the instance continued as new
    while (lastExecutionToRunCloseStatus == WorkflowExecutionCloseStatus.CONTINUED_AS_NEW) {
        // get the new execution's information
        HistoryEvent closeEvent = getInstanceCloseEvent(service, domain, lastExecutionToRun, timeout, unit);
        WorkflowExecutionContinuedAsNewEventAttributes continuedAsNewAttributes = closeEvent.getWorkflowExecutionContinuedAsNewEventAttributes();
        WorkflowExecution newGenerationExecution = new WorkflowExecution();
        newGenerationExecution.setRunId(continuedAsNewAttributes.getNewExecutionRunId());
        newGenerationExecution.setWorkflowId(lastExecutionToRun.getWorkflowId());
        // and wait for it
        long currentTime = System.currentTimeMillis();
        long millisecondsSinceFirstWait = currentTime - millisecondsAtFirstWait;
        long timeoutInSecondsForNextWait = unit.toMillis(timeout) - (millisecondsSinceFirstWait / 1000L);
        lastExecutionToRunCloseStatus = waitForWorkflowInstanceCompletion(service, domain, newGenerationExecution, timeoutInSecondsForNextWait, TimeUnit.MILLISECONDS);
        lastExecutionToRun = newGenerationExecution;
    }
    return lastExecutionToRunCloseStatus;
}
Also used : WorkflowExecutionCloseStatus(com.uber.cadence.WorkflowExecutionCloseStatus) WorkflowExecution(com.uber.cadence.WorkflowExecution) HistoryEvent(com.uber.cadence.HistoryEvent) WorkflowExecutionContinuedAsNewEventAttributes(com.uber.cadence.WorkflowExecutionContinuedAsNewEventAttributes)

Example 2 with WorkflowExecution

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

the class GenericWorkflowClientExternalImpl method terminateWorkflowExecution.

@Override
public void terminateWorkflowExecution(TerminateWorkflowExecutionParameters terminateParameters) {
    TerminateWorkflowExecutionRequest request = new TerminateWorkflowExecutionRequest();
    WorkflowExecution workflowExecution = terminateParameters.getWorkflowExecution();
    request.setWorkflowExecution(terminateParameters.getWorkflowExecution());
    request.setDomain(domain);
    request.setDetails(terminateParameters.getDetails());
    request.setReason(terminateParameters.getReason());
    // request.setChildPolicy(terminateParameters.getChildPolicy());
    try {
        service.TerminateWorkflowExecution(request);
    } catch (TException e) {
        throw CheckedExceptionWrapper.wrap(e);
    }
}
Also used : TException(org.apache.thrift.TException) WorkflowExecution(com.uber.cadence.WorkflowExecution) TerminateWorkflowExecutionRequest(com.uber.cadence.TerminateWorkflowExecutionRequest)

Example 3 with WorkflowExecution

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

the class WorkflowDecisionContext method handleChildWorkflowExecutionCanceled.

void handleChildWorkflowExecutionCanceled(HistoryEvent event) {
    ChildWorkflowExecutionCanceledEventAttributes attributes = event.getChildWorkflowExecutionCanceledEventAttributes();
    WorkflowExecution execution = attributes.getWorkflowExecution();
    String workflowId = execution.getWorkflowId();
    if (decisions.handleChildWorkflowExecutionCanceled(workflowId)) {
        OpenChildWorkflowRequestInfo scheduled = scheduledExternalWorkflows.remove(workflowId);
        if (scheduled != null) {
            CancellationException e = new CancellationException();
            BiConsumer<byte[], Exception> completionCallback = scheduled.getCompletionCallback();
            completionCallback.accept(null, e);
        }
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) WorkflowExecution(com.uber.cadence.WorkflowExecution) ChildWorkflowExecutionCanceledEventAttributes(com.uber.cadence.ChildWorkflowExecutionCanceledEventAttributes) StartChildWorkflowFailedException(com.uber.cadence.workflow.StartChildWorkflowFailedException) ChildWorkflowTerminatedException(com.uber.cadence.workflow.ChildWorkflowTerminatedException) CancellationException(java.util.concurrent.CancellationException) ChildWorkflowTimedOutException(com.uber.cadence.workflow.ChildWorkflowTimedOutException) SignalExternalWorkflowException(com.uber.cadence.workflow.SignalExternalWorkflowException)

Example 4 with WorkflowExecution

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

the class WorkflowDecisionContext method handleChildWorkflowExecutionTerminated.

void handleChildWorkflowExecutionTerminated(HistoryEvent event) {
    ChildWorkflowExecutionTerminatedEventAttributes attributes = event.getChildWorkflowExecutionTerminatedEventAttributes();
    WorkflowExecution execution = attributes.getWorkflowExecution();
    String workflowId = execution.getWorkflowId();
    if (decisions.handleChildWorkflowExecutionClosed(workflowId)) {
        OpenChildWorkflowRequestInfo scheduled = scheduledExternalWorkflows.remove(workflowId);
        if (scheduled != null) {
            RuntimeException failure = new ChildWorkflowTerminatedException(event.getEventId(), execution, attributes.getWorkflowType());
            BiConsumer<byte[], Exception> completionCallback = scheduled.getCompletionCallback();
            completionCallback.accept(null, failure);
        }
    }
}
Also used : WorkflowExecution(com.uber.cadence.WorkflowExecution) StartChildWorkflowFailedException(com.uber.cadence.workflow.StartChildWorkflowFailedException) ChildWorkflowTerminatedException(com.uber.cadence.workflow.ChildWorkflowTerminatedException) CancellationException(java.util.concurrent.CancellationException) ChildWorkflowTimedOutException(com.uber.cadence.workflow.ChildWorkflowTimedOutException) SignalExternalWorkflowException(com.uber.cadence.workflow.SignalExternalWorkflowException) ChildWorkflowExecutionTerminatedEventAttributes(com.uber.cadence.ChildWorkflowExecutionTerminatedEventAttributes) ChildWorkflowTerminatedException(com.uber.cadence.workflow.ChildWorkflowTerminatedException)

Example 5 with WorkflowExecution

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

the class ReplayDecisionTaskHandler method handleDecisionTaskImpl.

private Result handleDecisionTaskImpl(DecisionTaskWithHistoryIterator decisionTaskIterator) throws Throwable {
    HistoryHelper historyHelper = new HistoryHelper(decisionTaskIterator);
    ReplayDecider decider = createDecider(historyHelper);
    PollForDecisionTaskResponse decisionTask = historyHelper.getDecisionTask();
    if (decisionTask.isSetQuery()) {
        RespondQueryTaskCompletedRequest queryCompletedRequest = new RespondQueryTaskCompletedRequest();
        queryCompletedRequest.setTaskToken(decisionTask.getTaskToken());
        try {
            byte[] queryResult = decider.query(decisionTask.getQuery());
            queryCompletedRequest.setQueryResult(queryResult);
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.COMPLETED);
        } catch (Throwable e) {
            // TODO: Appropriate exception serialization.
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            queryCompletedRequest.setErrorMessage(sw.toString());
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.FAILED);
        }
        return new DecisionTaskHandler.Result(null, null, queryCompletedRequest, null);
    } else {
        decider.decide();
        DecisionsHelper decisionsHelper = decider.getDecisionsHelper();
        List<Decision> decisions = decisionsHelper.getDecisions();
        byte[] context = decisionsHelper.getWorkflowContextDataToReturn();
        if (log.isTraceEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.trace("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + WorkflowExecutionUtils.prettyPrintDecisions(decisions));
        } else if (log.isDebugEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.debug("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + decisions.size() + " new decisions");
        }
        RespondDecisionTaskCompletedRequest completedRequest = new RespondDecisionTaskCompletedRequest();
        completedRequest.setTaskToken(decisionTask.getTaskToken());
        completedRequest.setDecisions(decisions);
        completedRequest.setExecutionContext(context);
        return new DecisionTaskHandler.Result(completedRequest, null, null, null);
    }
}
Also used : PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) Decision(com.uber.cadence.Decision) StringWriter(java.io.StringWriter) RespondQueryTaskCompletedRequest(com.uber.cadence.RespondQueryTaskCompletedRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) RespondDecisionTaskCompletedRequest(com.uber.cadence.RespondDecisionTaskCompletedRequest) PrintWriter(java.io.PrintWriter)

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