Search in sources :

Example 1 with WorkflowExecutionCloseStatus

use of com.uber.cadence.WorkflowExecutionCloseStatus 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)

Aggregations

HistoryEvent (com.uber.cadence.HistoryEvent)1 WorkflowExecution (com.uber.cadence.WorkflowExecution)1 WorkflowExecutionCloseStatus (com.uber.cadence.WorkflowExecutionCloseStatus)1 WorkflowExecutionContinuedAsNewEventAttributes (com.uber.cadence.WorkflowExecutionContinuedAsNewEventAttributes)1