Return workflow response with activities running in background

Hi,

I have a scenario where my workflow consists of 4 activity calls (A,B,C,D) where A is sync while other three are async calls. I want the client to get the sync response of A immediately while the rest three activities can run in background.
Is there any way to achieve this? As per my understanding, if a workflow returns a response, it gets marked as completed even when the rest of the activities are still in scheduled/running state.
My requirement is to return the sync response of A, but rest of the activities should be executed and then only workflow to be marked as completed.

We plan to add direct support for this use case. Currently the following workaround is recommended:

  1. Invoke a workflow synchronously.
  2. This workflow executes activity A and then starts a child workflow asynchronously in the abandoned mode.
  3. Initial workflow completes unblocking the caller.
  4. The child workflow executes activities B, C , D asynchronously.

Yeah, i get your point. But my requirement is to compensate activity A if in case any of the rest 3 calls fail. Can that be handled?

The child workflow can compensate the activity A if any of the B, C, D fail.

2 Likes

@maxim Thanks for the response.
Followup queries :

  1. To compensate Activity A call, that needs to be handled in child workflow definition as part of failure handling? Because the parent workflow must have got completed, so there would be no point in defining the compensation in parent workflow. Is my understanding correct?
  2. How can we get the response being returned from the activity being executed as part of compensating SAGA transaction ?
  3. Does the SAGA compensating transaction get logged in workflow execution history?
  1. Yes, you would do the compensation in the child workflow (if any of B,C,D fail as you mentioned)
  2. One way you could do it is have the parent return the WorkflowExecution of the async child back to the client, see the async child sample, specifically:
    samples-java/Starter.java at main · temporalio/samples-java · GitHub
    With that, you can reconnect to the child workflow in the client code and wait for its results (or query it if you want) via:
// connect to the async child wf execution after parent returned
WorkflowStub childStub =
        client.newUntypedWorkflowStub(
            childWorkflowExecution.getWorkflowId(),
            Optional.of(childWorkflowExecution.getRunId()),
            Optional.empty());

    // wait for the child workflow to complete
    String childResult = childStub.getResult(String.class); // you can use childStub.query(...) to query as well
  1. Yes, if your compensation defines invocations of child workflows or activities, they will be recorded in history.

Thanks @tihomir for the explanation.
But point 2, let me clarify my question. If any activity call is being made, using saga.compensate() function, is there any way to fetch the result of that activity call. Because the compensate() call returns void, so trying to understand that.

saga.compensate() triggers compensation which can include multiple activity invocations.
You can get results of each in saga.addCompensation, for example:

saga.addCompensation(
        () -> {
          sagaCompensateResult = activity.compensate(myCompensateActivityInput);
        });
1 Like