0

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 ?

Goe_PK
  • 13
  • 5
  • If you haven't see it, there is an entire season of episodes on state machines available on [YouTube](https://youtube.com/playlist?list=PLx8uyNNs1ri1UA_Nerr7Ej3g9nT2PxbbH). You might improve your understanding there. – Chris Patterson Dec 20 '20 at 00:22

1 Answers1

1

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)
Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • Hi Chris, problem is I want to dosomething after transition to "acknowledged" not complete all activities then move to "acknowledged". So putting transition command before thenasync won't help , right ? Thank you very much for your support. – Goe_PK Dec 19 '20 at 23:36
  • You can do things after the `TransitionTo` statement, as I've shown above. How is your expectation different from what I explained? – Chris Patterson Dec 20 '20 at 00:21
  • The MarkAcknowledged is working but transition to acknowledged state and dosomething are not called. That is my issue now. – Goe_PK Dec 20 '20 at 08:43
  • If you have a standalone example of the issue (via GitHub), feel free to share it. Without that, I don't believe there is any issue with Automatonymous and somehow your ThenAsync is blocking the rest of the behavior from executing. – Chris Patterson Dec 20 '20 at 14:46
  • Chris, I have reviewed my coding again and found a bug from my coding. DoSomething was called per expect now. Thank you very much for your help. – Goe_PK Dec 21 '20 at 11:02
  • Chris, Is there a way to transition the state to specified state in ThenAync() task (Not usingTransitionTo syntax in During/when ). Example: I would like to have a logic to transition state or not in DoSomeTask and not using TransitionTo(Acknowledged). – Goe_PK Dec 24 '20 at 10:03
  • That isn't recommended, there are .If(), .IfElse() expressions that can be used if you have conditional logic for transitions. – Chris Patterson Dec 24 '20 at 13:51