Use case of sub process and sample logic to use

Hi all recently
we use selector to determine which flow to executed
now we have flow like this


we know how to make the process inside the sub process
but for the sub process part any idea what should we use from temporal feature

for !signalReceive && ctx.Err() == nil {
		selector := workflow.NewSelector(ctx)
		threeHourTimer := workflow.NewTimer(ctx, threeHour)
		selector.AddFuture(threeHourTimer, func(f workflow.Future) {
			err1 := f.Get(ctx, nil)
			if err1 == nil {
				logger.Debug("Success")
			} else if ctx.Err() != nil {
				logger.Error("Error occurred.", "error", ctx.Err())
			}
		})
		selector.AddReceive(workflow.GetSignalChannel(ctx, SignalAction),
			func(c workflow.ReceiveChannel, more bool) {
				c.Receive(ctx, &request)
			},
		)
		selector.Select(ctx)
	}

how do we make the sub process ?

  1. future action for timer 2 days, and only executed if inside sub process aren’t done within 2 days
1 Like

I would use a goroutine to implement your subprocess. Something like:

	subprocessDone := false
	workflow.Go(ctx, func(ctx workflow.Context) {
		signal := workflow.GetSignalChannel(ctx, "actionSignal")
		for {
			ok, more := signal.ReceiveWithTimeout(ctx, 3*time.Hour, &request)
			if ok {

			} else {
				// timed out
			}
		}
		subprocessDone = true
	})

	workflow.AwaitWithTimeout(ctx, 48*time.Hour, func() bool {
		return subprocessDone
	})

I would also recommend using Channel.ReceiveWithTimeout instead of the selector to listen on a single signal channel.

2 Likes

Thank You Maxim for the suggestion
We’ll try this

Hi Maxim,

Using this solution,

  1. if the 48-hour timer is triggered, do I need to cancel the 3-hour timer? I have a case in which the 48-hour timer is < than 3-hour timer. After the AwaitWithTimeout is triggered, the ReceiveWithTimeout() is also triggered when the timer is done.
  2. Should I use different ctx for workflow.Go() and AwaitWithTimeout()?
  1. Yes, you are correct. My code snippet missed the cancellation code.
  2. No. But inside the workflow.Go use the context that is passed as a parameter to the function.

Ok thank you, it works.