Fetching Workflow Result

How can I get a workflow’s result if I don’t know what the result type will be (or if there even is one)? Here is my current code:

    public String getWorkflowResult(WorkflowExecutionInfo executionInfo, String workflowId, String runId) {

        var status = executionInfo.getStatus();

        Optional<String> optRunId = Optional.ofNullable(runId).filter(s -> !s.isEmpty());
        WorkflowStub workflowStub = client.newUntypedWorkflowStub(workflowId, optRunId, Optional.empty());

        return switch (status) {
            case WORKFLOW_EXECUTION_STATUS_TERMINATED, WORKFLOW_EXECUTION_STATUS_TIMED_OUT, WORKFLOW_EXECUTION_STATUS_CANCELED, WORKFLOW_EXECUTION_STATUS_RUNNING ->
                    "void";
            default -> workflowStub.getResult(Object.class).toString();
        };

    }

This is called for each run of a workflow, but it somehow is running forever.

Hi @eric-gassel

I am not aware right now of any way to infer the type a workflow returns if you don’t have access to the workflow interface.

what do you mean by this?

getResult is a blocking operation. If the workflow status == WORKFLOW_EXECUTION_STATUS_RUNNING, it will wait/block your code until the workflow is completed. For any other status != WORKFLOW_EXECUTION_STATUS_COMPLETED (and WORKFLOW_EXECUTION_STATUS_RUNNING) the invocation to getResult will throw an exception (You might want to add WORKFLOW_EXECUTION_STATUS_FAILED to your switch)

Looking at the getResult Javadoc, I would rely on exception handling rather than WorkflowExecutionInfo.getStatus()

try{
   return getResult... 
}catch(Exception e){
   return "void";
}

The reason is that the SDK query the visibility store and there is a delay between the workflow data and the data that the visibility store shows (i.e. executionInfo.getStatus() could be = WORKFLOW_EXECUTION_STATUS_RUNNING in the visibility store, but the actual workflow is actually completed).

You can implement your own DataConverter that is going to deserialize results of unknown types into the Object type.