Waiting for external conditions before running workflow

Hi,

I was wondering what the best approach would be for waiting for a number of external conditions, before executing a workflow. Specifically, some data independent data imports run, and I need to wait for all of them to complete. The data imports are also temporal workflows.

For example, the workflow main body is something like:

for runs < LifecycleMaxEvents || selector.HasPending() {
		 
		if monitor.firstRun {

			// Here I want to wait for certain precondition are satisfied.

			err := monitor.Check(ctx, time.Time{}, time.Time{}, monitor.firstRun)
			if err != nil {
				return err
			}
			monitor.firstRun = false
			 
			continue
		}

		tCtx, cancel := workflow.WithCancel(ctx)
		timer := workflow.NewTimer(tCtx, time.Hour*24)
		selector.AddFuture(timer, func(f workflow.Future) {
	 
			err = monitor.Check(ctx, time.Time{}, time.Time{}, false)
			if err != nil {
				cancel()
			}
		})

		runs = runs + 1
		selector.Select(ctx)
	}

Can I for example, do external calls in a workflow.Await? Or this is not optimal?

Another option is I guess to signal this workflow from the external ones and set some local variables and await for those to be true?

Any suggestions, would be greatly appreciated.

I recommend using activity retry or always running a heartbeating activity for such a check. For more ideas, see this post.

BTW why do you use Timer with Selector instead of just workflow.Sleep?

timer := workflow.NewTimer(tCtx, time.Hour*24)
		selector.AddFuture(timer, func(f workflow.Future) {
	 
			err = monitor.Check(ctx, time.Time{}, time.Time{}, false)
			if err != nil {
				cancel()
			}
		})

Can be reduced to:

err := workflow.Sleep(tCtx, time.Hours * 24)
if err != nil {
   // handle cancellation
}
err = monitor.Check(ctx, time.Time{}, time.Time{}, false)

Thanks for the tips @maxim! So basically have an activity that returns a non error only when the precondition is met.

As for the selector, no specific reason. I will simplify it to your suggested approach. What is a good way to handle the cancel? Just log in? I do not want the workflow to fail in general.

Cancellation is an external request for the workflow to perform cleanup and stop executing. By using a disconnected context, you can code your workflow to ignore these requests.