The recommended way to complete a saga instance?

39 views
Skip to first unread message

niklas...@gmail.com

unread,
Mar 4, 2019, 1:12:35 PM3/4/19
to masstransit-discuss

I have a scenario where a saga starts a batch process.

The saga sends commands to other endpoints and waits for published events from endpoints indicating completion.


The number of sent commands will depend on the size of the batch (as the number of handled events).


The saga will store information about expected events in the state and the saga should be completed based on logic using the state machine and handled events.

I expected to find a finalize method within the BehaviorContext but I couldn’t find it. I only find that the Finlize method can be used when declaring the saga in the constructor.

Should I use TransitionToState in combination with RaiseEvent, use SetCompleted((i) =>  true); or is there some other recommended way?


Regards,

Niklas

Chris Patterson

unread,
Mar 4, 2019, 1:45:08 PM3/4/19
to masstrans...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/15beabd2-869d-4d01-ab16-0d0ac6537dd1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

niklas...@gmail.com

unread,
Mar 5, 2019, 1:25:59 AM3/5/19
to masstransit-discuss
Thank you for the answer,
But the use case is like this.

...
 During(Active,
                When(BatchSubsetAdded)
                .ThenAsync((context) => Handle(context)),               
                When(BatchTimedOut.Received)
                .ThenAsync((context)=> Handle(context))
                .Finalize()
                );
...

  private async Task Handle(BehaviorContext<BatchState, BatchSubsetAdded> context)
        {           
           //Some logic to find if the saga is complete.. and if so

            await this.TransitionToState(context.Instance, Completed);
            this.RaiseEvent(context.Instance, BatchCompleted);

            //or...
            SetCompleted((i) =>  true);
        }

niklas...@gmail.com

unread,
Apr 8, 2019, 5:02:25 AM4/8/19
to masstransit-discuss
I cannot find best practice how to end the saga.
But I find that transition to the built in stat final in combination of SetCompletedWhenFinalized is the most common way.
In my case, publishing an event on completion, is:

0. Use UseInMemoryOutbox()
1. Use SetCompletedWhenFinalized()
2. Use logic to find if the saga instance is complete, publish the event and then call TransitionToState(context.Instance, Final).ConfigureAwait(false);

/Niklas
Reply all
Reply to author
Forward
0 new messages