I’m not sure I understand Selectors as the following code snippet has led to an infinite loop that I can’t explain why or how it happened (it’s only happened once)
for !state.IsCutoffReached {
logger.Info("Waiting for cutoff...", "state", state)
workflow.NewSelector(ctx).
AddFuture(state.getCutoffTimer(ctx), func(_ workflow.Future) {
currentEndTime := state.C.EndAt
c, err := activities.GetBaz(ctx, state.ID)
if err != nil {
logger.Error("Failed to refetch baz", "state", state, "error", err)
}
state.C = c
state.IsCutoffReached = !state.C.EndAt.After(currentEndTime)
}).
AddReceive(workflow.GetSignalChannel(ctx, workflows.SignalA), func(c workflow.ReceiveChannel, _ bool) {
signal := workflows.Receive[workflows.Foo](c, ctx)
state.startChildWorkflows(ctx, signal)
}).
AddReceive(workflow.GetSignalChannel(ctx, workflows.SignalB), func(c workflow.ReceiveChannel, _ bool) {
if !state.IsX {
signal := workflows.Receive[workflows.Bar](c, ctx)
state.IsX = true
}
}).
Select(ctx)
}
Basically I’m recreating a selector in a loop as I’m setting a Timer that fires when a specific entity’s (that exists in a separate Postgres DB) end_at is reached. This entity can be updated so when the first Timer fires I check to see if the end_at datetime was updated, if so recreate the selector and wait again.
What happened is I got into an infinite loop where the selector just kept getting recreated with no signal or timers firing.
Thank you.