workflow-core icon indicating copy to clipboard operation
workflow-core copied to clipboard

Event is published, before event subscription in workflow finished

Open weismantelf opened this issue 3 years ago • 0 comments

Hello,

I am using the following workflow:

    public void Build(IWorkflowBuilder<OPCModel> builder)
    {

        builder
            .StartWith(context => Console.WriteLine($"START {nameof(ConnectWorkflow)} WITH CONTEXT {context.Workflow.Id}"))
            .WaitFor("MyEvent", ev => "CONNECT", data => DateTime.Now)
            .Saga(sage =>
                sage
                    .If(data => !data.IsConnected).Do(doit => 
                      doit
                        .StartWith<OpcConnectionStep>()
                            .Input(step => step.Model, data => data)
                        .Then<OpcKeepAliveRegisterStep>()
                            .Input(step => step.Model, data => data)
                            .CompensateWithSequence(c => CompensationSteps.FailedKeepAliveRegistration(c))
                        .Then<OpcMonitorRegisterStep>()
                            .Input(step => step.Model, data => data)
                            .CompensateWithSequence(c => CompensationSteps.FailedMonitorRegistration(c))
                        .Then<OpcConnectedStep>()
                            .Input(step => step.Model, data => data)
                    )
            )
            .OnError(WorkflowErrorHandling.Retry, TimeSpan.FromSeconds(2))
            .CancelCondition(data => data.EvCancel.IsSet, true)
            .Then<PostCancelStep>()
                .Input(step => step.Model, data => data)
            .Then(context => Console.WriteLine($"WORKFLOW {nameof(ConnectWorkflow)} {context.Workflow.Id} FINISHED"));
    }

This workflow should be start immediately when I am starting a specific thread:

await _workflowHost.StartWorkflow(ConnectWorkflow.CWorkflowID, data);
await _workflowHost.PublishEvent("MyEvent", "CONNECT", "");

while (true)
{
    if (_evQuit.Wait(10)) break;
    .
    .
    .                
 }

My problem is, that the workflow does not receive the published event.

I was adding then some code after directly after the starting of the workflow, which is waiting until the subscription of the event is available in the PersistenceStoreStore:

        while (true)
        {
            var myEvent = await _opcModel.WorkflowHost.PersistenceStore.GetSubscriptions("MyEvent", "CONNECT", DateTime.Now);
            if (myEvent.Count() == 0)
            {
                await Task.Delay(50);
            }
            else
            {
                break;
            }
        }

Now the workflow is getting the event. So it means that the start workflow method is not waiting until the subscription is finished.

weismantelf avatar Nov 17 '21 10:11 weismantelf