Hi,
I want to trigger a function in parallel to listening to a signal. I’m trying to figure out that would look with selectors.
Let’s say I have
- a listener initialize to listen for a
skip
signal. I have a workflow variable that gets updated when the signal is received. I’ll use that to monitor if i can skip an operation. - I have a function
foo()
that does a set of operations.
The goal is is as foo() is executing, if any time i get the signal skip
i want to exit out of the foo() function.
The problem I’m struggling with is
- how to create the
Future
needed for theselector
when its not tied to an activity. In this case, it’s tied to a variable that is updated by another listener or the execution of a function. - This is probably not ideal but can a workflow have two listener for a signal. I think I tried this and it does not work.
type signalInfo struct {
skip bool
}
func (s *signalInfo) Listen(ctx workflow.Context) {
for {
selector := workflow.NewSelector(ctx)
selector.AddReceive(workflow.GetSignalChannel(ctx, "skip"), func(c workflow.ReceiveChannel, _ bool) {
skip = true
}
selector.Select(ctx)
}
func SampleWorkflow(ctx....) {
var signal signalInfo
workflow.Go(ctx, signal.Listen)
selector := workflow.NewSelector(ctx)
selector.AddFuture(..., func(f workflow.Future) { // Don' t know the proper syntax for this
// basically wait til signal.request = true
workflow.Await(ctx, func() bool {
return signal.skip == true
})
})
selector.AddFuture(..., func(f workflow.Future) { // Don't know the proper syntax for this either
// wait till foo() finish executing
})
selector.select(ctx);
}
func foo(){}
Because I don’t need to worry about concurrency, one option could be.
func SampleWorkflow(ctx....) {
var signal signalInfo
workflow.Go(ctx, signal.Listen)
fooDone:= false
workflow.Go(ctx, func(ctx ) {
err := foo()
fooDone = true
})
for {
if signal != nil || fooDone {
break
}
}
}
```
Wonder if there is a cleaner way