# Use case of sub process and sample logic to use

**URL:** <https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034>\
**Category:** Community Support\
**Tags:** go-sdk\
**Created:** [January 24, 2023, 8:12am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034 "2023-01-24T08:12:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meli\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/meli_lee/32/1851_2.png) [@Meli\_Lee](https://community.temporal.io/u/Meli_Lee)\
**Post date:** [January 24, 2023, 8:12am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/1 "2023-01-24T08:12:37Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/a/ad20ffe0164e74696c1bce2b4cc5abbe574f1a41.png)  
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

```auto
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

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [January 24, 2023, 4:27pm UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/3 "2023-01-24T16:27:58Z")

</div>

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

```auto
	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.

---

<div class="post-metadata">

**Author:** ![Meli\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/meli_lee/32/1851_2.png) [@Meli\_Lee](https://community.temporal.io/u/Meli_Lee)\
**Post date:** [January 25, 2023, 1:51am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/4 "2023-01-25T01:51:29Z")

</div>

Thank You Maxim for the suggestion  
We’ll try this

---

<div class="post-metadata">

**Author:** ![Bryan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bryan/32/3245_2.png) [@Bryan](https://community.temporal.io/u/Bryan)\
**Post date:** [May 4, 2023, 6:20am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/5 "2023-05-04T06:20:53Z")

</div>

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()?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [May 6, 2023, 1:38am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/6 "2023-05-06T01:38:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bryan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bryan/32/3245_2.png) [@Bryan](https://community.temporal.io/u/Bryan)\
**Post date:** [May 8, 2023, 6:03am UTC](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034/7 "2023-05-08T06:03:54Z")

</div>

Ok thank you, it works.
