Best way to create an async child workflow

What’s the best way to start a child workflow async?

The important thing is to start the workflow, then the child workflow does not need to be associated with the parent workflow any more.

Currently, it is not straightforward. We have plans to improve the experience (relevant issue), but in the meantime, the following should be done.

Java SDK

  1. Set ChildWorkflowOptions.cancellationType to ABANDON when creating a child workflow stub.
  2. Start child workflow asynchronously using Async.function or Async.procedure.
  3. Call Workflow.getWorkflowExecution(…) on the child stub
  4. Wait for the Promise returned by getWorkflowExecution to complete. This indicates that the child successfully started (or start failed).
  5. Complete parent workflow.

Steps 3 and 4 are needed to ensure that child workflow starts before the parent closes. If the parent initiates child workflow and immediately completes the child would never start. Here is the code that demonstrates the steps:

   public void parentWorklfow() {
       ChildWorkflowOptions options =
          ChildWorkflowOptions.newBuilder()
              .setCancellationType(ChildWorkflowCancellationType.ABANDON)
              .build();
       MyChildWorkflow child = Workflow.newChildWorkflowStub(MyChildWorkflow.class, options);
       Promise<WorkflowExecution> childExecution = Workflow.getWorkflowExecution(child);
       // Wait for child to start
       childExecution.get()
  }

Go SDK

  1. Start child workflow using ExecuteChildWorkflow
  2. Call GetChildWorkflowExecution on the ChildWorkflowFuture returned by the ExecuteChildWorkflow
  3. Wait on the WorkflowExecution Future. This indicates that the child successfully started (or start failed).
  4. Complete parent workflow

Steps 3 and 4 are needed to ensure that child workflow starts before the parent closes. If the parent initiates child workflow and immediately completes the child would never start. Here is the code that demonstrates the steps (error handling omitted for brevity):

func ParentWorkflow(ctx workflow.Context) error {
    childWorkflow := workflow.ExecuteChildWorkflow(ctx, MyChildWorkflow)
    // Wait for child to start
    _ = childWorkflow.GetChildWorkflowExecution().Get(ctx, nil)
    return nil
}
2 Likes

I am using an activity to do something like that. the activity just submits a workflow and returns! of course in my usecase, i really dont care about the outcome of the child workflow, hence it kind of works for me… but don know the consequences . @maxim any suggestions?

@madhu It also works, you should be careful about assigning ID to the child when starting it this way to avoid duplicated workflows if start times out for some reason.