I'm using Masstransit 5.1.4 with sagas. I have a saga where I want to implement the following behaviour:
How can I achieve this?
Here is a small pseudocode
Initially(
When(StartEvt)
.Then(...)
.TransitionTo(Active)
);
During(Active,
When(OneEvt)
.Then(...),
When(AnotherEvt
.Then(...),
When(EndEvt)
.Finalize(),
When(StartEvt)
// Finalize current saga
// Transition to initial state reprocessing StartMessage bound to StartEvt
You would need to finalize the current saga, and publish an event (or send it, to the same queue, using Send
) which would create a new saga instance. In this case, you'd also need to use a saga repository which would properly lock around the correlationId, so that you aren't processing two messages at the same time for the same correlation, which would likely find the existing instance before it was deleted.
You could NOT do it in a single message handler, although, thinking about it, why would you need to finalize, versus resetting the current saga instance back to an initial state?
Also, you might consider using a separate correlationId for the subsequent startMsg, so that you can keep track of each "attempt" at finishing. Otherwise, you won't know if late messages from a previous attempt would confuse the actual state of the current attempt - since you'd have no way to distinguish them.