danielgerlag / workflow-core

Lightweight workflow engine for .NET Standard
MIT License
5.39k stars 1.2k forks source link

WaitFor inside While Loop #1317

Closed vicentemont closed 6 hours ago

vicentemont commented 1 week ago

I'm trying to achieve the following:

while a request is on draft mode, the workflow keeps waiting for an update event. once that event is published, it updates the request with the new status. if its still in draft, it remains in the loop. The problem is that because the event is published once, the next time the loop runs and it will WaitFor the event, it assumes the that the event is already there and keeps updating and going through an infinite loop. Any thoughs on a way around this?

here is my code:

builder
            .StartWith<CreateTravelRequest>()
            .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)
            .Output(data => data.TravelRequestWorkflowData, step => step.TravelRequestWorkflowData)
            .While(data => data.TravelRequestWorkflowData.TravelRequest.StatusSlug == "DRAFT") 
            .Do(x => x
                .StartWith(context => Log.Information("IN THE WHILE"))
                .WaitFor("UPDATED", data => data.TravelRequestWorkflowData.TravelRequestId.ToString())
                .Output(data => data.TravelRequestWorkflowData, step => step.EventData)
                .Then<UpdateTravelRequest>()
                .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)
            )
            .Then<SubmitTravelRequest>()
            .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)
danielgerlag commented 1 week ago

See the EffectiveDate option in https://workflow-core.readthedocs.io/en/latest/external-events/

vicentemont commented 1 week ago

Hey, thanks! I figured it out already! But it took a while! For the record, this is how it ended up working:

builder
            .StartWith<CreateTravelRequest>()
            .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)
            .Output(data => data.TravelRequestWorkflowData, step => step.TravelRequestWorkflowData)
            .While(data => data.TravelRequestWorkflowData.TravelRequest.StatusSlug == "DRAFT") 
            .Do(x => x
                .StartWith(context => Log.Information("IN THE WHILE"))
                .WaitFor("UPDATED", data => data.TravelRequestWorkflowData.TravelRequestId.ToString(), date => DateTime.Now)
                .Output(data => data.TravelRequestWorkflowData, step => step.EventData)
                .Then<UpdateTravelRequest>()
                .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)
            )
            .Then<SubmitTravelRequest>()
            .Input(step => step.TravelRequestWorkflowData, data => data.TravelRequestWorkflowData)