0

I'm using Masstransit 5.1.4 with sagas. I have a saga where I want to implement the following behaviour:

  • When a StartMsg is received -> transition to Active state
  • While in Active state, several events are processed
  • If a StartMsg is received in this state, the current saga is finalized and reprocess the StartMsg within a new saga instance

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
PGuevara
  • 3
  • 1

1 Answers1

0

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.

Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • I'm not sure to fully understand what you mean with the part of repository and "locking around correlationid" but anyway, I'll give it a try. The system bind events from different sources. It may fail sending the finish event and in such case the most likely reason is that a new chain of events has started so, I prefer a new saga. I believe the convenient solution is the one you point in the last paragraph about using a separated "correlationId" but I still have to work it out to see if it fits. @PhatBoyG, I really thank you your fast response and hard work around this project. – PGuevara Jul 10 '19 at 11:49
  • I marked this as answered because I wanted to know if there were available a different approach than re-sending to the queue. Anyway, if anyone feels there is any other suitable response, please go on (I'll keep an eye on this question) – PGuevara Jul 10 '19 at 11:59
  • The only other way I can think would be to reset the properties of the saga back to their initial state, and not create a new instance but "reset" the current instance back to that initially created state. – Chris Patterson Jul 11 '19 at 11:48