Async workflow invocation and long polling for result

Let’s say I kick off a Workflow async via WorkflowClient#start and then I want to wait for the workflow to complete (but it may take an hour or more for the workflow to finish since downstream services called from activities can be unavailable for lengthy periods of time) - what would be the ideal way to poll the workflow status? The docs seem to recommend calling the blocking method, but I don’t think that will work well when a workflow is so long running. I suppose use the WorkflowExecution to check the status of the workflow?

2 Likes

I believe WorkflowExecution is the best way of checking the status after starting a workflow asynchronously. Adding Max to double check.

If you want to wait for workflow to complete don’t use start, but run the workflow method synchronously. Behind the scenes, it is going to perform long polls waiting for its completion. So it is pretty efficient.

For long poll GetWorkflowExecutionHistory is used. When GetWorkflowExecutionHistoryRequest.WaitNewEvent property is set to true the request becomes a long poll request not returning for a minute if there a no new events since the last page. This is used by UI and CLI to stream history as it happens.

When GetWorkflowExecutionHistoryRequest.HistoryEventFilterType is set to CLOSE_EVENT then only the workflow completion event is returned from the API.

So setting both WaitNewEvent to true and HistoryEventFilterType to CLOSE_EVENT makes GetWorkflowExecutionHistory a long poll request that can be used to wait for workflow completion.

Here is the actual implementation of that logic in JavaSDK.

Another way to wait for any workflow completion giving its ID is using untyped workflow stub:

WorkflowStub workflowStub = workflowClient.newUntypedWorkflowStub(execution, Optional.empty());
<ResultType> result = workflowStub.getResult(<ResultType>.class);
1 Like