workflow-core
workflow-core copied to clipboard
Howto: While in combination with WaitFor
Describe the bug When using While the WaitFor step does not block and as a result I get a workflow locked exception.
To Reproduce Steps to reproduce the behavior: Just a simple Workflow:
builder
.While(data => data.Continue)
.Do(loop => loop
.WaitFor("MyEvent", (data, context) => context.Workflow.Id)
.Output(data => data.Message, step => step.EventData)
.Then<MyStep>()
.Input(step => step.Continue, data => data.Continue));
Expected behavior I would expect the WaitFor blocking further execution.
Additional context There is already a closed issue here according to this problem. I found the hint to add DateTime.Now to the WaitFor statement.
This does work if the events come in quite slow.
My code producing the events looks like this:
host.PublishEvent("MyEvent", workflowId, data);
But these events are generated quite rapid and also not in strict order. So basically while the workflow is still processing one event more events are already incoming. So the DateTime.Now is then after some events, which results in loss of data.
Perhaps it helps also to understand what I am trying to achieve: Basically from another system lots of messages come in. One of them is a special message containing the number of messages send.
So in plain text my workflow shall loop until all messages have been collected. So my idea was to use While the condition is achieved the workflow should wait for the next message event, call a custom step to do the logic to determine if all messages where received and if that is not the case wait for the next message event.
(The next problem is that also there must be a timeout for this, but that is a different story)
I currently have solved it with DateTime.Now in the WaitFor statement on the receiving side and a Thread.Sleep on the sending side. But this is really ugly