Adding Child Workflows after Parent has started

Hi,

I’m working on modeling a business use case where, at the moment, I’m creating a parent workflow and N child workflows when the workflow is started; however, after the start of the workflow there may be other requests that come in to add other child workflows.

To help illustrate here’s a made up example that has a similar modeling:

Say we’re building a platform to manage a Competition. Once the competition owner has defined the details and rules of their competition they can invite users and publish for others to join. In Temporal we’d like to model the Competition as a workflow that tracks the lifecycle of the competition (start, end, and key points to notify all participants of) and we’d like to track each participants progress on the competition as a child workflow.

The basic lifecycle of the parent/competition wf might be:

  1. Wait for human to approve the competition
  2. Spin up child workflow to track money movement from 1 account to another so that winners can be paid.
  3. Publish competition to folks that may be interested
  4. Notify specific individuals
  5. Create N child workflows for participants knowns at creation time
  6. Use Signals to track key milestones, e.g. first participant (child wf) to reach milestone X notify all participants.
    etc…

The basic lifecycle of the child/participant wfs might be:

  1. Send reminders
  2. Give them the next steps as they reach specific milestones
  3. Track time limit for specific milestones.
    etc…

With this structure how would one add a new child workflow after the parent/competition workflow has already started as new participants are added (up to some cutt-off date)?

Thank you!

You would send a signal or update to the workflow. It would start a new child when a signal or update is received. See

What if I wanted the paren workflow to wait for all it’s children to finish, e.g. as I created the initial set of children, I stored their futures in a slice and awaited on them? If I use a signal/update after the fact to trigger another ExecuteChildWorkflow how could I update that list of child wf futures the parent is waiting on?

There are many ways to achieve this. One option is to add them to have a Channel of these Futures and drain the channel in a loop. So each new child Future is added to the Channel, and the main workflow goroutine drains that channel until it is empty.

How can I know how many participants/child wfs there are in this case though? I can handle a signal to exec a new child workflow and then add that child wfs future to a selector, but I don’t know how many to wait for.

Currently I’m adding each child future to a list and as those futures complete incrementing a counter. Then waiting for that counter to reach the length of the child futures. Is that the right approach?

You can have some shutdown logic that closes the channel to indicate that no more children are coming.

Another option is to use a counter