Goe_PK
Goe_PK

Reputation: 23

MassTransit Saga State : Would like to do action after moving the state

I am implementing MassTransit SAGA state machine with states "Initial" > "Pending Acknowledged" > "Acknowledged" > "Finalized" . The "Pending Acknowledged" and "Acknowledged" can be switched. But I would like to act do something after changing state from "Pending Acknowledged" to "Acknowledged".

Currently , I try to add thenAsync task after transition to "AcknowLedged". I found that the state will not be moved to "Acknowledged" when DoSomeThing task was called and action. It is not working as expect.

 **During(PendingAcknowledged)**,
 When(DoAcknowledged)
   .ThenAsync(MarkAcknowledged)
   .Then(context => Log.Information("{@DoAcknowledge}", context.Instance))
   **.TransitionTo(Acknowledged),**
   **.ThenAsync(DoSomeThing)**

Any suggestion ? How can I do it ?

Upvotes: 0

Views: 2073

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33268

Upon event receipt, MassTransit:

  1. Loads an existing or creates a new saga instance
  2. Executes all activities associated with the current state at the time the instance was loaded
  3. Saves the saga instance to the saga repository

Having multiple TransitionTo activities in a state machine is allowed, but the saga instance will only be saved with the last state after all activities have completed.

So, from your example, this is entirely legal and will save the Acknowledged state to the repository.

During(PendingAcknowledged),
    When(DoAcknowledged)
        .ThenAsync(MarkAcknowledged)
        .Then(context => Log.Information("{@DoAcknowledge}", context.Instance))
        .TransitionTo(Acknowledged),
        .ThenAsync(DoSomeThing)

Upvotes: 2

Related Questions